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             package Rose::HTML::Object::MakeMethods::Generic;
2              
3 43     43   351 use strict;
  43         1389  
  43         1278  
4              
5 43     43   1544 use Carp();
  43         135  
  43         778  
6              
7 43     43   288 use base 'Rose::Object::MakeMethods';
  43         111  
  43         34560  
8              
9             our $VERSION = '0.606';
10              
11             sub array
12             {
13 500     500 0 27171 my($class, $name, $args) = @_;
14              
15 500         5793 require Rose::HTML::Text;
16              
17 500         823 my %methods;
18              
19 500   66     1447 my $key = $args->{'hash_key'} || $name;
20 500   100     1103 my $interface = $args->{'interface'} || 'get_set';
21              
22             #unless($class->can('parent'))
23             #{
24             # $methods{'parent'} = sub
25             # {
26             # my($self) = shift;
27             # return Scalar::Util::weaken($self->{'parent'} = shift) if(@_);
28             # return $self->{'parent'};
29             # };
30             #}
31              
32 500 50       4270 if($interface eq 'get_set_init')
    100          
    50          
    100          
    100          
    100          
    100          
    100          
    50          
    100          
    100          
    50          
33             {
34 0   0     0 my $init_method = $args->{'init_method'} || "init_$name";
35              
36             $methods{$name} = sub
37             {
38 0     0   0 my($self) = shift;
39              
40             # If called with no arguments, return array contents
41 0 0       0 unless(@_)
42             {
43 0 0       0 $self->{$key} = $self->$init_method() unless(defined $self->{$key});
44 0 0 0     0 return wantarray ? @{$self->{$key} ||= []} : $self->{$key};
  0         0  
45             }
46              
47             # If called with a array ref, set new value
48 0 0 0     0 if(@_ == 1 && ref $_[0] eq 'ARRAY')
49             {
50 0         0 $self->{$key} = [ map { _coerce_html_object($self, $_) } @{$_[0]} ];
  0         0  
  0         0  
51             }
52             else
53             {
54 0         0 $self->{$key} = [ map { _coerce_html_object($self, $_) } @_ ];
  0         0  
55             }
56              
57 0 0       0 return wantarray ? @{$self->{$key}} : $self->{$key};
  0         0  
58             }
59 0         0 }
60             elsif($interface eq 'get_set_inited')
61             {
62             $methods{$name} = sub
63             {
64 2624     2624   4547 my($self) = shift;
65              
66             # If called with no arguments, return array contents
67 2624 100       5559 unless(@_)
68             {
69 2174 100       5523 $self->{$key} = [] unless(defined $self->{$key});
70 2174 100       5640 return wantarray ? @{$self->{$key}} : $self->{$key};
  902         2667  
71             }
72              
73             # If called with a array ref, set new value
74 450 100 100     2070 if(@_ == 1 && ref $_[0] eq 'ARRAY')
75             {
76 8         19 $self->{$key} = [ map { _coerce_html_object($self, $_) } @{$_[0]} ];
  8         24  
  8         21  
77             }
78             else
79             {
80 442         904 $self->{$key} = [ map { _coerce_html_object($self, $_) } @_ ];
  443         994  
81             }
82              
83 450 50       1352 return wantarray ? @{$self->{$key}} : $self->{$key};
  0         0  
84             }
85 43         257 }
86             elsif($interface eq 'get_set_item')
87             {
88             $methods{$name} = sub
89             {
90 0     0   0 my($self) = shift;
91              
92 0 0       0 Carp::croak "Missing array index" unless(@_);
93              
94 0 0       0 if(@_ == 2)
95             {
96 0         0 return $self->{$key}[$_[0]] = _coerce_html_object($self, $_[1]);
97             }
98             else
99             {
100 0         0 return $self->{$key}[$_[0]]
101             }
102             }
103 0         0 }
104             elsif($interface eq 'get_item')
105             {
106             $methods{$name} = sub
107             {
108 7     7   18 my($self) = shift;
109 7 50       15 Carp::croak "Missing array index" unless(@_);
110 7         29 return $self->{$key}[$_[0]];
111             }
112 43         237 }
113             elsif($interface eq 'delete_item')
114             {
115             $methods{$name} = sub
116             {
117 3     3   8 my($self) = shift;
118 3 50       8 Carp::croak "Missing array index" unless(@_);
119 43     43   390 no warnings;
  43         1184  
  43         41503  
120 3 50       6 splice(@{$self->{$key} || []}, $_[0], 1);
  3         13  
121             }
122 43         341 }
123             elsif($interface eq 'unshift')
124             {
125             $methods{$name} = sub
126             {
127 3     3   8 my($self) = shift;
128 3 50       9 Carp::croak "Missing value(s) to add" unless(@_);
129 3 50 100     10 unshift(@{$self->{$key} ||= []}, map { _coerce_html_object($self, $_) } (@_ == 1 && ref $_[0] eq 'ARRAY') ? @{$_[0]} : @_);
  3   33     33  
  3         9  
  0         0  
130             }
131 69         454 }
132             elsif($interface eq 'shift')
133             {
134             $methods{$name} = sub
135             {
136 3     3   8 my($self) = shift;
137 3 50 50     10 return splice(@{$self->{$key} ||= []}, 0, $_[0]) if(@_);
  3         16  
138 0   0     0 return shift(@{$self->{$key} ||= []})
  0         0  
139             }
140 69         2418 }
141             elsif($interface eq 'clear')
142             {
143             $methods{$name} = sub
144             {
145 3     3   19 $_[0]->{$key} = []
146             }
147 69         430 }
148             elsif($interface eq 'reset')
149             {
150             $methods{$name} = sub
151             {
152 0     0   0 $_[0]->{$key} = undef;
153             }
154 0         0 }
155             elsif($interface =~ /^(?:push|add)$/)
156             {
157             $methods{$name} = sub
158             {
159 8     8   25 my($self) = shift;
160 8 50       26 Carp::croak "Missing value(s) to add" unless(@_);
161 8 50 100     16 push(@{$self->{$key} ||= []}, map { _coerce_html_object($self, $_) } (@_ == 1 && ref $_[0] && ref $_[0] eq 'ARRAY') ? @{$_[0]} : @_);
  8   66     101  
  9         27  
  0         0  
162             }
163 69         467 }
164             elsif($interface eq 'pop')
165             {
166             $methods{$name} = sub
167             {
168 5     5   12 my($self) = shift;
169              
170 5 50       17 if(@_)
171             {
172 5   50     20 my $a = $self->{$key} ||= [];
173 5         12 my $offset = @$a - $_[0];
174 5 100       24 return splice(@$a, $offset < 0 ? 0 : $offset)
175             }
176              
177 0   0     0 return pop(@{$self->{$key} ||= []})
  0         0  
178             }
179 69         1801 }
180             elsif($interface eq 'get_set')
181             {
182             $methods{$name} = sub
183             {
184 2404     2404   3779 my($self) = shift;
185              
186             # If called with no arguments, return array contents
187 2404 50       4864 unless(@_)
188             {
189 2404 100       8587 return wantarray ? (defined $self->{$key} ? @{$self->{$key}} : ()) : $self->{$key}
  36 50       152  
190             }
191              
192             # If called with a array ref, set new value
193 0 0 0     0 if(@_ == 1 && ref $_[0] eq 'ARRAY')
194             {
195 0         0 $self->{$key} = [ map { _coerce_html_object($self, $_) } @{$_[0]} ];
  0         0  
  0         0  
196             }
197             else
198             {
199 0         0 $self->{$key} = [ map { _coerce_html_object($self, $_) } @_ ];
  0         0  
200             }
201              
202 0 0       0 return wantarray ? @{$self->{$key}} : $self->{$key};
  0         0  
203             }
204 26         235 }
205 0         0 else { Carp::croak "Unknown interface: $interface" }
206              
207 500         2956 return \%methods;
208             }
209              
210             sub _coerce_html_object
211             {
212 463     463   990 my($self, $arg) = (shift, shift);
213              
214 463 100       1837 if(!ref $arg)
    50          
215             {
216 35         179 return Rose::HTML::Text->new(text => $arg, parent => $self);
217             }
218             elsif(!$arg->isa('Rose::HTML::Object'))
219             {
220 0         0 return Rose::HTML::Text->new(text => $arg, parent => $self);
221             }
222              
223 428         1408 $arg->parent($self);
224              
225 428         2379 return $arg;
226             }
227              
228             1;