Commit | Line | Data |
e3c07b19 |
1 | |
c466e58f |
2 | package Moose::Meta::Attribute::Native::Trait::Array; |
e3c07b19 |
3 | use Moose::Role; |
4 | |
122a129a |
5 | our $VERSION = '0.89'; |
e3c07b19 |
6 | $VERSION = eval $VERSION; |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
c466e58f |
9 | use Moose::Meta::Attribute::Native::MethodProvider::Array; |
e3c07b19 |
10 | |
c466e58f |
11 | with 'Moose::Meta::Attribute::Native::Trait'; |
e3c07b19 |
12 | |
13 | has '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 |
20 | sub _helper_type { 'ArrayRef' } |
e3c07b19 |
21 | |
22 | no Moose::Role; |
23 | |
e3c07b19 |
24 | 1; |
25 | |
26 | __END__ |
27 | |
28 | =pod |
29 | |
30 | =head1 NAME |
31 | |
c466e58f |
32 | Moose::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 | |
65 | This module provides an Array attribute which provides a number of |
80683705 |
66 | array operations. |
33f819e1 |
67 | |
68 | =head1 PROVIDED METHODS |
69 | |
70 | These methods are implemented in |
71 | L<Moose::Meta::Attribute::Native::MethodProvider::Array>. |
72 | |
73 | =over 4 |
74 | |
75 | =item B<count> |
76 | |
77 | Returns 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 |
87 | Returns 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 | |
93 | Returns 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 | |
100 | Returns an element of the array by its index. You can also use negative index |
101 | numbers, 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 | |
116 | These methods are all equivalent to the Perl core functions of the same name. |
117 | |
391c761c |
118 | =item B<first( sub { ... } )> |
33f819e1 |
119 | |
80683705 |
120 | This method returns the first item matching item in the array. The matching is |
121 | done with a subroutine reference you pass to this method. The reference will |
122 | be called against each element in the array until one matches or all elements |
123 | have been checked. |
33f819e1 |
124 | |
c9edbf05 |
125 | my $found = $stuff->find_option( sub { /^b/ } ); |
33f819e1 |
126 | print "$found\n"; # prints "bar" |
127 | |
cd50e921 |
128 | =item B<grep( sub { ... } )> |
33f819e1 |
129 | |
80683705 |
130 | This method returns every element matching a given criteria, just like Perl's |
131 | core C<grep> function. This method requires a subroutine which implements the |
132 | matching logic. |
33f819e1 |
133 | |
c9edbf05 |
134 | my @found = $stuff->filter_options( sub { /^b/ } ); |
33f819e1 |
135 | print "@found\n"; # prints "bar baz boo" |
136 | |
cd50e921 |
137 | =item B<map( sub { ... } )> |
33f819e1 |
138 | |
80683705 |
139 | This method transforms every element in the array and returns a new array, |
140 | just like Perl's core C<map> function. This method requires a subroutine which |
141 | implements the transformation. |
33f819e1 |
142 | |
c9edbf05 |
143 | my @mod_options = $stuff->map_options( sub { $_ . "-tag" } ); |
33f819e1 |
144 | print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" |
145 | |
cd50e921 |
146 | =item B<sort( sub { ... } )> |
33f819e1 |
147 | |
80683705 |
148 | Returns a the array in sorted order. |
33f819e1 |
149 | |
80683705 |
150 | You can provide an optional subroutine reference to sort with (as you can with |
151 | Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you |
152 | will 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 |
163 | Sorts the array I<in place>, modifying the value of the attribute. |
33f819e1 |
164 | |
cd50e921 |
165 | You can provide an optional subroutine reference to sort with (as you can with |
166 | Perl's core C<sort> function). However, instead of using C<$a> and C<$b>, you |
167 | will need to use C<$_[0]> and C<$_[1]> instead. |
33f819e1 |
168 | |
cd50e921 |
169 | =item B<join($str)> |
33f819e1 |
170 | |
80683705 |
171 | Joins every element of the array using the separator given as argument, just |
172 | like 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 |
179 | Given an index and a value, sets the specified array element's value. |
33f819e1 |
180 | |
cd50e921 |
181 | =item B<delete($index)> |
182 | |
183 | Removes the element at the given index from the array. |
184 | |
185 | =item B<insert($index, $value)> |
186 | |
187 | Inserts a new element into the array at the given index. |
188 | |
189 | =item B<clear> |
190 | |
191 | Empties the entire array, like C<@array = ()>. |
33f819e1 |
192 | |
391c761c |
193 | =item B<head> |
33f819e1 |
194 | |
195 | Returns 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 | |
202 | Returns 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 | |
209 | Returns 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 |
216 | This method provides a get/set accessor for the array, based on array indexes. |
217 | If passed one argument, it returns the value at the specified index. If |
218 | passed 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 | |
236 | All complex software has bugs lurking in it, and this module is no |
237 | exception. If you find a bug please either email me, or add the bug |
238 | to cpan-RT. |
239 | |
240 | =head1 AUTHOR |
241 | |
242 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
243 | |
244 | =head1 COPYRIGHT AND LICENSE |
245 | |
246 | Copyright 2007-2009 by Infinity Interactive, Inc. |
247 | |
248 | L<http://www.iinteractive.com> |
249 | |
250 | This library is free software; you can redistribute it and/or modify |
251 | it under the same terms as Perl itself. |
252 | |
253 | =cut |