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