blib/lib/Class/MakeMethods/Standard/Global.pm untested

filter: Statementstmt 0.0% 0 / 75 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 162 CC 21 SCAR 41.2
CC
21
Cov
0%
hash
110.0
array
56.0
object
30.0
CRAP 61.5
1 =head1 NAME
2  
3 Class::MakeMethods::Standard::Global - Global data
4  
5 =head1 SYNOPSIS
6  
7 package MyClass;
8 use Class::MakeMethods::Standard::Global (
9 scalar => [ 'foo' ],
10 array => [ 'my_list' ],
11 hash => [ 'my_index' ],
12 );
13 ...
14
15 MyClass->foo( 'Foozle' );
16 print MyClass->foo();
17  
18 print MyClass->new(...)->foo(); # same value for any instance
19 print MySubclass->foo(); # ... and for any subclass
20
21 MyClass->my_list(0 => 'Foozle', 1 => 'Bang!');
22 print MyClass->my_list(1);
23
24 MyClass->my_index('broccoli' => 'Blah!', 'foo' => 'Fiddle');
25 print MyClass->my_index('foo');
26  
27  
28 =head1 DESCRIPTION
29  
30 The Standard::Global suclass of MakeMethods provides basic accessors for shared data.
31  
32 =head2 Calling Conventions
33  
34 When you C<use> this package, the method names you provide
35 as arguments cause subroutines to be generated and installed in
36 your module.
37  
38 See L<Class::MakeMethods::Standard/"Calling Conventions"> for more information.
39  
40 =head2 Declaration Syntax
41  
42 To declare methods, pass in pairs of a method-type name followed
43 by one or more method names.
44  
45 Valid method-type names for this package are listed in L<"METHOD
46 GENERATOR TYPES">.
47  
48 See L<Class::MakeMethods::Standard/"Declaration Syntax"> and L<Class::MakeMethods::Standard/"Parameter Syntax"> for more information.
49  
50 =cut
51  
52 package Class::MakeMethods::Standard::Global;
53  
54 $VERSION = 1.000;
55 use strict;
56 use Class::MakeMethods::Standard '-isasubclass';
57 use Class::MakeMethods::Utility::ArraySplicer 'array_splicer';
58  
59 ########################################################################
60  
61 =head1 METHOD GENERATOR TYPES
62  
63 =head2 scalar - Global Accessor
64  
65 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
66  
67 =over 4
68  
69 =item *
70  
71 May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on.
72  
73 =item *
74  
75 If called without any arguments returns the current value.
76  
77 =item *
78  
79 If called with an argument, stores that as the value, and returns it,
80  
81 =back
82  
83 Sample declaration and usage:
84  
85 package MyClass;
86 use Class::MakeMethods::Standard::Global (
87 scalar => 'foo',
88 );
89 ...
90
91 # Store value
92 MyClass->foo('Foozle');
93
94 # Retrieve value
95 print MyClass->foo;
96  
97 =cut
98  
99 sub scalar {
100 map {
101 my $name = $_->{name};
102 my $data;
103 $name => sub {
104 my $self = shift;
105 if ( scalar(@_) == 0 ) {
106 $data;
107 } else {
108 $data = shift;
109 }
110 }
111 } (shift)->_get_declarations(@_)
112 }
113  
114 ########################################################################
115  
116 =head2 array - Global Ref Accessor
117  
118 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
119  
120 =over 4
121  
122 =item *
123  
124 May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on.
125  
126 =item *
127  
128 The global value will be a reference to an array (or undef).
129  
130 =item *
131  
132 If called without any arguments, returns the current array-ref value (or undef).
133  
134  
135 =item *
136  
137 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).
138  
139 =item *
140  
141 If called with a single array ref argument, uses that list to return a slice of the referenced array.
142  
143 =item *
144  
145 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 global 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.
146  
147 =item *
148  
149 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.
150  
151 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.
152  
153 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.
154  
155 If both numbers are omitted, or are both undefined, they default to containing the entire value array.
156  
157 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.
158  
159 The method returns the items that removed from the array, if any.
160  
161 =back
162  
163 Sample declaration and usage:
164
165 package MyClass;
166 use Class::MakeMethods::Standard::Global (
167 array => 'bar',
168 );
169 ...
170
171 # Clear and set contents of list
172 print MyClass->bar([ 'Spume', 'Frost' ] );
173
174 # Set values by position
175 MyClass->bar(0 => 'Foozle', 1 => 'Bang!');
176
177 # Positions may be overwritten, and in any order
178 MyClass->bar(2 => 'And Mash', 1 => 'Blah!');
179
180 # Retrieve value by position
181 print MyClass->bar(1);
182
183 # Direct access to referenced array
184 print scalar @{ MyClass->bar() };
185  
186 There are also calling conventions for slice and splice operations:
187  
188 # Retrieve slice of values by position
189 print join(', ', MyClass->bar( undef, [0, 2] ) );
190
191 # Insert an item at position in the array
192 MyClass->bar([3], 'Potatoes' );
193
194 # Remove 1 item from position 3 in the array
195 MyClass->bar([3, 1], undef );
196
197 # Set a new value at position 2, and return the old value
198 print MyClass->bar([2, 1], 'Froth' );
199  
200 =cut
201  
202 sub array {
203 map {
204 my $name = $_->{name};
205 my $data;
206 my $init = $_->{auto_init};
207 $name => sub {
208 my $self = shift;
209 if ( scalar(@_) == 0 ) {
210 if ( $init and ! defined $data ) {
211 $data = [];
212 }
213 ! $data ? () : wantarray ? @$data : $data;
214 } elsif ( scalar(@_) == 1 and ref $_[0] eq 'ARRAY' ) {
215 $data = [ @{ $_[0] } ];
216 wantarray ? @$data : $data;
217 } else {
218 $data ||= [];
219 return array_splicer( $data, @_ );
220 }
221 }
222 } (shift)->_get_declarations(@_)
223 }
224  
225 ########################################################################
226  
227 =head2 hash - Global 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, and behaves identically regardless of what it was called on.
236  
237 =item *
238  
239 The global value will be a reference to a hash (or undef).
240  
241 =item *
242  
243 If called without any arguments, returns the contents of the hash in list context, or a hash reference in scalar context (or undef).
244  
245 =item *
246  
247 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).
248  
249 =item *
250  
251 If called with one array-ref argument, uses the contents of that array to retrieve a slice of the referenced hash.
252  
253 =item *
254  
255 If called with one hash-ref argument, sets the contents of the referenced hash to match that provided.
256  
257 =item *
258  
259 If called with a list of key-value pairs, stores the value under the given key in the referenced hash. If the global 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.
260  
261 =back
262  
263 Sample declaration and usage:
264  
265 package MyClass;
266 use Class::MakeMethods::Standard::Global (
267 hash => 'baz',
268 );
269 ...
270
271 # Set values by key
272 MyClass->baz('foo' => 'Foozle', 'bar' => 'Bang!');
273
274 # Values may be overwritten, and in any order
275 MyClass->baz('broccoli' => 'Blah!', 'foo' => 'Fiddle');
276
277 # Retrieve value by key
278 print MyClass->baz('foo');
279
280 # Retrive slice of values by position
281 print join(', ', MyClass->baz( ['foo', 'bar'] ) );
282
283 # Direct access to referenced hash
284 print keys %{ MyClass->baz() };
285
286 # Reset the hash contents to empty
287 @{ MyClass->baz() } = ();
288  
289 =cut
290  
291 sub hash {
292 map {
293 my $name = $_->{name};
294 my $data;
295 my $init = $_->{auto_init};
296 $name => sub {
297 my $self = shift;
298 if ( scalar(@_) == 0 ) {
299 if ( $init and ! defined $data ) {
300 $data = {};
301 }
302 ! $data ? () : wantarray ? %$data : $data
303 } elsif ( scalar(@_) == 1 ) {
304 if ( ref($_[0]) eq 'HASH' ) {
305 my $hash = shift;
306 $data = { %$hash };
307 } elsif ( ref($_[0]) eq 'ARRAY' ) {
308 return @{$data}{ @{$_[0]} }
309 } else {
310 return $data->{ $_[0] }
311 }
312 } elsif ( scalar(@_) % 2 ) {
313 Carp::croak "Odd number of items in assigment to $name";
314 } else {
315 while ( scalar(@_) ) {
316 my $key = shift();
317 $data->{ $key } = shift();
318 }
319 wantarray ? %$data : $data;
320 }
321 }
322 } (shift)->_get_declarations(@_)
323 }
324  
325 ########################################################################
326  
327 =head2 object - Global Ref Accessor
328  
329 For each method name passed, uses a closure to generate a subroutine with the following characteristics:
330  
331 =over 4
332  
333 =item *
334  
335 May be called as a class method, or on any instance or subclass, and behaves identically regardless of what it was called on.
336  
337 =item *
338  
339 The global value will be a reference to an object (or undef).
340  
341 =item *
342  
343 If called without any arguments returns the current value.
344  
345 =item *
346  
347 If called with an argument, stores that as the value, and returns it,
348  
349 =back
350  
351 Sample declaration and usage:
352  
353 package MyClass;
354 use Class::MakeMethods::Standard::Global (
355 object => 'foo',
356 );
357 ...
358
359 # Store value
360 MyClass->foo( Foozle->new() );
361
362 # Retrieve value
363 print MyClass->foo;
364  
365 =cut
366  
367 sub object {
368 map {
369 my $name = $_->{name};
370 my $data;
371 my $class = $_->{class};
372 my $init = $_->{auto_init};
373 if ( $init and ! $class ) {
374 Carp::croak("Use of auto_init requires value for class parameter")
375 }
376 my $new_method = $_->{new_method} || 'new';
377 $name => sub {
378 my $self = shift;
379 if ( scalar @_ ) {
380 my $value = shift;
381 if ( $class and ! UNIVERSAL::isa( $value, $class ) ) {
382 Carp::croak "Wrong argument type ('$value') in assigment to $name";
383 }
384 $data = $value;
385 } else {
386 if ( $init and ! defined $data ) {
387 $data = $class->$new_method();
388 }
389 $data;
390 }
391 }
392 } (shift)->_get_declarations(@_)
393 }
394  
395 ########################################################################
396  
397 =head1 SEE ALSO
398  
399 See L<Class::MakeMethods> for general information about this distribution.
400  
401 See L<Class::MakeMethods::Standard> for more about this family of subclasses.
402  
403 =cut
404  
405 1;