File Coverage

blib/lib/Rose/HTML/Object/MakeMethods/Generic.pm
Criterion Covered Total %
statement 75 115 65.2
branch 47 82 57.3
condition 16 37 43.2
subroutine 15 18 83.3
pod 0 1 0.0
total 153 253 60.4


line stmt bran cond sub pod time code
1              
2             use strict;
3 43     43   274  
  43         93  
  43         3203  
4             use Carp();
5 43     43   189  
  43         80  
  43         624  
6             use base 'Rose::Object::MakeMethods';
7 43     43   174  
  43         1166  
  43         27845  
8             our $VERSION = '0.606';
9              
10             {
11             my($class, $name, $args) = @_;
12              
13 500     500 0 21828 require Rose::HTML::Text;
14              
15 500         4646 my %methods;
16              
17 500         678 my $key = $args->{'hash_key'} || $name;
18             my $interface = $args->{'interface'} || 'get_set';
19 500   66     2211  
20 500   100     938 #unless($class->can('parent'))
21             #{
22             # $methods{'parent'} = sub
23             # {
24             # my($self) = shift;
25             # return Scalar::Util::weaken($self->{'parent'} = shift) if(@_);
26             # return $self->{'parent'};
27             # };
28             #}
29              
30             if($interface eq 'get_set_init')
31             {
32 500 50       3652 my $init_method = $args->{'init_method'} || "init_$name";
    100          
    50          
    100          
    100          
    100          
    100          
    100          
    50          
    100          
    100          
    50          
33              
34 0   0     0 $methods{$name} = sub
35             {
36             my($self) = shift;
37              
38 0     0   0 # If called with no arguments, return array contents
39             unless(@_)
40             {
41 0 0       0 $self->{$key} = $self->$init_method() unless(defined $self->{$key});
42             return wantarray ? @{$self->{$key} ||= []} : $self->{$key};
43 0 0       0 }
44 0 0 0     0  
  0         0  
45             # If called with a array ref, set new value
46             if(@_ == 1 && ref $_[0] eq 'ARRAY')
47             {
48 0 0 0     0 $self->{$key} = [ map { _coerce_html_object($self, $_) } @{$_[0]} ];
49             }
50 0         0 else
  0         0  
  0         0  
51             {
52             $self->{$key} = [ map { _coerce_html_object($self, $_) } @_ ];
53             }
54 0         0  
  0         0  
55             return wantarray ? @{$self->{$key}} : $self->{$key};
56             }
57 0 0       0 }
  0         0  
58             elsif($interface eq 'get_set_inited')
59 0         0 {
60             $methods{$name} = sub
61             {
62             my($self) = shift;
63              
64 2622     2622   3681 # If called with no arguments, return array contents
65             unless(@_)
66             {
67 2622 100       4824 $self->{$key} = [] unless(defined $self->{$key});
68             return wantarray ? @{$self->{$key}} : $self->{$key};
69 2172 100       4472 }
70 2172 100       4853  
  901         2247  
71             # If called with a array ref, set new value
72             if(@_ == 1 && ref $_[0] eq 'ARRAY')
73             {
74 450 100 100     1596 $self->{$key} = [ map { _coerce_html_object($self, $_) } @{$_[0]} ];
75             }
76 8         14 else
  8         15  
  8         20  
77             {
78             $self->{$key} = [ map { _coerce_html_object($self, $_) } @_ ];
79             }
80 442         733  
  443         845  
81             return wantarray ? @{$self->{$key}} : $self->{$key};
82             }
83 450 50       1145 }
  0         0  
84             elsif($interface eq 'get_set_item')
85 43         272 {
86             $methods{$name} = sub
87             {
88             my($self) = shift;
89              
90 0     0   0 Carp::croak "Missing array index" unless(@_);
91              
92 0 0       0 if(@_ == 2)
93             {
94 0 0       0 return $self->{$key}[$_[0]] = _coerce_html_object($self, $_[1]);
95             }
96 0         0 else
97             {
98             return $self->{$key}[$_[0]]
99             }
100 0         0 }
101             }
102             elsif($interface eq 'get_item')
103 0         0 {
104             $methods{$name} = sub
105             {
106             my($self) = shift;
107             Carp::croak "Missing array index" unless(@_);
108 7     7   14 return $self->{$key}[$_[0]];
109 7 50       14 }
110 7         20 }
111             elsif($interface eq 'delete_item')
112 43         243 {
113             $methods{$name} = sub
114             {
115             my($self) = shift;
116             Carp::croak "Missing array index" unless(@_);
117 3     3   5 no warnings;
118 3 50       7 splice(@{$self->{$key} || []}, $_[0], 1);
119 43     43   1360 }
  43         1111  
  43         32161  
120 3 50       3 }
  3         11  
121             elsif($interface eq 'unshift')
122 43         255 {
123             $methods{$name} = sub
124             {
125             my($self) = shift;
126             Carp::croak "Missing value(s) to add" unless(@_);
127 3     3   5 unshift(@{$self->{$key} ||= []}, map { _coerce_html_object($self, $_) } (@_ == 1 && ref $_[0] eq 'ARRAY') ? @{$_[0]} : @_);
128 3 50       8 }
129 3 50 100     6 }
  3   33     28  
  3         7  
  0         0  
130             elsif($interface eq 'shift')
131 69         304 {
132             $methods{$name} = sub
133             {
134             my($self) = shift;
135             return splice(@{$self->{$key} ||= []}, 0, $_[0]) if(@_);
136 3     3   7 return shift(@{$self->{$key} ||= []})
137 3 50 50     8 }
  3         13  
138 0   0     0 }
  0         0  
139             elsif($interface eq 'clear')
140 69         1334 {
141             $methods{$name} = sub
142             {
143             $_[0]->{$key} = []
144             }
145 3     3   16 }
146             elsif($interface eq 'reset')
147 69         277 {
148             $methods{$name} = sub
149             {
150             $_[0]->{$key} = undef;
151             }
152 0     0   0 }
153             elsif($interface =~ /^(?:push|add)$/)
154 0         0 {
155             $methods{$name} = sub
156             {
157             my($self) = shift;
158             Carp::croak "Missing value(s) to add" unless(@_);
159 8     8   59 push(@{$self->{$key} ||= []}, map { _coerce_html_object($self, $_) } (@_ == 1 && ref $_[0] && ref $_[0] eq 'ARRAY') ? @{$_[0]} : @_);
160 8 50       26 }
161 8 50 100     12 }
  8   66     88  
  9         24  
  0         0  
162             elsif($interface eq 'pop')
163 69         1493 {
164             $methods{$name} = sub
165             {
166             my($self) = shift;
167              
168 5     5   11 if(@_)
169             {
170 5 50       13 my $a = $self->{$key} ||= [];
171             my $offset = @$a - $_[0];
172 5   50     15 return splice(@$a, $offset < 0 ? 0 : $offset)
173 5         12 }
174 5 100       21  
175             return pop(@{$self->{$key} ||= []})
176             }
177 0   0     0 }
  0         0  
178             elsif($interface eq 'get_set')
179 69         369 {
180             $methods{$name} = sub
181             {
182             my($self) = shift;
183              
184 2404     2404   3134 # If called with no arguments, return array contents
185             unless(@_)
186             {
187 2404 50       3819 return wantarray ? (defined $self->{$key} ? @{$self->{$key}} : ()) : $self->{$key}
188             }
189 2404 100       6943  
  36 50       130  
190             # If called with a array ref, set new value
191             if(@_ == 1 && ref $_[0] eq 'ARRAY')
192             {
193 0 0 0     0 $self->{$key} = [ map { _coerce_html_object($self, $_) } @{$_[0]} ];
194             }
195 0         0 else
  0         0  
  0         0  
196             {
197             $self->{$key} = [ map { _coerce_html_object($self, $_) } @_ ];
198             }
199 0         0  
  0         0  
200             return wantarray ? @{$self->{$key}} : $self->{$key};
201             }
202 0 0       0 }
  0         0  
203             else { Carp::croak "Unknown interface: $interface" }
204 26         161  
205 0         0 return \%methods;
206             }
207 500         2453  
208             {
209             my($self, $arg) = (shift, shift);
210              
211             if(!ref $arg)
212 463     463   810 {
213             return Rose::HTML::Text->new(text => $arg, parent => $self);
214 463 100       1534 }
    50          
215             elsif(!$arg->isa('Rose::HTML::Object'))
216 35         161 {
217             return Rose::HTML::Text->new(text => $arg, parent => $self);
218             }
219              
220 0         0 $arg->parent($self);
221              
222             return $arg;
223 428         1121 }
224              
225 428         2040 1;