File Coverage

blib/lib/WE_Content/Base.pm
Criterion Covered Total %
statement 14 96 14.5
branch 2 44 4.5
condition n/a
subroutine 3 14 21.4
pod 8 11 72.7
total 27 165 16.3


line stmt bran cond sub pod time code
1             # -*- perl -*-
2              
3             #
4             # $Id: Base.pm,v 1.14 2005/05/13 09:53:13 eserte Exp $
5             # Author: Slaven Rezic
6             #
7             # Copyright (C) 2001 Online Office Berlin. All rights reserved.
8             # Copyright (C) 2002 Slaven Rezic.
9             # This is free software; you can redistribute it and/or modify it under the
10             # terms of the GNU General Public License, see the file COPYING.
11              
12             #
13             # Mail: slaven@rezic.de
14             # WWW: http://we-framework.sourceforge.net
15             #
16              
17             package WE_Content::Base;
18              
19 2     2   11 use strict;
  2         5  
  2         64  
20 2     2   11 use vars qw($VERSION);
  2         3  
  2         2706  
21             $VERSION = sprintf("%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/);
22              
23             sub new {
24 0     0 0 0 my($self, %args) = @_;
25 0         0 my $buf = $self->get_string(%args);
26 0         0 my $class = $self->guess_class($buf);
27 0         0 my %newargs = %args;
28 0         0 delete $newargs{$_} for ("-string", "-file");
29 0         0 $newargs{-string} = $buf;
30 0         0 $class->new(%newargs);
31             }
32              
33             sub debug {
34 0     0 0 0 my $self = shift;
35 0 0       0 if (@_) {
36 0         0 $self->{Debug} = $_[0];
37             }
38 0         0 $self->{Debug};
39             }
40              
41             sub clone {
42 0     0 1 0 my($self, $as) = @_;
43 0         0 require Data::Dumper;
44 0         0 my $o;
45 0         0 eval Data::Dumper->new([$self],['o'])->Indent(0)->Purity(1)->Dump;
46 0 0       0 die $@ if $@;
47 0 0       0 if (defined $as) {
48 0         0 bless $o, $as;
49             }
50 0         0 $o;
51             }
52              
53             sub get_string {
54 1     1 1 3 my($self, %args) = @_;
55 1         3 my($buf);
56 1 50       5 if ($args{-file}) {
    0          
57 1 50       91 open(F, "< $args{-file}") or die "Can't read $args{-file}: $!";
58 1         6 local $/ = undef;
59 1         46 $buf = ;
60 1         14 close F;
61             } elsif ($args{-string}) {
62 0         0 $buf = $args{-string};
63             } else {
64 0         0 die "Either -string or -file must be specified";
65             }
66 1         5 $buf;
67             }
68              
69             sub guess_class {
70 0     0 1   my($self, $buf) = @_;
71 0 0         if ($buf =~ /^---( \#YAML:)?/) {
    0          
72 0           require WE_Content::YAML;
73 0           'WE_Content::YAML';
74             } elsif ($buf =~ /^<\?xml/) {
75 0 0         if ($buf =~ /
76 0           require WE_Content::XMLText;
77 0           'WE_Content::XMLText';
78             } else {
79 0           require WE_Content::XML;
80 0           'WE_Content::XML';
81             }
82             } else {
83 0           require WE_Content::PerlDD;
84 0           'WE_Content::PerlDD';
85             }
86             }
87              
88             sub _by_path {
89 0     0     my $self = shift;
90 0           my $path = shift;
91 0           my $sep = shift;
92 0           my $do_set;
93             my $value;
94 0 0         if (@_) {
95 0           $do_set = 1;
96 0           $value = shift;
97             }
98 0           my $o = $self->{Object};
99 0 0         return undef if !$o;
100 0           my $o_ref = \$o;
101 0           my @path;
102 0 0         $sep = "/" if !defined $sep;
103 0           my $sep_rx = quotemeta $sep;
104 0 0         if (UNIVERSAL::isa($path, 'ARRAY')) {
105 0           @path = @$path;
106             } else {
107 0           $path =~ s|^$sep_rx/+||;
108 0           @path = split $sep_rx, $path;
109             }
110 0           foreach my $p (@path) {
111 0 0         if (UNIVERSAL::isa($o, 'ARRAY')) {
    0          
112 0 0         if (defined $o->[$p]) {
113 0           $o_ref = \$o->[$p];
114 0           $o = $o->[$p];
115             } else {
116 0 0         if ($do_set) {
117 0           die "Can't set $path to $value";
118             }
119 0           return undef;
120             }
121             } elsif (UNIVERSAL::isa($o, 'HASH')) {
122 0 0         if (exists $o->{$p}) {
123 0           $o_ref = \$o->{$p};
124 0           $o = $o->{$p};
125             } else {
126 0 0         if ($do_set) {
127 0           die "Can't set $path to $value";
128             }
129 0           return undef;
130             }
131             } else {
132 0           die "Can't handle " . ref($o) . " ($o) from path @path";
133             }
134             }
135 0 0         if ($do_set) {
136 0           $$o_ref = $value;
137             }
138 0           $o;
139             }
140              
141 0     0 1   sub by_path { shift->_by_path(@_, "/") }
142 0     0 1   sub by_dotted { shift->_by_path(@_, ".") }
143 0     0 1   sub set_by_path { shift->by_path(shift, "/", @_) }
144 0     0 1   sub set_by_dotted { shift->by_dotted(shift, ".", @_) }
145              
146             # "prototype": new name, "template": old name
147             sub is_prototype {
148 0     0 1   $_[0]->{Type} eq 'template';
149             }
150              
151             sub serialize_as {
152 0     0 0   my $self = shift;
153 0           my $as = shift;
154 0 0         if ($as !~ /^WE_Content::/) {
155 0           $as = "WE_Content::$as";
156             }
157 0           eval "require $as";
158 0 0         die $@ if $@;
159 0           require Storable;
160 0           my $clone = Storable::dclone($self);
161 0           bless $clone, $as; # re-bless
162 0           $clone->serialize(@_);
163             }
164              
165             1;
166              
167             __END__