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

filter: Statementstmt 43.4% 69 / 159 Branchbran 23.3% 14 / 60 Conditioncond 16.6% 9 / 54 MC/DCmcdc 2.8% 1 / 35 Subroutinesub 51.5% 17 / 33 Podpod 100% 5 / 5 totaltotal 33.2% 115 / 346 CC 50 SCAR 36.2
CC
50
Cov
34%
__ANON__
132.0
__ANON__
90.0
__ANON__
72.0
CRAP 37.5
« blib/lib/Class/MakeMethods/Utility/DiskCache.pm ↑ Summary blib/lib/Class/MakeMethods/Composite/Inheritable.pm »
1 =head1 NAME
2  
3 Class::MakeMethods::Composite::Hash - Composite hash methods
4  
5 =head1 SYNOPSIS
6  
7 package MyObject;
8 use Class::MakeMethods::Composite::Hash (
9 new => 'new',
10 scalar => [ 'foo', 'bar' ],
11 array => 'my_list',
12 hash => 'my_index',
13 );
14 ...
15
16 my $obj = MyObject->new( foo => 'Foozle' );
17 print $obj->foo();
18
19 $obj->bar('Barbados');
20 print $obj->bar();
21
22 $obj->my_list(0 => 'Foozle', 1 => 'Bang!');
23 print $obj->my_list(1);
24
25 $obj->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
26 print $obj->my_index('foo');
27  
28 =head1 DESCRIPTION
29  
30 The Composite::Hash suclass of MakeMethods provides a basic constructor and accessors for blessed-hash object instances.
31  
32 =head2 Class::MakeMethods Calling Interface
33  
34 When you C<use> this package, the method declarations you provide
35 as arguments cause subroutines to be generated and installed in
36 your module.
37  
38 You can also omit the arguments to C<use> and instead make methods
39 at runtime by passing the declarations to a subsequent call to
40 C<make()>.
41  
42 You may include any number of declarations in each call to C<use>
43 or C<make()>. If methods with the same name already exist, earlier
44 calls to C<use> or C<make()> win over later ones, but within each
45 call, later declarations superceed earlier ones.
46  
47 You can install methods in a different package by passing C<-TargetClass =E<gt> I<package>> as your first arguments to C<use> or C<make>.
48  
49 See L<Class::MakeMethods> for more details.
50  
51 =head2 Class::MakeMethods::Basic Declaration Syntax
52  
53 The following types of Basic declarations are supported:
54  
55 =over 4
56  
57 =item *
58  
59 I<generator_type> => "I<method_name>"
60  
61 =item *
62  
63 I<generator_type> => "I<name_1> I<name_2>..."
64  
65 =item *
66  
67 I<generator_type> => [ "I<name_1>", "I<name_2>", ...]
68  
69 =back
70  
71 See the "METHOD GENERATOR TYPES" section below for a list of the supported values of I<generator_type>.
72  
73 For each method name you provide, a subroutine of the indicated
74 type will be generated and installed under that name in your module.
75  
76 Method names should start with a letter, followed by zero or more
77 letters, numbers, or underscores.
78  
79 =head2 Class::MakeMethods::Composite Declaration Syntax
80  
81 The Composite syntax also provides several ways to optionally
82 associate a hash of additional parameters with a given method
83 name.
84  
85 =over 4
86  
87 =item *
88  
89 I<generator_type> => [ "I<name_1>" => { I<param>=>I<value>... }, ... ]
90  
91 A hash of parameters to use just for this method name.
92  
93 (Note: to prevent confusion with self-contained definition hashes,
94 described below, parameter hashes following a method name must not
95 contain the key 'name'.)
96  
97 =item *
98  
99 I<generator_type> => [ [ "I<name_1>", "I<name_2>", ... ] => { I<param>=>I<value>... } ]
100  
101 Each of these method names gets a copy of the same set of parameters.
102  
103 =item *
104  
105 I<generator_type> => [ { "name"=>"I<name_1>", I<param>=>I<value>... }, ... ]
106  
107 By including the reserved parameter C<name>, you create a self-contained declaration with that name and any associated hash values.
108  
109 =back
110  
111 Basic declarations, as described above, are given an empty parameter hash.
112  
113 =cut
114  
115 package Class::MakeMethods::Composite::Hash;
116  
117 5 $VERSION = 1.000;
118 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
119 5 ▶use Class::MakeMethods::Composite '-isasubclass';
Subroutine BEGIN: called CC=1 SCAR=0.0
120 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
121  
122 ########################################################################
123  
124 =head1 METHOD GENERATOR TYPES
125  
126 =head2 new - Constructor
127  
128 For each method name passed, returns a subroutine with the following characteristics:
129  
130 =over 4
131  
132 =item *
133  
134 Has a reference to a sample item to copy. This defaults to a reference to an empty hash, but you may override this with the C<'defaults' =E<gt> I<hash_ref>> method parameter.
135  
136 =item *
137  
138 If called as a class method, makes a new hash and blesses it into that class.
139  
140 =item *
141  
142 If called on a hash-based instance, makes a copy of it and blesses the copy into the same class as the original instance.
143  
144 =item *
145  
146 If passed a list of key-value pairs, appends them to the new hash. These arguments override any copied values, and later arguments with the same name will override earlier ones.
147  
148 =item *
149  
150 Returns the new instance.
151  
152 =back
153  
154 Sample declaration and usage:
155  
156 package MyObject;
157 use Class::MakeMethods::Composite::Hash (
158 new => 'new',
159 );
160 ...
161
162 # Bare constructor
163 my $empty = MyObject->new();
164
165 # Constructor with initial values
166 my $obj = MyObject->new( foo => 'Foozle', bar => 'Barbados' );
167
168 # Copy with overriding value
169 my $copy = $obj->new( bar => 'Bob' );
170  
171 =cut
172  
173 =head2 new --with_values - Constructor
174  
175 For each method name passed, returns a subroutine with the following characteristics:
176  
177 =over 4
178  
179 =item *
180  
181 May be called as a class method, or (equivalently) on any existing object of that class.
182  
183 =item *
184  
185 Creates a hash, blesses it into the class, and returns the new instance.
186  
187 =item *
188  
189 If no arguments are provided, the returned hash will be empty. If passed a single hash-ref argument, copies its contents into the new hash. If called with multiple arguments, treats them as key-value pairs, and copies them into the new hash. (Note that this is a "shallow" copy, not a "deep" clone.)
190  
191 =back
192  
193 =cut
194  
195 5 ▶use vars qw( %ConstructorFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
198 8 (shift)->_build_composite( \%ConstructorFragments, @_ );
199 }
200  
201 %ConstructorFragments = (
202 '' => [
203 8 ▶ '+init' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
204 8 my $method = pop @_;
205 8 ▶ $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass};
Condition33%1/3
$method->{"target_class"} ||= $Class::MethodMaker::CONTEXT{"TargetClass"}
l!l&&r!l&&!r
008
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
206 8 ▶ $method->{defaults} ||= {};
Condition50%1/2
$method->{"defaults"} ||= {}
l!l
08
MC/DC0%0/1
$method->{"defaults"} ||= {}
$method->{"defaults"}
Truth table50%1/2
A
A= $method->{"defaults"}
Aresult
11
00
207 },
208 9 ▶ 'do' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.5
209 9 my $method = pop @_;
210 9 my $self = shift @_;
211 my $obj = ref($self) ? bless( { %$self }, ref $self )
212 9 ▶ : bless( { %{$method->{defaults}} }, $self );
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 1 8 9
213 0 ▶ @_ = %{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and ref $_[0] eq "HASH" 0 9 9
Condition33%1/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
900
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
214 if ( scalar @_ == 1 and ref $_[0] eq 'HASH' );
215 9 ▶ while ( scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 16 9 25
216 16 my $method = shift @_;
217 16 my $value = shift @_;
218 16 $obj->$method( $value );
219 }
220 9 $obj;
221 },
222 ],
223 'with_values' => [
224 2 ▶ 'do' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.3
225 2 my $method = pop @_;
226 2 my $self = shift @_;
227 2 ▶ @_ = %{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 1 and ref $_[0] eq "HASH" 1 1 2
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
101
MC/DC50%1/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
228 if ( scalar @_ == 1 and ref $_[0] eq 'HASH' );
229 2 ▶ bless( { @_ }, ref($self) || $self );
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
230 }
231 5 ],
232 );
233  
234 ########################################################################
235  
236 =head2 scalar - Instance Accessor
237  
238 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
239  
240 =over 4
241  
242 =item *
243  
244 Must be called on a hash-based instance.
245  
246 =item *
247  
248 Has a specific hash key to use to access the related value for each instance.
249 This defaults to the method name, but you may override this with the C<'hash_key' => I<string>> method parameter.
250  
251 =item *
252  
253 If called without any arguments returns the current value.
254  
255 =item *
256  
257 If called with an argument, stores that as the value, and returns it.
258  
259 =item *
260  
261 If called with multiple arguments, stores a reference to a new array with those arguments as contents, and returns that array reference.
262  
263 =back
264  
265 Sample declaration and usage:
266  
267 package MyObject;
268 use Class::MakeMethods::Composite::Hash (
269 scalar => 'foo',
270 );
271 ...
272
273 # Store value
274 $obj->foo('Foozle');
275
276 # Retrieve value
277 print $obj->foo;
278  
279 =cut
280  
281 5 ▶use vars qw( %ScalarFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
282  
283 16 ▶sub scalar {
Subroutine scalar: called CC=1 SCAR=0.0
Pod documented
284 16 (shift)->_build_composite( \%ScalarFragments, @_ );
285 }
286  
287 %ScalarFragments = (
288 '' => [
289 37 ▶ '+init' => sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
290 37 my ($method) = @_;
291 37 ▶ $method->{hash_key} ||= $method->{name};
Condition66%2/3
$method->{"hash_key"} ||= $method->{"name"}
l!l&&r!l&&!r
16210
MC/DC0%0/2
$method->{"hash_key"} ||= $method->{"name"}
$method->{"hash_key"} $method->{"name"}
Truth table66%2/3
A ||= B
A= $method->{"hash_key"}B= $method->{"name"}
ABresult
1X1
011
000
292 37 ▶ $method->{target_class} ||= $Class::MethodMaker::CONTEXT{TargetClass};
Condition33%1/3
$method->{"target_class"} ||= $Class::MethodMaker::CONTEXT{"TargetClass"}
l!l&&r!l&&!r
0037
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
293 },
294 53 ▶ 'do' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
295 53 my $method = pop @_;
296 53 my $self = shift @_;
297 53 ▶ if ( scalar(@_) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 0) { } 29 24 53
elsif (scalar @_ == 1) { } 24 0 24
298 29 $self->{$method->{hash_key}};
299 } elsif ( scalar(@_) == 1 ) {
300 24 $self->{$method->{hash_key}} = shift;
301 } else {
302 0 $self->{$method->{hash_key}} = [@_];
303 }
304 },
305 ],
306 'rw' => [],
307 'p' => [
308 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
309 0 my $method = pop @_;
310 0 my $self = shift @_;
311 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
312 0 croak "Method $method->{name} is protected";
313 }
314 },
315 ],
316 'pp' => [
317 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
318 0 my $method = pop @_;
319 0 my $self = shift @_;
320 0 ▶ unless ( (caller(1))[0] eq $method->{target_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ((caller 1)[0] eq $method->{"target_class"}) 0 0 0
321 0 croak "Method $method->{name} is private";
322 }
323 },
324 ],
325 'pw' => [
326 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
327 0 my $method = pop @_;
328 0 my $self = shift @_;
329 0 my $args = \@_;
330 0 ▶ unless ( @$args == 0 or UNIVERSAL::isa((caller(1))[0], $method->{target_class}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 0 or UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})) 0 0 0
Condition0%0/3
@$args == 0 or UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})
l!l&&r!l&&!r
000
MC/DC0%0/2
@$args == 0 or UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})
@$args == 0 UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})
Truth table0%0/3
A or B
A= @$args == 0B= UNIVERSAL::isa((caller 1)[0], $method->{"target_class"})
ABresult
1X1
011
000
331 0 croak "Method $method->{name} is write-protected";
332 }
333 },
334 ],
335 'ppw' => [
336 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
337 0 my $method = pop @_;
338 0 my $self = shift @_;
339 0 my $args = \@_;
340 0 ▶ unless ( @$args == 0 or (caller(1))[0] eq $method->{target_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 0 or (caller 1)[0] eq $method->{"target_class"}) 0 0 0
Condition0%0/3
@$args == 0 or (caller 1)[0] eq $method->{"target_class"}
l!l&&r!l&&!r
000
MC/DC0%0/2
@$args == 0 or (caller 1)[0] eq $method->{"target_class"}
@$args == 0 (caller 1)[0] eq $method->{"target_class"}
Truth table0%0/3
A or B
A= @$args == 0B= (caller 1)[0] eq $method->{"target_class"}
ABresult
1X1
011
000
341 0 croak "Method $method->{name} is write-private";
342 }
343 },
344 ],
345 'r' => [
346 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
347 0 my $method = pop @_;
348 0 my $self = shift @_;
349 0 my $args = \@_;
350 0 @$args = ();
351 },
352 ],
353 'ro' => [
354 2 ▶ '+pre' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
355 2 my $method = pop @_;
356 2 my $self = shift @_;
357 2 my $args = \@_;
358 2 ▶ unless ( @$args == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$args == 0) 1 1 2
359 1 croak("Method $method->{name} is read-only");
360 }
361 },
362 ],
363 'wo' => [
364 2 ▶ '+pre' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
365 2 my $method = pop @_;
366 2 my $self = shift @_;
367 2 my $args = \@_;
368 2 ▶ if ( @$args == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args == 0) 1 1 2
369 1 croak("Method $method->{name} is write-only");
370 }
371 },
372 ],
373 'return_original' => [
374 0 ▶ '+pre' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
375 0 my $method = pop @_;
376 0 my $self = shift @_;
377 0 my $args = \@_;
378 0 $method->{scratch}{return_original} = $self->{$method->{hash_key}};
379 },
380 0 ▶ '+post' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
381 0 my $method = pop @_;
382 0 my $self = shift @_;
383 0 my $args = \@_;
384 0 ${ $method->{result} } = $method->{scratch}{return_original};
385 },
386 5 ],
387 );
388  
389 ########################################################################
390  
391 =head2 array - Instance Ref Accessor
392  
393 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
394  
395 =over 4
396  
397 =item *
398  
399 Must be called on a hash-based instance.
400  
401 =item *
402  
403 Has a specific hash key to use to access the related value for each instance.
404 This defaults to the method name, but you may override this with the C<'hash_key' => I<string>> method parameter.
405  
406 =item *
407  
408 The value for each instance will be a reference to an array (or undef).
409  
410 =item *
411  
412 If called without any arguments, returns the current array-ref value (or undef).
413  
414  
415 =item *
416  
417 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).
418  
419 =item *
420  
421 If called with a single array ref argument, uses that list to return a slice of the referenced array.
422  
423 =item *
424  
425 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 instance's 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.
426  
427 =item *
428  
429 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.
430  
431 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.
432  
433 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.
434  
435 If both numbers are omitted, or are both undefined, they default to containing the entire value array.
436  
437 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.
438  
439 The method returns the items that removed from the array, if any.
440  
441 =back
442  
443 Sample declaration and usage:
444
445 package MyObject;
446 use Class::MakeMethods::Composite::Hash (
447 array => 'bar',
448 );
449 ...
450
451 # Clear and set contents of list
452 print $obj->bar([ 'Spume', 'Frost' ] );
453
454 # Set values by position
455 $obj->bar(0 => 'Foozle', 1 => 'Bang!');
456
457 # Positions may be overwritten, and in any order
458 $obj->bar(2 => 'And Mash', 1 => 'Blah!');
459
460 # Retrieve value by position
461 print $obj->bar(1);
462
463 # Direct access to referenced array
464 print scalar @{ $obj->bar() };
465  
466 There are also calling conventions for slice and splice operations:
467  
468 # Retrieve slice of values by position
469 print join(', ', $obj->bar( undef, [0, 2] ) );
470
471 # Insert an item at position in the array
472 $obj->bar([3], 'Potatoes' );
473
474 # Remove 1 item from position 3 in the array
475 $obj->bar([3, 1], undef );
476
477 # Set a new value at position 2, and return the old value
478 print $obj->bar([2, 1], 'Froth' );
479  
480 =cut
481  
482 5 ▶use vars qw( %ArrayFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
483  
484 0 ▶sub array {
Subroutine array: not called CC=1 SCAR=6.9
Pod documented
485 0 (shift)->_build_composite( \%ArrayFragments, @_ );
486 }
487  
488 %ArrayFragments = (
489 '' => [
490 0 ▶ '+init' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
491 0 my ($method) = @_;
492 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
493 },
494 0 ▶ 'do' => sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
495 0 my $method = pop @_;
496 0 my $self = shift @_;
497 0 my $args = \@_;
498 0 ▶ if ( scalar(@$args) == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$args == 0) { } 0 0 0
elsif (scalar @_ == 1 and ref $_[0] eq 'ARRAY') { } 0 0 0
499 0 ▶ if ( $method->{auto_init} and
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}) 0 0 0
Condition0%0/3
$method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}
$method->{"auto_init"} not defined $$self{$method->{"hash_key"}}
Truth table0%0/3
A and not(B)
A= $method->{"auto_init"}B= not defined $$self{$method->{"hash_key"}}
ABresult
0X0
100
111
500 ! defined $self->{$method->{hash_key}} ) {
501 0 $self->{$method->{hash_key}} = [];
502 }
503 0 ▶ wantarray ? @{ $self->{$method->{hash_key}} } : $self->{$method->{hash_key}};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
504 ▶ } 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
505 0 $self->{$method->{hash_key}} = [ @{ $_[0] } ];
506 0 ▶ wantarray ? @{ $self->{$method->{hash_key}} } : $self->{$method->{hash_key}};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
507 } else {
508 0 ▶ $self->{$method->{hash_key}} ||= [];
Condition0%0/2
$$self{$method->{"hash_key"}} ||= []
l!l
00
MC/DC0%0/1
$$self{$method->{"hash_key"}} ||= []
$$self{$method->{"hash_key"}}
Truth table0%0/2
A
A= $$self{$method->{"hash_key"}}
Aresult
11
00
509 0 array_splicer( $self->{$method->{hash_key}}, @$args );
510 }
511 },
512 5 ],
513 );
514  
515 ########################################################################
516  
517 =head2 hash - Instance Ref Accessor
518  
519 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
520  
521 =over 4
522  
523 =item *
524  
525 Must be called on a hash-based instance.
526  
527 =item *
528  
529 Has a specific hash key to use to access the related value for each instance.
530 This defaults to the method name, but you may override this with the C<'hash_key' => I<string>> method parameter.
531  
532 =item *
533  
534 The value for each instance will be a reference to a hash (or undef).
535  
536 =item *
537  
538 If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef).
539  
540 =item *
541  
542 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).
543  
544 =item *
545  
546 If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash.
547  
548 =item *
549  
550 If called with one hash-ref argument, sets the contents of the referenced hash to match that provided.
551  
552 =item *
553  
554 If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the instance's value was previously undefined, a new hash is autovivified. 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.
555  
556 =back
557  
558 Sample declaration and usage:
559  
560 package MyObject;
561 use Class::MakeMethods::Composite::Hash (
562 hash => 'baz',
563 );
564 ...
565
566 # Set values by key
567 $obj->baz('foo' => 'Foozle', 'bar' => 'Bang!');
568
569 # Values may be overwritten, and in any order
570 $obj->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
571
572 # Retrieve value by key
573 print $obj->baz('foo');
574
575 # Retrive slice of values by position
576 print join(', ', $obj->baz( ['foo', 'bar'] ) );
577
578 # Direct access to referenced hash
579 print keys %{ $obj->baz() };
580
581 # Reset the hash contents to empty
582 @{ $obj->baz() } = ();
583  
584 =cut
585  
586 5 ▶use vars qw( %HashFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
587  
588 0 ▶sub hash {
Subroutine hash: not called CC=1 SCAR=6.9
Pod documented
589 0 (shift)->_build_composite( \%HashFragments, @_ );
590 }
591  
592 %HashFragments = (
593 '' => [
594 0 ▶ '+init' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
595 0 my ($method) = @_;
596 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
597 },
598 0 ▶ 'do' => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
599 0 my $method = pop @_;
600 0 my $self = shift @_;
601 0 my $args = \@_;
602 0 ▶ if ( scalar(@$args) == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @$args == 0) { } 0 0 0
elsif (scalar @$args == 1) { } 0 0 0
elsif (scalar(@$args) % 2) { } 0 0 0
603 0 ▶ if ( $method->{auto_init} and ! defined $self->{$method->{hash_key}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}) 0 0 0
Condition0%0/3
$method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}
$method->{"auto_init"} not defined $$self{$method->{"hash_key"}}
Truth table0%0/3
A and not(B)
A= $method->{"auto_init"}B= not defined $$self{$method->{"hash_key"}}
ABresult
0X0
100
111
604 0 $self->{$method->{hash_key}} = {};
605 }
606 0 ▶ wantarray ? %{ $self->{$method->{hash_key}} } : $self->{$method->{hash_key}};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
607 } elsif ( scalar(@$args) == 1 ) {
608 0 ▶ if ( ref($_[0]) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
elsif (ref $_[0] eq 'ARRAY') { } 0 0 0
609 0 %{$self->{$method->{hash_key}}} = %{$_[0]};
610 } elsif ( ref($_[0]) eq 'ARRAY' ) {
611 0 return @{$self->{$method->{hash_key}}}{ @{$_[0]} }
612 } else {
613 0 return $self->{$method->{hash_key}}->{ $_[0] }
614 }
615 } elsif ( scalar(@$args) % 2 ) {
616 0 croak "Odd number of items in assigment to $method->{name}";
617 } else {
618 0 ▶ while ( scalar(@$args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$args) 0 0 0
619 0 my $key = shift @$args;
620 0 $self->{$method->{hash_key}}->{ $key} = shift @$args;
621 }
622 0 ▶ wantarray ? %{ $self->{$method->{hash_key}} } : $self->{$method->{hash_key}};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
623 }
624 },
625 5 ],
626 );
627  
628 ########################################################################
629  
630 =head2 object - Instance Ref Accessor
631  
632 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
633  
634 =over 4
635  
636 =item *
637  
638 Must be called on a hash-based instance.
639  
640 =item *
641  
642 Has a specific hash key to use to access the related value for each instance.
643 This defaults to the method name, but you may override this with the C<'hash_key' => I<string>> method parameter.
644  
645 =item *
646  
647 The value for each instance will be a reference to an object (or undef).
648  
649 =item *
650  
651 If called without any arguments returns the current value.
652  
653 =item *
654  
655 If called with an argument, stores that as the value, and returns it,
656  
657 =back
658  
659 Sample declaration and usage:
660  
661 package MyObject;
662 use Class::MakeMethods::Composite::Hash (
663 object => 'foo',
664 );
665 ...
666
667 # Store value
668 $obj->foo( Foozle->new() );
669
670 # Retrieve value
671 print $obj->foo;
672  
673 =cut
674  
675 5 ▶use vars qw( %ObjectFragments );
Subroutine BEGIN: called CC=1 SCAR=0.0
676  
677 0 ▶sub object {
Subroutine object: not called CC=1 SCAR=6.9
Pod documented
678 0 (shift)->_build_composite( \%ObjectFragments, @_ );
679 }
680  
681 %ObjectFragments = (
682 '' => [
683 0 ▶ '+init' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
684 0 my ($method) = @_;
685 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
686 },
687 0 ▶ 'do' => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
688 0 my $method = pop @_;
689 0 my $self = shift;
690 0 ▶ if ( scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) { } 0 0 0
691 0 my $value = shift;
692 0 ▶ if ( $method->{class} and ! UNIVERSAL::isa( $value, $method->{class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"class"} and not UNIVERSAL::isa($value, $method->{"class"})) 0 0 0
Condition0%0/3
$method->{"class"} and not UNIVERSAL::isa($value, $method->{"class"})
!ll&&!rl&&r
000
MC/DC0%0/2
$method->{"class"} and not UNIVERSAL::isa($value, $method->{"class"})
$method->{"class"} not UNIVERSAL::isa($value, $method->{"class"})
Truth table0%0/3
A and not(B)
A= $method->{"class"}B= not UNIVERSAL::isa($value, $method->{"class"})
ABresult
0X0
100
111
693 0 croak "Wrong argument type ('$value') in assigment to $method->{name}";
694 }
695 0 $self->{$method->{hash_key}} = $value;
696 } else {
697 0 ▶ if ( $method->{auto_init} and ! defined $self->{$method->{hash_key}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}) 0 0 0
Condition0%0/3
$method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$method->{"auto_init"} and not defined $$self{$method->{"hash_key"}}
$method->{"auto_init"} not defined $$self{$method->{"hash_key"}}
Truth table0%0/3
A and not(B)
A= $method->{"auto_init"}B= not defined $$self{$method->{"hash_key"}}
ABresult
0X0
100
111
698 my $class = $method->{class}
699 0 ▶ or die "Can't auto_init without a class";
Branch: 1 branch
BranchTrueFalseTotal
my $class = $method->{"class"} or die "Can't auto_init without a class" 0 0 0
700 0 ▶ my $new_method = $method->{new_method} || 'new';
Condition0%0/2
$method->{'new_method'} || 'new'
l!l
00
MC/DC0%0/1
$method->{'new_method'} || 'new'
$method->{'new_method'}
Truth table0%0/2
A
A= $method->{'new_method'}
Aresult
11
00
701 0 $self->{$method->{hash_key}} = $class->$new_method();
702 }
703 0 $self->{$method->{hash_key}};
704 }
705 },
706 5 ],
707 );
708  
709 ########################################################################
710  
711 =head1 SEE ALSO
712  
713 See L<Class::MakeMethods> for general information about this distribution.
714  
715 See L<Class::MakeMethods::Composite> for more about this family of subclasses.
716  
717 =cut
718  
719 5 1;
« blib/lib/Class/MakeMethods/Utility/DiskCache.pm ↑ Summary blib/lib/Class/MakeMethods/Composite/Inheritable.pm »