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