blib/lib/Class/MakeMethods/Composite/Inheritable.pm

filter: Statementstmt 55.0% 88 / 160 Branchbran 42.5% 34 / 80 Conditioncond 25.7% 9 / 35 MC/DCmcdc 9.0% 2 / 22 Subroutinesub 58.0% 18 / 31 Podpod 83.3% 5 / 6 totaltotal 46.7% 156 / 334 CC 57 SCAR 34.3
CC
57
Cov
48%
__ANON__
156.0
__ANON__
21.3
__ANON__
19.6
CRAP 30.9
« blib/lib/Class/MakeMethods/Composite/Hash.pm ↑ Summary blib/lib/Class/MakeMethods/Template.pm »
1 =head1 NAME
2  
3 Class::MakeMethods::Composite::Inheritable - Overridable data
4  
5 =head1 SYNOPSIS
6  
7 package MyClass;
8  
9 use Class::MakeMethods( 'Composite::Inheritable:scalar' => 'foo' );
10 # We now have an accessor method for an "inheritable" scalar value
11
12 MyClass->foo( 'Foozle' ); # Set a class-wide value
13 print MyClass->foo(); # Retrieve class-wide value
14
15 my $obj = MyClass->new(...);
16 print $obj->foo(); # All instances "inherit" that value...
17
18 $obj->foo( 'Foible' ); # until you set a value for an instance.
19 print $obj->foo(); # This now finds object-specific value.
20 ...
21
22 package MySubClass;
23 @ISA = 'MyClass';
24
25 print MySubClass->foo(); # Intially same as superclass,
26 MySubClass->foo('Foobar'); # but overridable per subclass,
27 print $subclass_obj->foo(); # and shared by its instances
28 $subclass_obj->foo('Fosil');# until you override them...
29 ...
30
31 # Similar behaviour for hashes and arrays is currently incomplete
32 package MyClass;
33 use Class::MakeMethods::Composite::Inheritable (
34 array => 'my_list',
35 hash => 'my_index',
36 );
37
38 MyClass->my_list(0 => 'Foozle', 1 => 'Bang!');
39 print MyClass->my_list(1);
40
41 MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
42 print MyClass->my_index('foo');
43  
44  
45 =head1 DESCRIPTION
46  
47 The MakeMethods subclass provides accessor methods that search an inheritance tree to find a value. This allows you to set a shared or default value for a given class, optionally override it in a subclass, and then optionally override it on a per-instance basis.
48  
49 Note that all MakeMethods methods are inheritable, in the sense that they work as expected for subclasses. These methods are different in that the I<data> accessed by each method can be inherited or overridden in each subclass or instance. See L< Class::MakeMethods::Utility::Inheritable> for more about this type of "inheritable" or overridable" data.
50  
51  
52 =head2 Class::MakeMethods Calling Interface
53  
54 When you C<use> this package, the method declarations you provide
55 as arguments cause subroutines to be generated and installed in
56 your module.
57  
58 See L<Class::MakeMethods::Standard/"Calling Conventions"> for more information.
59  
60 =head2 Class::MakeMethods::Standard Declaration Syntax
61  
62 To declare methods, pass in pairs of a method-type name followed
63 by one or more method names.
64  
65 See the "METHOD GENERATOR TYPES" section below for a list of the supported values of I<generator_type>.
66  
67 See L<Class::MakeMethods::Standard/"Declaration Syntax"> and L<Class::MakeMethods::Standard/"Parameter Syntax"> for more information.
68  
69 =cut
70  
71 package Class::MakeMethods::Composite::Inheritable;
72  
73 3 $VERSION = 1.000;
74 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 3 ▶use Class::MakeMethods::Composite '-isasubclass';
Subroutine BEGIN: called CC=1 SCAR=0.0
78 3 ▶use Class::MakeMethods::Utility::Inheritable qw(get_vvalue set_vvalue find_vself );
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 ########################################################################
81  
82 =head1 METHOD GENERATOR TYPES
83  
84 =head2 scalar - Overrideable Accessor
85  
86 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
87  
88 =over 4
89  
90 =item *
91  
92 May be called as a class or instance method, on the declaring class or any subclass.
93  
94 =item *
95  
96 If called without any arguments returns the current value for the callee. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located.
97  
98 =item *
99  
100 If called with an argument, stores that as the value associated with the callee, whether instance or class, and returns it,
101  
102 =item *
103  
104 If called with multiple arguments, stores a reference to a new array with those arguments as contents, and returns that array reference.
105  
106 =back
107  
108 Sample declaration and usage:
109  
110 package MyClass;
111 use Class::MakeMethods::Composite::Inheritable (
112 scalar => 'foo',
113 );
114 ...
115
116 # Store value
117 MyClass->foo('Foozle');
118
119 # Retrieve value
120 print MyClass->foo;
121  
122 =cut
123  
124 3 ▶use vars qw( %ScalarFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 2 ▶sub scalar {
Subroutine scalar: called CC=1 SCAR=0.0
Pod documented
127 2 (shift)->_build_composite( \%ScalarFragments, @_ );
128 }
129  
130 %ScalarFragments = (
131 '' => [
132 3 ▶ '+init' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
133 3 my ($method) = @_;
134 3 ▶ $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass};
Condition33%1/3
$method->{"target_class"} ||= $Class::MethodMaker::CONTEXT{"TargetClass"}
l!l&&r!l&&!r
003
MC/DC0%0/2
$method->{"target_class"} ||= $Class::MethodMaker::CONTEXT{"TargetClass"}
$method->{"target_class"} $Class::MethodMaker::CONTEXT{"TargetClass"}
Truth table33%1/3
A ||= B
A= $method->{"target_class"}B= $Class::MethodMaker::CONTEXT{"TargetClass"}
ABresult
1X1
011
000
135 3 ▶ $method->{data} ||= {};
Condition50%1/2
$method->{"data"} ||= {}
l!l
03
MC/DC0%0/1
$method->{"data"} ||= {}
$method->{"data"}
Truth table50%1/2
A
A= $method->{"data"}
Aresult
11
00
136 },
137 18 ▶ 'do' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
138 18 my $method = pop @_;
139 18 my $self = shift @_;
140 18 ▶ if ( scalar(@_) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 0) { } 14 4 18
141 14 return get_vvalue($method->{data}, $self);
142 } else {
143 4 ▶ my $value = (@_ == 1 ? $_[0] : [@_]);
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 4 0 4
144 4 set_vvalue($method->{data}, $self, $value);
145 }
146 },
147 ],
148 'rw' => [],
149 'p' => [
150 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
151 0 my $method = pop @_;
152 0 my $self = shift @_;
153 0 ▶ unless ( UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})) 0 0 0
154 0 croak "Method $method->{name} is protected";
155 }
156 },
157 ],
158 'pp' => [
159 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
160 0 my $method = pop @_;
161 0 my $self = shift @_;
162 0 ▶ unless ( (caller(1))[0] eq $method->{target_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((caller 1)[0] eq $method->{"target_class"}) 0 0 0
163 0 croak "Method $method->{name} is private";
164 }
165 },
166 ],
167 'pw' => [
168 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
169 0 my $method = pop @_;
170 0 my $self = shift @_;
171 0 ▶ unless ( @_ == 0 or UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 0 or UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})) 0 0 0
Condition0%0/3
@_ == 0 or UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 0 or UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})
@_ == 0 UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})
Truth table0%0/3
A or B
A= @_ == 0B= UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})
ABresult
1X1
011
000
172 0 croak "Method $method->{name} is write-protected";
173 }
174 },
175 ],
176 'ppw' => [
177 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
178 0 my $method = pop @_;
179 0 my $self = shift @_;
180 0 ▶ unless ( @_ == 0 or (caller(1))[0] eq $method->{target_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 0 or (caller 1)[0] eq $method->{"target_class"}) 0 0 0
Condition0%0/3
@_ == 0 or (caller 1)[0] eq $method->{"target_class"}
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 0 or (caller 1)[0] eq $method->{"target_class"}
@_ == 0 (caller 1)[0] eq $method->{"target_class"}
Truth table0%0/3
A or B
A= @_ == 0B= (caller 1)[0] eq $method->{"target_class"}
ABresult
1X1
011
000
181 0 croak "Method $method->{name} is write-private";
182 }
183 },
184 ],
185 'r' => [
186 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
187 0 my $method = pop @_;
188 0 my $self = shift @_;
189 0 @{ $method->{args} } = ();
190 },
191 ],
192 'ro' => [
193 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
194 0 my $method = pop @_;
195 0 my $self = shift @_;
196 0 ▶ unless ( @_ == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 0) 0 0 0
197 0 croak("Method $method->{name} is read-only");
198 }
199 },
200 ],
201 'wo' => [
202 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
203 0 my $method = pop @_;
204 0 my $self = shift @_;
205 0 ▶ if ( @_ == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 0 0 0
206 0 croak("Method $method->{name} is write-only");
207 }
208 },
209 ],
210 'return_original' => [
211 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
212 0 my $method = pop @_;
213 0 my $self = shift @_;
214 0 my $v_self = find_vself($method->{data}, $self);
215 $method->{scratch}{return_original} =
216 0 ▶ $v_self ? $method->{data}{$v_self} : ();
Branch: 1 branch
BranchTrueFalseTotal
$v_self ? : 0 0 0
217 },
218 0 ▶ '+post' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 0 my $method = pop @_;
220 0 $method->{result} = \{ $method->{scratch}{return_original} };
221 },
222 3 ],
223 );
224  
225 ########################################################################
226  
227 =head2 array - Overrideable Ref Accessor
228  
229 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
230  
231 =over 4
232  
233 =item *
234  
235 May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance.
236  
237 =item *
238  
239 The class value will be a reference to an array (or undef).
240  
241 =item *
242  
243 If called without any arguments, returns the current array-ref value (or undef).
244  
245  
246 =item *
247  
248 If called with a single non-ref argument, uses that argument as an index to retrieve from the referenced array, and returns that value (or undef).
249  
250 =item *
251  
252 If called with a single array ref argument, uses that list to return a slice of the referenced array.
253  
254 =item *
255  
256 If called with a list of argument pairs, each with a non-ref index and an associated value, stores the value at the given index in the referenced array. If the class value was previously undefined, a new array is autovivified. The current value in each position will be overwritten, and later arguments with the same index will override earlier ones. Returns the current array-ref value.
257  
258 =item *
259  
260 If called with a list of argument pairs, each with the first item being a reference to an array of up to two numbers, loops over each pair and uses those numbers to splice the value array.
261  
262 The first controlling number is the position at which the splice will begin. Zero will start before the first item in the list. Negative numbers count backwards from the end of the array.
263  
264 The second number is the number of items to be removed from the list. If it is omitted, or undefined, or zero, no items are removed. If it is a positive integer, that many items will be returned.
265  
266 If both numbers are omitted, or are both undefined, they default to containing the entire value array.
267  
268 If the second argument is undef, no values will be inserted; if it is a non-reference value, that one value will be inserted; if it is an array-ref, its values will be copied.
269  
270 The method returns the items that removed from the array, if any.
271  
272 =back
273  
274 Sample declaration and usage:
275
276 package MyClass;
277 use Class::MakeMethods::Composite::Inheritable (
278 array => 'bar',
279 );
280 ...
281
282 # Clear and set contents of list
283 print MyClass->bar([ 'Spume', 'Frost' ] );
284
285 # Set values by position
286 MyClass->bar(0 => 'Foozle', 1 => 'Bang!');
287
288 # Positions may be overwritten, and in any order
289 MyClass->bar(2 => 'And Mash', 1 => 'Blah!');
290
291 # Retrieve value by position
292 print MyClass->bar(1);
293
294 # Direct access to referenced array
295 print scalar @{ MyClass->bar() };
296  
297 There are also calling conventions for slice and splice operations:
298  
299 # Retrieve slice of values by position
300 print join(', ', MyClass->bar( undef, [0, 2] ) );
301
302 # Insert an item at position in the array
303 MyClass->bar([3], 'Potatoes' );
304
305 # Remove 1 item from position 3 in the array
306 MyClass->bar([3, 1], undef );
307
308 # Set a new value at position 2, and return the old value
309 print MyClass->bar([2, 1], 'Froth' );
310  
311 B<NOTE: THIS METHOD GENERATOR HAS NOT BEEN WRITTEN YET.>
312  
313 =cut
314  
315 3 ▶use vars qw( %ArrayFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
316  
317 0 ▶sub array {
Subroutine array: not called CC=1 SCAR=6.9
Pod documented
318 0 (shift)->_build_composite( \%ArrayFragments, @_ );
319 }
320  
321 %ArrayFragments = (
322 '' => [
323 0 ▶ '+init' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
324 0 my ($method) = @_;
325 0 ▶ $method->{hash_key} ||= $_->{name};
Condition0%0/3
$method->{"hash_key"} ||= $_->{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$method->{"hash_key"} ||= $_->{"name"}
$method->{"hash_key"} $_->{"name"}
Truth table0%0/3
A ||= B
A= $method->{"hash_key"}B= $_->{"name"}
ABresult
1X1
011
000
326 0 ▶ $method->{data} ||= {};
Condition0%0/2
$method->{"data"} ||= {}
l!l
00
MC/DC0%0/1
$method->{"data"} ||= {}
$method->{"data"}
Truth table0%0/2
A
A= $method->{"data"}
Aresult
11
00
327 },
328 0 ▶ 'do' => sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
329 0 my $method = pop @_;
330 0 my $self = shift @_;
331
332 0 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 0 0 0
elsif (scalar @_ == 1 and ref $_[0] eq 'ARRAY') { } 0 0 0
333 0 my $v_self = find_vself($method->{data}, $self);
334 0 ▶ my $value = $v_self ? $method->{data}{$v_self} : ();
Branch: 1 branch
BranchTrueFalseTotal
$v_self ? : 0 0 0
335 0 ▶ if ( $method->{auto_init} and ! $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"auto_init"} and not $value) 0 0 0
Condition0%0/3
$method->{"auto_init"} and not $value
!ll&&!rl&&r
000
MC/DC0%0/2
$method->{"auto_init"} and not $value
$method->{"auto_init"} not $value
Truth table0%0/3
A and not(B)
A= $method->{"auto_init"}B= not $value
ABresult
0X0
100
111
336 0 $value = $method->{data}{$self} = [];
337 }
338 0 ▶ ( ! $value ) ? () : wantarray ? @$value : $value;
Branch: 2 branches
BranchTrueFalseTotal
!$value ? : 0 0 0
wantarray ? : 0 0 0
339
340 ▶ } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) {
Condition0%0/3
scalar @_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "ARRAY"
scalar @_ == 1 ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
341 0 $method->{data}{$self} = [ @{ $_[0] } ];
342 0 ▶ wantarray ? @{ $method->{data}{$self} } : $method->{data}{$self}
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
343
344 } else {
345 0 ▶ if ( ! exists $method->{data}{$self} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $method->{"data"}{$self}) 0 0 0
346 0 my $v_self = find_vself($method->{data}, $self);
347 0 ▶ $method->{data}{$self} = [ $v_self ? @{$method->{data}{$v_self}} : () ];
Branch: 1 branch
BranchTrueFalseTotal
$v_self ? : 0 0 0
348 }
349 0 return array_splicer( $method->{data}{$self}, @_ );
350 }
351 },
352 3 ],
353 );
354  
355 ########################################################################
356  
357 =head2 hash - Overrideable Ref Accessor
358  
359 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
360  
361 =over 4
362  
363 =item *
364  
365 May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance.
366  
367 =item *
368  
369 The class value will be a reference to a hash (or undef).
370  
371 =item *
372  
373 If called without any arguments returns the contents of the hash in list context, or a hash reference in scalar context for the callee. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located.
374  
375 =item *
376  
377 If called with one non-ref argument, uses that argument as an index to retrieve from the referenced hash, and returns that value (or undef). If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located.
378  
379 =item *
380  
381 If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located.
382  
383 =item *
384  
385 If called with one hash-ref argument, sets the contents of the referenced hash to match that provided.
386  
387 =item *
388  
389 If called with a list of key-value pairs, stores the value under the given key in the hash associated with the callee, whether instance or class. If the callee did not previously have a hash-ref value associated with it, searches up instance to class, and from class to superclass, until a callee with a value is located, and copies that hash before making the assignments. The current value under each key will be overwritten, and later arguments with the same key will override earlier ones. Returns the contents of the hash in list context, or a hash reference in scalar context.
390  
391 =back
392  
393 Sample declaration and usage:
394  
395 package MyClass;
396 use Class::MakeMethods::Composite::Inheritable (
397 hash => 'baz',
398 );
399 ...
400
401 # Set values by key
402 MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!');
403
404 # Values may be overwritten, and in any order
405 MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
406
407 # Retrieve value by key
408 print MyClass->baz('foo');
409
410 # Retrive slice of values by position
411 print join(', ', MyClass->baz( ['foo', 'bar'] ) );
412
413 # Direct access to referenced hash
414 print keys %{ MyClass->baz() };
415
416 # Reset the hash contents to empty
417 @{ MyClass->baz() } = ();
418  
419 B<NOTE: THIS METHOD GENERATOR IS INCOMPLETE.>
420  
421 =cut
422  
423 3 ▶use vars qw( %HashFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
424  
425 1 ▶sub hash {
Subroutine hash: called CC=1 SCAR=0.0
Pod documented
426 1 (shift)->_build_composite( \%HashFragments, @_ );
427 }
428  
429 %HashFragments = (
430 '' => [
431 2 ▶ '+init' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
432 2 my ($method) = @_;
433 2 ▶ $method->{hash_key} ||= $_->{name};
Condition33%1/3
$method->{"hash_key"} ||= $_->{"name"}
l!l&&r!l&&!r
020
MC/DC0%0/2
$method->{"hash_key"} ||= $_->{"name"}
$method->{"hash_key"} $_->{"name"}
Truth table33%1/3
A ||= B
A= $method->{"hash_key"}B= $_->{"name"}
ABresult
1X1
011
000
434 2 ▶ $method->{data} ||= {};
Condition50%1/2
$method->{"data"} ||= {}
l!l
02
MC/DC0%0/1
$method->{"data"} ||= {}
$method->{"data"}
Truth table50%1/2
A
A= $method->{"data"}
Aresult
11
00
435 },
436 17 ▶ 'do' => sub {
Subroutine __ANON__: called CC=15 SCAR=30.6
437 17 my $method = pop @_;
438 17 my $self = shift @_;
439
440 17 ▶ if ( scalar(@_) == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 9 8 17
elsif (scalar @_ == 1) { } 4 4 8
elsif (scalar(@_) % 2) { } 0 4 4
441 9 my $value = get_vvalue($method->{data}, $self);
442 9 ▶ if ( $method->{auto_init} and ! $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"auto_init"} and not $value) 3 6 9
Condition66%2/3
$method->{"auto_init"} and not $value
!ll&&!rl&&r
063
MC/DC50%1/2
$method->{"auto_init"} and not $value
$method->{"auto_init"} not $value
Truth table66%2/3
A and not(B)
A= $method->{"auto_init"}B= not $value
ABresult
0X0
100
111
443 3 $value = set_vvalue( $method->{data}, $self, {} );
444 }
445 9 ▶ wantarray ? %$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 9 9
446 } elsif ( scalar(@_) == 1 ) {
447 4 ▶ if ( ref($_[0]) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 4 4
elsif (ref $_[0] eq 'ARRAY') { } 1 3 4
448 0 %{$method->{data}{$self}} = %{$_[0]};
449 } elsif ( ref($_[0]) eq 'ARRAY' ) {
450 1 ▶ my $v_self = find_vself($method->{data}, $self) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $v_self = find_vself($method->{"data"}, $self) or return 1 0 1
451 1 return @{ $method->{data}{$v_self} }{ @{$_[0]} }
452 } else {
453 3 ▶ my $v_self = find_vself($method->{data}, $self) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $v_self = find_vself($method->{"data"}, $self) or return 2 1 3
454 2 return $method->{data}{$v_self}{ $_[0] }
455 }
456  
457 } elsif ( scalar(@_) % 2 ) {
458 0 Carp::croak "Odd number of items in assigment to $method->{name}";
459 } else {
460 4 ▶ if ( ! exists $method->{data}{$self} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $method->{"data"}{$self}) 0 4 4
461 0 my $v_self = find_vself($method->{data}, $self);
462 0 ▶ $method->{data}{$self} = { $v_self ? %{ $method->{data}{$v_self} } : () };
Branch: 1 branch
BranchTrueFalseTotal
$v_self ? : 0 0 0
463 }
464 4 ▶ while ( scalar(@_) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 6 4 10
465 6 my $key = shift();
466 6 $method->{data}{$self}->{ $key } = shift();
467 }
468 0 ▶ wantarray ? %{$method->{data}{$self}} : $method->{data}{$self};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
469 }
470 },
471 3 ],
472 );
473  
474 ########################################################################
475  
476 =head2 hook - Overrideable array of subroutines
477  
478 A hook method is called from the outside as a normal method. However, internally, it contains an array of subroutine references, each of which are called in turn to produce the method's results.
479  
480 Subroutines may be added to the hook's array by calling it with a blessed subroutine reference, as shown below. Subroutines may be added on a class-wide basis or on an individual object.
481  
482 You might want to use this type of method to provide an easy way for callbacks to be registered.
483  
484 package MyClass;
485 use Class::MakeMethods::Composite::Inheritable ( 'hook' => 'init' );
486
487 MyClass->init( Class::MakeMethods::Composite::Inheritable->Hook( sub {
488 my $callee = shift;
489 warn "Init...";
490 } );
491
492 my $obj = MyClass->new;
493 $obj->init();
494  
495 =cut
496  
497 3 ▶use vars qw( %HookFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
498  
499 1 ▶sub hook {
Subroutine hook: called CC=1 SCAR=0.0
Pod documented
500 1 (shift)->_build_composite( \%HookFragments, @_ );
501 }
502  
503 %HookFragments = (
504 '' => [
505 1 ▶ '+init' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
506 1 my ($method) = @_;
507 1 ▶ $method->{data} ||= {};
Condition50%1/2
$method->{"data"} ||= {}
l!l
01
MC/DC0%0/1
$method->{"data"} ||= {}
$method->{"data"}
Truth table50%1/2
A
A= $method->{"data"}
Aresult
11
00
508 },
509 6 ▶ 'do' => sub {
Subroutine __ANON__: called CC=14 SCAR=29.8
510 6 my $method = pop @_;
511 6 my $self = shift @_;
512
513 6 ▶ if ( scalar(@_) and
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ and ref $_[0] eq 'Class::MakeMethods::Composite::Inheritable::Hook') { } 2 4 6
Condition66%2/3
scalar @_ and ref $_[0] eq "Class::MakeMethods::Composite::Inheritable::Hook"
!ll&&!rl&&r
402
MC/DC50%1/2
scalar @_ and ref $_[0] eq "Class::MakeMethods::Composite::Inheritable::Hook"
scalar @_ ref $_[0] eq "Class::MakeMethods::Composite::Inheritable::Hook"
Truth table66%2/3
A and B
A= scalar @_B= ref $_[0] eq "Class::MakeMethods::Composite::Inheritable::Hook"
ABresult
0X0
100
111
514 ref($_[0]) eq 'Class::MakeMethods::Composite::Inheritable::Hook' ) {
515 2 ▶ if ( ! exists $method->{data}{$self} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $method->{"data"}{$self}) 2 0 2
516 2 my $v_self = find_vself($method->{data}, $self);
517 2 ▶ $method->{data}{$self} = [ $v_self ? @{ $method->{data}{$v_self} } : () ];
Branch: 1 branch
BranchTrueFalseTotal
$v_self ? : 1 1 2
518 }
519 2 push @{ $method->{data}{$self} }, map $$_, @_;
520 } else {
521 4 my $v_self = find_vself($method->{data}, $self);
522 4 ▶ my $subs = $v_self ? $method->{data}{$v_self} : ();
Branch: 1 branch
BranchTrueFalseTotal
$v_self ? : 3 1 4
523 4 ▶ my @subs = ( ( ! $subs ) ? () : @$subs );
Branch: 1 branch
BranchTrueFalseTotal
!$subs ? : 1 3 4
524
525 4 ▶ if ( ! defined $method->{wantarray} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $method->{'wantarray'}) { } 0 4 4
elsif (not $method->{'wantarray'}) { } 4 0 4
526 0 foreach my $sub ( @subs ) {
527 0 &$sub( @{$method->{args}} );
528 }
529 } elsif ( ! $method->{wantarray} ) {
530 4 foreach my $sub ( @subs ) {
531 4 my $value = &$sub( @{$method->{args}} );
532 4 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 3 1 4
533 3 $method->{result} = \$value;
534 }
535 }
536 } else {
537 0 foreach my $sub ( @subs ) {
538 0 my @value = &$sub( @{$method->{args}} );
539 0 ▶ if ( scalar @value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @value) 0 0 0
540 0 push @{ $method->{result} }, @value;
541 }
542 }
543 }
544
545 }
546 6 return Class::MakeMethods::Composite->CurrentResults();
547 },
548 3 ],
549 );
550  
551 2 ▶sub Hook (&) {
Subroutine Hook: called CC=1 SCAR=0.0
Pod undocumented
552 2 my $package = shift;
553 2 my $sub = shift;
554 2 bless \$sub, 'Class::MakeMethods::Composite::Inheritable::Hook';
555 }
556  
557 ########################################################################
558  
559 =head2 object - Overrideable Ref Accessor
560  
561 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
562  
563 =over 4
564  
565 =item *
566  
567 May be called as a class method, or on any instance or subclass, Must be called on a hash-based instance.
568  
569 =item *
570  
571 The class value will be a reference to an object (or undef).
572  
573 =item *
574  
575 If called without any arguments returns the current value for the callee. If the callee has not had a value defined for this method, searches up from instance to class, and from class to superclass, until a callee with a value is located.
576  
577 =item *
578  
579 If called with an argument, stores that as the value associated with the callee, whether instance or class, and returns it,
580  
581 =back
582  
583 Sample declaration and usage:
584  
585 package MyClass;
586 use Class::MakeMethods::Composite::Inheritable (
587 object => 'foo',
588 );
589 ...
590
591 # Store value
592 MyClass->foo( Foozle->new() );
593
594 # Retrieve value
595 print MyClass->foo;
596  
597 B<NOTE: THIS METHOD GENERATOR HAS NOT BEEN WRITTEN YET.>
598  
599 =cut
600  
601 0 ▶sub object { }
Subroutine object: not called CC=1 SCAR=0.0
Pod documented
602  
603 ########################################################################
604  
605 =head1 SEE ALSO
606  
607 See L<Class::MakeMethods> for general information about this distribution.
608  
609 See L<Class::MakeMethods::Composite> for more about this family of subclasses.
610  
611 =cut
612  
613 3 1;
« blib/lib/Class/MakeMethods/Composite/Hash.pm ↑ Summary blib/lib/Class/MakeMethods/Template.pm »