Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait::Array; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
e3c07b19 |
5 | our $AUTHORITY = 'cpan:STEVAN'; |
6 | |
c466e58f |
7 | with 'Moose::Meta::Attribute::Native::Trait'; |
e3c07b19 |
8 | |
2e069f5a |
9 | sub _helper_type { 'ArrayRef' } |
e3c07b19 |
10 | |
11 | no Moose::Role; |
12 | |
e3c07b19 |
13 | 1; |
14 | |
ad46f524 |
15 | # ABSTRACT: Helper trait for array delegation |
16 | |
e3c07b19 |
17 | __END__ |
18 | |
19 | =pod |
20 | |
e3c07b19 |
21 | =head1 SYNOPSIS |
22 | |
33f819e1 |
23 | package Stuff; |
24 | use Moose; |
33f819e1 |
25 | |
26 | has 'options' => ( |
e132fd56 |
27 | traits => ['Array'], |
28 | is => 'ro', |
29 | isa => 'ArrayRef[Str]', |
30 | default => sub { [] }, |
31 | handles => { |
32 | all_options => 'elements', |
33 | add_option => 'push', |
34 | map_options => 'map', |
35 | filter_options => 'grep', |
36 | find_option => 'first', |
37 | get_option => 'get', |
38 | join_options => 'join', |
39 | count_options => 'count', |
40 | has_options => 'count', |
41 | has_no_options => 'is_empty', |
42 | sorted_options => 'sort', |
43 | }, |
33f819e1 |
44 | ); |
45 | |
46 | no Moose; |
47 | 1; |
80683705 |
48 | |
e3c07b19 |
49 | =head1 DESCRIPTION |
50 | |
7795e4de |
51 | This trait provides native delegation methods for array references. |
52 | |
53 | =head1 DEFAULT TYPE |
54 | |
55 | If you don't provide an C<isa> value for your attribute, it will default to |
56 | C<ArrayRef>. |
33f819e1 |
57 | |
58 | =head1 PROVIDED METHODS |
59 | |
33f819e1 |
60 | =over 4 |
61 | |
e132fd56 |
62 | =item * B<count> |
33f819e1 |
63 | |
64 | Returns the number of elements in the array. |
65 | |
e132fd56 |
66 | $stuff = Stuff->new; |
67 | $stuff->options( [ "foo", "bar", "baz", "boo" ] ); |
33f819e1 |
68 | |
e132fd56 |
69 | print $stuff->count_options; # prints 4 |
33f819e1 |
70 | |
e132fd56 |
71 | This method does not accept any arguments. |
72 | |
73 | =item * B<is_empty> |
33f819e1 |
74 | |
276828fa |
75 | Returns a boolean value that is true when the array has no elements. |
33f819e1 |
76 | |
e132fd56 |
77 | $stuff->has_no_options ? die "No options!\n" : print "Good boy.\n"; |
78 | |
79 | This method does not accept any arguments. |
80 | |
81 | =item * B<elements> |
33f819e1 |
82 | |
e132fd56 |
83 | Returns all of the elements of the array as an array (not an array reference). |
cd50e921 |
84 | |
e132fd56 |
85 | my @option = $stuff->all_options; |
86 | print "@options\n"; # prints "foo bar baz boo" |
cd50e921 |
87 | |
e132fd56 |
88 | This method does not accept any arguments. |
cd50e921 |
89 | |
e132fd56 |
90 | =item * B<get($index)> |
cd50e921 |
91 | |
92 | Returns an element of the array by its index. You can also use negative index |
93 | numbers, just as with Perl's core array handling. |
94 | |
e132fd56 |
95 | my $option = $stuff->get_option(1); |
96 | print "$option\n"; # prints "bar" |
97 | |
98 | If the specified element does not exist, this will return C<undef>. |
99 | |
100 | This method does accepts just one argument. |
101 | |
102 | =item * B<pop> |
103 | |
104 | Just like Perl's builtin C<pop>. |
105 | |
106 | This method does not accept any arguments. |
107 | |
108 | =item * B<push($value1, $value2, value3 ...)> |
109 | |
110 | Just like Perl's builtin C<push>. Returns the number of elements in the new |
111 | array. |
112 | |
113 | This method accepts any number of arguments. |
114 | |
115 | =item * B<shift> |
cd50e921 |
116 | |
e132fd56 |
117 | Just like Perl's builtin C<shift>. |
cd50e921 |
118 | |
e132fd56 |
119 | This method does not accept any arguments. |
cd50e921 |
120 | |
e132fd56 |
121 | =item * B<unshift($value1, $value2, value3 ...)> |
cd50e921 |
122 | |
e132fd56 |
123 | Just like Perl's builtin C<unshift>. Returns the number of elements in the new |
124 | array. |
cd50e921 |
125 | |
e132fd56 |
126 | This method accepts any number of arguments. |
cd50e921 |
127 | |
e132fd56 |
128 | =item * B<splice($offset, $length, @values)> |
cd50e921 |
129 | |
e132fd56 |
130 | Just like Perl's builtin C<splice>. In scalar context, this returns the last |
131 | element removed, or C<undef> if no elements were removed. In list context, |
132 | this returns all the elements removed from the array. |
133 | |
134 | This method requires at least one argument. |
135 | |
136 | =item * B<first( sub { ... } )> |
33f819e1 |
137 | |
7960bcc0 |
138 | This method returns the first item matching item in the array, just like |
139 | L<List::Util>'s C<first> function. The matching is done with a subroutine |
e132fd56 |
140 | reference you pass to this method. The subroutine will be called against each |
7960bcc0 |
141 | element in the array until one matches or all elements have been checked. |
33f819e1 |
142 | |
e132fd56 |
143 | my $found = $stuff->find_option( sub {/^b/} ); |
144 | print "$found\n"; # prints "bar" |
145 | |
146 | This method requires a single argument. |
33f819e1 |
147 | |
e132fd56 |
148 | =item * B<grep( sub { ... } )> |
33f819e1 |
149 | |
80683705 |
150 | This method returns every element matching a given criteria, just like Perl's |
151 | core C<grep> function. This method requires a subroutine which implements the |
152 | matching logic. |
33f819e1 |
153 | |
e132fd56 |
154 | my @found = $stuff->filter_options( sub {/^b/} ); |
155 | print "@found\n"; # prints "bar baz boo" |
33f819e1 |
156 | |
e132fd56 |
157 | This method requires a single argument. |
158 | |
159 | =item * B<map( sub { ... } )> |
33f819e1 |
160 | |
80683705 |
161 | This method transforms every element in the array and returns a new array, |
162 | just like Perl's core C<map> function. This method requires a subroutine which |
163 | implements the transformation. |
33f819e1 |
164 | |
e132fd56 |
165 | my @mod_options = $stuff->map_options( sub { $_ . "-tag" } ); |
166 | print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" |
167 | |
168 | This method requires a single argument. |
33f819e1 |
169 | |
e132fd56 |
170 | =item * B<reduce( sub { ... } )> |
7960bcc0 |
171 | |
e132fd56 |
172 | This method turns an array into a single value, by passing a function the |
7960bcc0 |
173 | value so far and the next value in the array, just like L<List::Util>'s |
174 | C<reduce> function. The reducing is done with a subroutine reference you pass |
175 | to this method. |
176 | |
e132fd56 |
177 | my $found = $stuff->reduce_options( sub { $_[0] . $_[1] } ); |
178 | print "$found\n"; # prints "foobarbazboo" |
179 | |
180 | This method requires a single argument. |
7960bcc0 |
181 | |
e132fd56 |
182 | =item * B<sort> |
33f819e1 |
183 | |
e132fd56 |
184 | =item * B<sort( sub { ... } )> |
185 | |
186 | Returns the elements of the array in sorted order. |
33f819e1 |
187 | |
80683705 |
188 | You can provide an optional subroutine reference to sort with (as you can with |
e132fd56 |
189 | Perl's core C<sort> function). However, instead of using C<$a> and C<$b> in |
190 | this subroutine, you will need to use C<$_[0]> and C<$_[1]>. |
191 | |
192 | # ascending ASCIIbetical |
193 | my @sorted = $stuff->sort_options(); |
194 | |
195 | # Descending alphabetical order |
196 | my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } ); |
197 | print "@sorted_options\n"; # prints "foo boo baz bar" |
33f819e1 |
198 | |
e132fd56 |
199 | This method accepts a single argument. |
33f819e1 |
200 | |
e132fd56 |
201 | =item * B<sort_in_place> |
33f819e1 |
202 | |
e132fd56 |
203 | =item * B<sort_in_place( sub { ... } )> |
33f819e1 |
204 | |
cd50e921 |
205 | Sorts the array I<in place>, modifying the value of the attribute. |
33f819e1 |
206 | |
cd50e921 |
207 | You can provide an optional subroutine reference to sort with (as you can with |
208 | Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you |
209 | will need to use C<$_[0]> and C<$_[1]> instead. |
33f819e1 |
210 | |
e132fd56 |
211 | This method does not define a return value. |
7960bcc0 |
212 | |
e132fd56 |
213 | This method accepts a single argument. |
214 | |
215 | =item * B<shuffle> |
216 | |
217 | Returns the elements of the array in random order, like C<shuffle> from |
7960bcc0 |
218 | L<List::Util>. |
219 | |
e132fd56 |
220 | This method does not accept any arguments. |
221 | |
222 | =item * B<uniq> |
7960bcc0 |
223 | |
e132fd56 |
224 | Returns the array with all duplicate elements removed, like C<uniq> from |
7960bcc0 |
225 | L<List::MoreUtils>. |
226 | |
e132fd56 |
227 | This method does not accept any arguments. |
228 | |
229 | =item * B<join($str)> |
33f819e1 |
230 | |
80683705 |
231 | Joins every element of the array using the separator given as argument, just |
232 | like Perl's core C<join> function. |
33f819e1 |
233 | |
e132fd56 |
234 | my $joined = $stuff->join_options(':'); |
235 | print "$joined\n"; # prints "foo:bar:baz:boo" |
236 | |
237 | This method requires a single argument. |
33f819e1 |
238 | |
e132fd56 |
239 | =item * B<set($index, $value)> |
33f819e1 |
240 | |
cd50e921 |
241 | Given an index and a value, sets the specified array element's value. |
33f819e1 |
242 | |
e132fd56 |
243 | This method returns the value at C<$index> after the set. |
244 | |
245 | This method requires two arguments. |
246 | |
247 | =item * B<delete($index)> |
cd50e921 |
248 | |
249 | Removes the element at the given index from the array. |
250 | |
e132fd56 |
251 | This method returns the deleted value. Note that if no value exists, it will |
252 | return C<undef>. |
253 | |
254 | This method requires one argument. |
255 | |
256 | =item * B<insert($index, $value)> |
cd50e921 |
257 | |
258 | Inserts a new element into the array at the given index. |
259 | |
e132fd56 |
260 | This method returns the new value at C<$index>. |
261 | |
262 | This method requires two arguments. |
263 | |
264 | =item * B<clear> |
cd50e921 |
265 | |
266 | Empties the entire array, like C<@array = ()>. |
33f819e1 |
267 | |
e132fd56 |
268 | This method does not define a return value. |
269 | |
270 | This method does not accept any arguments. |
271 | |
272 | =item * B<accessor($index)> |
273 | |
274 | =item * B<accessor($index, $value)> |
33f819e1 |
275 | |
80683705 |
276 | This method provides a get/set accessor for the array, based on array indexes. |
277 | If passed one argument, it returns the value at the specified index. If |
278 | passed two arguments, it sets the value of the specified index. |
33f819e1 |
279 | |
e132fd56 |
280 | When called as a setter, this method returns the new value at C<$index>. |
281 | |
282 | This method accepts one or two arguments. |
283 | |
284 | =item * B<natatime($n)> |
285 | |
286 | =item * B<natatime($n, $code)> |
7960bcc0 |
287 | |
288 | This method returns an iterator which, on each call, returns C<$n> more items |
289 | from the array, in order, like C<natatime> from L<List::MoreUtils>. A coderef |
290 | can optionally be provided; it will be called on each group of C<$n> elements |
291 | in the array. |
292 | |
e132fd56 |
293 | This method accepts one or two arguments. |
e3c07b19 |
294 | |
e3c07b19 |
295 | =back |
296 | |
297 | =head1 BUGS |
298 | |
d4048ef3 |
299 | See L<Moose/BUGS> for details on reporting bugs. |
e3c07b19 |
300 | |
e3c07b19 |
301 | =cut |