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