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