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