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