Commit | Line | Data |
457dc4fb |
1 | package MooseX::AttributeHelpers::MethodProvider::List; |
2 | use Moose::Role; |
3 | |
7a93b96e |
4 | our $VERSION = '0.14'; |
38430345 |
5 | $VERSION = eval $VERSION; |
457dc4fb |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
8 | sub count : method { |
9 | my ($attr, $reader, $writer) = @_; |
10 | return sub { |
11 | scalar @{$reader->($_[0])} |
12 | }; |
13 | } |
14 | |
15 | sub empty : method { |
16 | my ($attr, $reader, $writer) = @_; |
17 | return sub { |
facf2175 |
18 | scalar @{$reader->($_[0])} ? 1 : 0 |
457dc4fb |
19 | }; |
20 | } |
21 | |
22 | sub find : method { |
23 | my ($attr, $reader, $writer) = @_; |
24 | return sub { |
25 | my ($instance, $predicate) = @_; |
26 | foreach my $val (@{$reader->($instance)}) { |
27 | return $val if $predicate->($val); |
28 | } |
29 | return; |
30 | }; |
31 | } |
32 | |
33 | sub map : method { |
34 | my ($attr, $reader, $writer) = @_; |
35 | return sub { |
36 | my ($instance, $f) = @_; |
37 | CORE::map { $f->($_) } @{$reader->($instance)} |
38 | }; |
39 | } |
40 | |
41 | sub grep : method { |
42 | my ($attr, $reader, $writer) = @_; |
43 | return sub { |
44 | my ($instance, $predicate) = @_; |
45 | CORE::grep { $predicate->($_) } @{$reader->($instance)} |
46 | }; |
47 | } |
48 | |
6f60a71e |
49 | sub elements : method { |
50 | my ($attr, $reader, $writer) = @_; |
51 | return sub { |
c80e1668 |
52 | my ($instance) = @_; |
6f60a71e |
53 | @{$reader->($instance)} |
54 | }; |
55 | } |
56 | |
d57bba34 |
57 | sub contains : method { |
58 | my ($attr, $reader, $writer) = @_; |
59 | return sub { |
60 | my ($instance, $item) = @_; |
61 | return scalar (defined $item ? |
62 | CORE::grep {defined && $_ eq $item} @{$reader->($instance)} |
63 | : CORE::grep {!defined} @{$reader->($instance)} |
64 | ); |
65 | }; |
66 | } |
67 | |
654096bc |
68 | sub join : method { |
69 | my ($attr, $reader, $writer) = @_; |
70 | return sub { |
71 | my ($instance, $separator) = @_; |
72 | join $separator, @{$reader->($instance)} |
73 | }; |
74 | } |
75 | |
b77cfe61 |
76 | sub get : method { |
77 | my ($attr, $reader, $writer) = @_; |
78 | return sub { |
79 | $reader->($_[0])->[$_[1]] |
80 | }; |
81 | } |
82 | |
83 | sub first : method { |
84 | my ($attr, $reader, $writer) = @_; |
85 | return sub { |
86 | $reader->($_[0])->[0] |
87 | }; |
88 | } |
89 | |
90 | sub last : method { |
91 | my ($attr, $reader, $writer) = @_; |
92 | return sub { |
93 | $reader->($_[0])->[-1] |
94 | }; |
95 | } |
96 | |
457dc4fb |
97 | 1; |
98 | |
99 | __END__ |
100 | |
101 | =pod |
102 | |
103 | =head1 NAME |
104 | |
105 | MooseX::AttributeHelpers::MethodProvider::List |
1b5dc533 |
106 | |
107 | =SYNOPSIS |
108 | |
109 | package Stuff; |
110 | use Moose; |
111 | use MooseX::AttributeHelpers; |
112 | |
113 | has 'options' => ( |
114 | metaclass => 'Collection::List', |
115 | is => 'rw', |
116 | isa => 'ArrayRef[Str]', |
117 | default => sub { [] }, |
118 | auto_deref => 1, |
119 | provides => { |
d57bba34 |
120 | map => 'map_options', |
121 | grep => 'filter_options', |
122 | find => 'find_option', |
123 | first => 'first_option', |
124 | last => 'last_option', |
125 | get => 'get_option', |
126 | join => 'join_options', |
127 | count => 'count_options', |
128 | empty => 'do_i_have_options', |
129 | contains => 'options_contains', |
1b5dc533 |
130 | |
131 | } |
132 | ); |
133 | |
134 | no Moose; |
135 | 1; |
136 | |
457dc4fb |
137 | =head1 DESCRIPTION |
138 | |
139 | This is a role which provides the method generators for |
140 | L<MooseX::AttributeHelpers::Collection::List>. |
141 | |
142 | =head1 METHODS |
143 | |
144 | =over 4 |
145 | |
146 | =item B<meta> |
147 | |
148 | =back |
149 | |
150 | =head1 PROVIDED METHODS |
151 | |
152 | =over 4 |
153 | |
154 | =item B<count> |
1b5dc533 |
155 | Returns the number of elements of the list. |
156 | |
157 | $stuff = Stuff->new; |
158 | $stuff->options(["foo", "bar", "baz", "boo"]); |
159 | |
160 | my $count = $stuff->count_options; |
161 | print "$count\n"; # prints 4 |
162 | |
163 | =item B<empty> |
164 | If the list is populated, returns true. Otherwise, returns false. |
457dc4fb |
165 | |
1b5dc533 |
166 | $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ; |
457dc4fb |
167 | |
168 | =item B<find> |
1b5dc533 |
169 | Returns the first element that returns true in the anonymous subroutine |
170 | passed as argument. |
171 | |
172 | my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } ); |
173 | print "$found\n"; # prints "bar" |
457dc4fb |
174 | |
175 | =item B<grep> |
b290a465 |
176 | Returns every element of the list that returns true in the anonymous |
1b5dc533 |
177 | subroutine passed as argument. |
178 | |
179 | my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } ); |
180 | print "@found\n"; # prints "bar baz boo" |
457dc4fb |
181 | |
182 | =item B<map> |
b290a465 |
183 | Executes the anonymous subroutine given as argument sequentially |
1b5dc533 |
184 | for each element of the list. |
185 | |
186 | my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } ); |
187 | print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag" |
457dc4fb |
188 | |
6f60a71e |
189 | =item B<elements> |
1b5dc533 |
190 | Returns an element of the list by its index. |
191 | |
192 | my $option = $stuff->get_option(1); |
193 | print "$option\n"; # prints "bar" |
6f60a71e |
194 | |
d57bba34 |
195 | =item B<contains> |
196 | Returns a true value if the list contains the passed value, otherwise returns |
197 | a false value. The return value is actually the number of times the passed |
198 | value appears in the list (0, or more). |
199 | |
200 | my $found = $stuff->contains( 'apple' ); |
201 | print "Found apple in the list\n" if $found; |
202 | |
203 | Note that this works even when looking for the undefined value. |
204 | |
654096bc |
205 | =item B<join> |
b290a465 |
206 | Joins every element of the list using the separator given as argument. |
1b5dc533 |
207 | |
208 | my $joined = $stuff->join_options( ':' ); |
209 | print "$joined\n"; # prints "foo:bar:baz:boo" |
654096bc |
210 | |
b77cfe61 |
211 | =item B<get> |
1b5dc533 |
212 | Returns an element of the list by its index. |
213 | |
214 | my $option = $stuff->get_option(1); |
215 | print "$option\n"; # prints "bar" |
b77cfe61 |
216 | |
217 | =item B<first> |
1b5dc533 |
218 | Returns the first element. |
219 | |
220 | my $first = $stuff->first_option; |
221 | print "$first\n"; # prints "foo" |
b77cfe61 |
222 | |
223 | =item B<last> |
1b5dc533 |
224 | Returns the last item. |
225 | |
226 | my $last = $stuff->last_option; |
227 | print "$last\n"; # prints "boo" |
b77cfe61 |
228 | |
457dc4fb |
229 | =back |
230 | |
231 | =head1 BUGS |
232 | |
233 | All complex software has bugs lurking in it, and this module is no |
234 | exception. If you find a bug please either email me, or add the bug |
235 | to cpan-RT. |
236 | |
237 | =head1 AUTHOR |
238 | |
239 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
240 | |
241 | =head1 COPYRIGHT AND LICENSE |
242 | |
99c62fb8 |
243 | Copyright 2007-2008 by Infinity Interactive, Inc. |
457dc4fb |
244 | |
245 | L<http://www.iinteractive.com> |
246 | |
247 | This library is free software; you can redistribute it and/or modify |
248 | it under the same terms as Perl itself. |
249 | |
250 | =cut |