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