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