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