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