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