rename/add a few methods on 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',
49 first_option => 'head',
50 all_but_first_option => 'tail',
51 last_option => 'last',
52 get_option => 'get',
53 join_options => 'join',
54 count_options => 'count',
55 has_no_options => 'empty',
56 sorted_options => 'sort',
33f819e1 57 }
58 );
59
60 no Moose;
61 1;
80683705 62
e3c07b19 63=head1 DESCRIPTION
64
65This module provides an Array attribute which provides a number of
80683705 66array operations.
33f819e1 67
68=head1 PROVIDED METHODS
69
70These methods are implemented in
71L<Moose::Meta::Attribute::Native::MethodProvider::Array>.
72
73=over 4
74
75=item B<count>
76
77Returns the number of elements in the array.
78
79 $stuff = Stuff->new;
80 $stuff->options(["foo", "bar", "baz", "boo"]);
81
82 my $count = $stuff->count_options;
83 print "$count\n"; # prints 4
84
85=item B<empty>
86
80683705 87Returns a boolean value indicating whether or not the array has any elements.
33f819e1 88
af44c00c 89 $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n";
33f819e1 90
cd50e921 91=item B<elements>
92
93Returns all of the elements of the array.
94
95 my @option = $stuff->all_options;
96 print "@options\n"; # prints "foo bar baz boo"
97
98=item B<get($index)>
99
100Returns an element of the array by its index. You can also use negative index
101numbers, just as with Perl's core array handling.
102
103 my $option = $stuff->get_option(1);
104 print "$option\n"; # prints "bar"
105
106=item B<pop>
107
108=item B<push($value)>
109
110=item B<shift>
111
112=item B<unshift($value)>
113
114=item B<splice($offset, $length, @values)>
115
116These methods are all equivalent to the Perl core functions of the same name.
117
391c761c 118=item B<first( sub { ... } )>
33f819e1 119
80683705 120This method returns the first item matching item in the array. The matching is
121done with a subroutine reference you pass to this method. The reference will
122be called against each element in the array until one matches or all elements
123have been checked.
33f819e1 124
125 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
126 print "$found\n"; # prints "bar"
127
cd50e921 128=item B<grep( sub { ... } )>
33f819e1 129
80683705 130This method returns every element matching a given criteria, just like Perl's
131core C<grep> function. This method requires a subroutine which implements the
132matching logic.
33f819e1 133
134 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
135 print "@found\n"; # prints "bar baz boo"
136
cd50e921 137=item B<map( sub { ... } )>
33f819e1 138
80683705 139This method transforms every element in the array and returns a new array,
140just like Perl's core C<map> function. This method requires a subroutine which
141implements the transformation.
33f819e1 142
143 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
144 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
145
cd50e921 146=item B<sort( sub { ... } )>
33f819e1 147
80683705 148Returns a the array in sorted order.
33f819e1 149
80683705 150You can provide an optional subroutine reference to sort with (as you can with
151Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
152will need to use C<$_[0]> and C<$_[1]> instead.
33f819e1 153
154 # ascending ASCIIbetical
155 my @sorted = $stuff->sort_options();
156
157 # Descending alphabetical order
158 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
159 print "@sorted_options\n"; # prints "foo boo baz bar"
160
cd50e921 161=item B<sort_in_place>
33f819e1 162
cd50e921 163Sorts the array I<in place>, modifying the value of the attribute.
33f819e1 164
cd50e921 165You can provide an optional subroutine reference to sort with (as you can with
166Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you
167will need to use C<$_[0]> and C<$_[1]> instead.
33f819e1 168
cd50e921 169=item B<join($str)>
33f819e1 170
80683705 171Joins every element of the array using the separator given as argument, just
172like Perl's core C<join> function.
33f819e1 173
174 my $joined = $stuff->join_options( ':' );
175 print "$joined\n"; # prints "foo:bar:baz:boo"
176
cd50e921 177=item B<set($index, $value)>
33f819e1 178
cd50e921 179Given an index and a value, sets the specified array element's value.
33f819e1 180
cd50e921 181=item B<delete($index)>
182
183Removes the element at the given index from the array.
184
185=item B<insert($index, $value)>
186
187Inserts a new element into the array at the given index.
188
189=item B<clear>
190
191Empties the entire array, like C<@array = ()>.
33f819e1 192
391c761c 193=item B<head>
33f819e1 194
195Returns the first element of the array.
196
197 my $first = $stuff->first_option;
198 print "$first\n"; # prints "foo"
199
391c761c 200=item B<tail>
201
202Returns all elements of the array after the first.
203
204 my @tail = $stuff->all_but_first_option;
205 print join(', ', @tail), "\n"; # prints "bar, baz, boo"
206
33f819e1 207=item B<last>
208
209Returns the last element of the array.
210
211 my $last = $stuff->last_option;
212 print "$last\n"; # prints "boo"
213
33f819e1 214=item B<accessor>
215
80683705 216This method provides a get/set accessor for the array, based on array indexes.
217If passed one argument, it returns the value at the specified index. If
218passed two arguments, it sets the value of the specified index.
33f819e1 219
220=back
e3c07b19 221
222=head1 METHODS
223
224=over 4
225
226=item B<meta>
227
228=item B<method_provider>
229
230=item B<has_method_provider>
231
e3c07b19 232=back
233
234=head1 BUGS
235
236All complex software has bugs lurking in it, and this module is no
237exception. If you find a bug please either email me, or add the bug
238to cpan-RT.
239
240=head1 AUTHOR
241
242Stevan Little E<lt>stevan@iinteractive.comE<gt>
243
244=head1 COPYRIGHT AND LICENSE
245
246Copyright 2007-2009 by Infinity Interactive, Inc.
247
248L<http://www.iinteractive.com>
249
250This library is free software; you can redistribute it and/or modify
251it under the same terms as Perl itself.
252
253=cut