All native array methods are being inlined.
[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
b6cca0d5 5our $VERSION = '1.14';
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
f7fd22b6 38sub _native_type { 'Array' }
39
e3c07b19 40no Moose::Role;
41
e3c07b19 421;
43
44__END__
45
46=pod
47
48=head1 NAME
49
2420461c 50Moose::Meta::Attribute::Native::Trait::Array - Helper trait for ArrayRef attributes
e3c07b19 51
52=head1 SYNOPSIS
53
33f819e1 54 package Stuff;
55 use Moose;
33f819e1 56
57 has 'options' => (
58 traits => ['Array'],
59 is => 'ro',
60 isa => 'ArrayRef[Str]',
61 default => sub { [] },
9610c1d2 62 handles => {
63 all_options => 'elements',
39e17fef 64 add_option => 'push',
9610c1d2 65 map_options => 'map',
66 filter_options => 'grep',
67 find_option => 'first',
68 get_option => 'get',
69 join_options => 'join',
70 count_options => 'count',
39e17fef 71 has_options => 'count',
9610c1d2 72 has_no_options => 'is_empty',
73 sorted_options => 'sort',
74 },
33f819e1 75 );
76
77 no Moose;
78 1;
80683705 79
e3c07b19 80=head1 DESCRIPTION
81
82This module provides an Array attribute which provides a number of
80683705 83array operations.
33f819e1 84
85=head1 PROVIDED METHODS
86
87These methods are implemented in
88L<Moose::Meta::Attribute::Native::MethodProvider::Array>.
89
90=over 4
91
92=item B<count>
93
94Returns the number of elements in the array.
95
96 $stuff = Stuff->new;
97 $stuff->options(["foo", "bar", "baz", "boo"]);
98
99 my $count = $stuff->count_options;
100 print "$count\n"; # prints 4
101
1853a27e 102=item B<is_empty>
33f819e1 103
276828fa 104Returns a boolean value that is true when the array has no elements.
33f819e1 105
af44c00c 106 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
33f819e1 107
cd50e921 108=item B<elements>
109
110Returns all of the elements of the array.
111
112 my @option = $stuff->all_options;
113 print "@options\n"; # prints "foo bar baz boo"
114
115=item B<get($index)>
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
120 my $option = $stuff->get_option(1);
121 print "$option\n"; # prints "bar"
122
123=item B<pop>
124
39e17fef 125=item B<push($value1, $value2, value3 ...)>
cd50e921 126
127=item B<shift>
128
39e17fef 129=item B<unshift($value1, $value2, value3 ...)>
cd50e921 130
131=item B<splice($offset, $length, @values)>
132
133These methods are all equivalent to the Perl core functions of the same name.
134
391c761c 135=item B<first( sub { ... } )>
33f819e1 136
7960bcc0 137This method returns the first item matching item in the array, just like
138L<List::Util>'s C<first> function. The matching is done with a subroutine
139reference you pass to this method. The reference will be called against each
140element in the array until one matches or all elements have been checked.
33f819e1 141
c9edbf05 142 my $found = $stuff->find_option( sub { /^b/ } );
33f819e1 143 print "$found\n"; # prints "bar"
144
cd50e921 145=item B<grep( sub { ... } )>
33f819e1 146
80683705 147This method returns every element matching a given criteria, just like Perl's
148core C<grep> function. This method requires a subroutine which implements the
149matching logic.
33f819e1 150
c9edbf05 151 my @found = $stuff->filter_options( sub { /^b/ } );
33f819e1 152 print "@found\n"; # prints "bar baz boo"
153
cd50e921 154=item B<map( sub { ... } )>
33f819e1 155
80683705 156This method transforms every element in the array and returns a new array,
157just like Perl's core C<map> function. This method requires a subroutine which
158implements the transformation.
33f819e1 159
c9edbf05 160 my @mod_options = $stuff->map_options( sub { $_ . "-tag" } );
33f819e1 161 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
162
7960bcc0 163=item B<reduce( sub { ... } )>
164
165This method condenses an array into a single value, by passing a function the
166value so far and the next value in the array, just like L<List::Util>'s
167C<reduce> function. The reducing is done with a subroutine reference you pass
168to this method.
169
170 my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } );
171 print "$found\n"; # prints "foobarbazboo"
172
cd50e921 173=item B<sort( sub { ... } )>
33f819e1 174
80683705 175Returns a the array in sorted order.
33f819e1 176
80683705 177You can provide an optional subroutine reference to sort with (as you can with
178Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
179will need to use C<$_[0]> and C<$_[1]> instead.
33f819e1 180
181 # ascending ASCIIbetical
182 my @sorted = $stuff->sort_options();
183
184 # Descending alphabetical order
185 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
186 print "@sorted_options\n"; # prints "foo boo baz bar"
187
cd50e921 188=item B<sort_in_place>
33f819e1 189
cd50e921 190Sorts the array I<in place>, modifying the value of the attribute.
33f819e1 191
cd50e921 192You can provide an optional subroutine reference to sort with (as you can with
193Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
194will need to use C<$_[0]> and C<$_[1]> instead.
33f819e1 195
7960bcc0 196=item B<shuffle>
197
198Returns the array, with indices in random order, like C<shuffle> from
199L<List::Util>.
200
201=item B<uniq>
202
203Returns the array, with all duplicate elements removed, like C<uniq> from
204L<List::MoreUtils>.
205
cd50e921 206=item B<join($str)>
33f819e1 207
80683705 208Joins every element of the array using the separator given as argument, just
209like Perl's core C<join> function.
33f819e1 210
211 my $joined = $stuff->join_options( ':' );
212 print "$joined\n"; # prints "foo:bar:baz:boo"
213
cd50e921 214=item B<set($index, $value)>
33f819e1 215
cd50e921 216Given an index and a value, sets the specified array element's value.
33f819e1 217
cd50e921 218=item B<delete($index)>
219
220Removes the element at the given index from the array.
221
222=item B<insert($index, $value)>
223
224Inserts a new element into the array at the given index.
225
226=item B<clear>
227
228Empties the entire array, like C<@array = ()>.
33f819e1 229
33f819e1 230=item B<accessor>
231
80683705 232This method provides a get/set accessor for the array, based on array indexes.
233If passed one argument, it returns the value at the specified index. If
234passed two arguments, it sets the value of the specified index.
33f819e1 235
7960bcc0 236=item B<natatime($n, $code)>
237
238This method returns an iterator which, on each call, returns C<$n> more items
239from the array, in order, like C<natatime> from L<List::MoreUtils>. A coderef
240can optionally be provided; it will be called on each group of C<$n> elements
241in the array.
242
33f819e1 243=back
e3c07b19 244
245=head1 METHODS
246
247=over 4
248
249=item B<meta>
250
251=item B<method_provider>
252
253=item B<has_method_provider>
254
e3c07b19 255=back
256
257=head1 BUGS
258
d4048ef3 259See L<Moose/BUGS> for details on reporting bugs.
e3c07b19 260
261=head1 AUTHOR
262
263Stevan Little E<lt>stevan@iinteractive.comE<gt>
264
265=head1 COPYRIGHT AND LICENSE
266
267Copyright 2007-2009 by Infinity Interactive, Inc.
268
269L<http://www.iinteractive.com>
270
271This library is free software; you can redistribute it and/or modify
272it under the same terms as Perl itself.
273
274=cut