clarification re the limitations of the "map" array delegation handler
[gitmo/Moose.git] / lib / Moose / Meta / Attribute / Native / Trait / Array.pm
1
2 package Moose::Meta::Attribute::Native::Trait::Array;
3 use Moose::Role;
4
5 our $VERSION   = '1.9900';
6 $VERSION = eval $VERSION;
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 with 'Moose::Meta::Attribute::Native::Trait';
10
11 sub _helper_type { 'ArrayRef' }
12
13 no Moose::Role;
14
15 1;
16
17 __END__
18
19 =pod
20
21 =head1 NAME
22
23 Moose::Meta::Attribute::Native::Trait::Array - Helper trait for array delegation
24
25 =head1 SYNOPSIS
26
27     package Stuff;
28     use Moose;
29
30     has 'options' => (
31         traits  => ['Array'],
32         is      => 'ro',
33         isa     => 'ArrayRef[Str]',
34         default => sub { [] },
35         handles => {
36             all_options    => 'elements',
37             add_option     => 'push',
38             map_options    => 'map',
39             filter_options => 'grep',
40             find_option    => 'first',
41             get_option     => 'get',
42             join_options   => 'join',
43             count_options  => 'count',
44             has_options    => 'count',
45             has_no_options => 'is_empty',
46             sorted_options => 'sort',
47         },
48     );
49
50     no Moose;
51     1;
52
53 =head1 DESCRIPTION
54
55 This trait provides native delegation methods for array references.
56
57 =head1 DEFAULT TYPE
58
59 If you don't provide an C<isa> value for your attribute, it will default to
60 C<ArrayRef>.
61
62 =head1 PROVIDED METHODS
63
64 =over 4
65
66 =item * B<count>
67
68 Returns the number of elements in the array.
69
70   $stuff = Stuff->new;
71   $stuff->options( [ "foo", "bar", "baz", "boo" ] );
72
73   print $stuff->count_options; # prints 4
74
75 This method does not accept any arguments.
76
77 =item * B<is_empty>
78
79 Returns a boolean value that is true when the array has no elements.
80
81   $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
82
83 This method does not accept any arguments.
84
85 =item * B<elements>
86
87 Returns all of the elements of the array as an array (not an array reference).
88
89   my @option = $stuff->all_options;
90   print "@options\n";    # prints "foo bar baz boo"
91
92 This method does not accept any arguments.
93
94 =item * B<get($index)>
95
96 Returns an element of the array by its index. You can also use negative index
97 numbers, just as with Perl's core array handling.
98
99   my $option = $stuff->get_option(1);
100   print "$option\n";    # prints "bar"
101
102 If the specified element does not exist, this will return C<undef>.
103
104 This method does accepts just one argument.
105
106 =item * B<pop>
107
108 Just like Perl's builtin C<pop>.
109
110 This method does not accept any arguments.
111
112 =item * B<push($value1, $value2, value3 ...)>
113
114 Just like Perl's builtin C<push>. Returns the number of elements in the new
115 array.
116
117 This method accepts any number of arguments.
118
119 =item * B<shift>
120
121 Just like Perl's builtin C<shift>.
122
123 This method does not accept any arguments.
124
125 =item * B<unshift($value1, $value2, value3 ...)>
126
127 Just like Perl's builtin C<unshift>. Returns the number of elements in the new
128 array.
129
130 This method accepts any number of arguments.
131
132 =item * B<splice($offset, $length, @values)>
133
134 Just like Perl's builtin C<splice>. In scalar context, this returns the last
135 element removed, or C<undef> if no elements were removed. In list context,
136 this returns all the elements removed from the array.
137
138 This method requires at least one argument.
139
140 =item * B<first( sub { ... } )>
141
142 This method returns the first item matching item in the array, just like
143 L<List::Util>'s C<first> function. The matching is done with a subroutine
144 reference you pass to this method. The subroutine will be called against each
145 element in the array until one matches or all elements have been checked.
146
147   my $found = $stuff->find_option( sub {/^b/} );
148   print "$found\n";    # prints "bar"
149
150 This method requires a single argument.
151
152 =item * B<grep( sub { ... } )>
153
154 This method returns every element matching a given criteria, just like Perl's
155 core C<grep> function. This method requires a subroutine which implements the
156 matching logic.
157
158   my @found = $stuff->filter_options( sub {/^b/} );
159   print "@found\n";    # prints "bar baz boo"
160
161 This method requires a single argument.
162
163 =item * B<map( sub { ... } )>
164
165 This method transforms every element in the array and returns a new array,
166 just like Perl's core C<map> function. This method requires a subroutine which
167 implements the transformation.
168
169 Note that if you add or subtract values within the supplied coderef, the
170 values are I<not> saved back to the attribute. You need to use the attribute
171 writer wrapped around the map call for that.
172
173   my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
174   print "@mod_options\n";    # prints "foo-tag bar-tag baz-tag boo-tag"
175
176 This method requires a single argument.
177
178 =item * B<reduce( sub { ... } )>
179
180 This method turns an array into a single value, by passing a function the
181 value so far and the next value in the array, just like L<List::Util>'s
182 C<reduce> function. The reducing is done with a subroutine reference you pass
183 to this method.
184
185   my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
186   print "$found\n";    # prints "foobarbazboo"
187
188 This method requires a single argument.
189
190 =item * B<sort>
191
192 =item * B<sort( sub { ... } )>
193
194 Returns the elements of the array in sorted order.
195
196 You can provide an optional subroutine reference to sort with (as you can with
197 Perl's core C<sort> function). However, instead of using C<$a> and C<$b> in
198 this subroutine, you will need to use C<$_[0]> and C<$_[1]>.
199
200   # ascending ASCIIbetical
201   my @sorted = $stuff->sort_options();
202
203   # Descending alphabetical order
204   my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
205   print "@sorted_options\n";    # prints "foo boo baz bar"
206
207 This method accepts a single argument.
208
209 =item * B<sort_in_place>
210
211 =item * B<sort_in_place( sub { ... } )>
212
213 Sorts the array I<in place>, modifying the value of the attribute.
214
215 You can provide an optional subroutine reference to sort with (as you can with
216 Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
217 will need to use C<$_[0]> and C<$_[1]> instead.
218
219 This method does not define a return value.
220
221 This method accepts a single argument.
222
223 =item * B<shuffle>
224
225 Returns the elements of the array in random order, like C<shuffle> from
226 L<List::Util>.
227
228 This method does not accept any arguments.
229
230 =item * B<uniq>
231
232 Returns the array with all duplicate elements removed, like C<uniq> from
233 L<List::MoreUtils>.
234
235 This method does not accept any arguments.
236
237 =item * B<join($str)>
238
239 Joins every element of the array using the separator given as argument, just
240 like Perl's core C<join> function.
241
242   my $joined = $stuff->join_options(':');
243   print "$joined\n";    # prints "foo:bar:baz:boo"
244
245 This method requires a single argument.
246
247 =item * B<set($index, $value)>
248
249 Given an index and a value, sets the specified array element's value.
250
251 This method returns the value at C<$index> after the set.
252
253 This method requires two arguments.
254
255 =item * B<delete($index)>
256
257 Removes the element at the given index from the array.
258
259 This method returns the deleted value. Note that if no value exists, it will
260 return C<undef>.
261
262 This method requires one argument.
263
264 =item * B<insert($index, $value)>
265
266 Inserts a new element into the array at the given index.
267
268 This method returns the new value at C<$index>.
269
270 This method requires two arguments.
271
272 =item * B<clear>
273
274 Empties the entire array, like C<@array = ()>.
275
276 This method does not define a return value.
277
278 This method does not accept any arguments.
279
280 =item * B<accessor($index)>
281
282 =item * B<accessor($index, $value)>
283
284 This method provides a get/set accessor for the array, based on array indexes.
285 If passed one argument, it returns the value at the specified index.  If
286 passed two arguments, it sets the value of the specified index.
287
288 When called as a setter, this method returns the new value at C<$index>.
289
290 This method accepts one or two arguments.
291
292 =item * B<natatime($n)>
293
294 =item * B<natatime($n, $code)>
295
296 This method returns an iterator which, on each call, returns C<$n> more items
297 from the array, in order, like C<natatime> from L<List::MoreUtils>. A coderef
298 can optionally be provided; it will be called on each group of C<$n> elements
299 in the array.
300
301 This method accepts one or two arguments.
302
303 =back
304
305 =head1 BUGS
306
307 See L<Moose/BUGS> for details on reporting bugs.
308
309 =head1 AUTHOR
310
311 Stevan Little E<lt>stevan@iinteractive.comE<gt>
312
313 =head1 COPYRIGHT AND LICENSE
314
315 Copyright 2007-2010 by Infinity Interactive, Inc.
316
317 L<http://www.iinteractive.com>
318
319 This library is free software; you can redistribute it and/or modify
320 it under the same terms as Perl itself.
321
322 =cut