1 package MooseX::AttributeHelpers::MethodProvider::List;
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
9 my ($attr, $reader, $writer) = @_;
11 scalar @{$reader->($_[0])}
16 my ($attr, $reader, $writer) = @_;
18 scalar @{$reader->($_[0])} ? 1 : 0
23 my ($attr, $reader, $writer) = @_;
25 my ($instance, $predicate) = @_;
26 foreach my $val (@{$reader->($instance)}) {
27 return $val if $predicate->($val);
34 my ($attr, $reader, $writer) = @_;
36 my ($instance, $f) = @_;
37 CORE::map { $f->($_) } @{$reader->($instance)}
42 my ($attr, $reader, $writer) = @_;
44 my ($instance, $predicate) = @_;
45 die "Argument must be a code reference"
46 unless ref $predicate eq "CODE";
47 CORE::sort { $predicate->($a, $b) } @{$reader->($instance)};
52 my ($attr, $reader, $writer) = @_;
54 my ($instance, $predicate) = @_;
55 CORE::grep { $predicate->($_) } @{$reader->($instance)}
59 sub elements : method {
60 my ($attr, $reader, $writer) = @_;
63 @{$reader->($instance)}
68 my ($attr, $reader, $writer) = @_;
70 my ($instance, $separator) = @_;
71 join $separator, @{$reader->($instance)}
76 my ($attr, $reader, $writer) = @_;
78 $reader->($_[0])->[$_[1]]
83 my ($attr, $reader, $writer) = @_;
90 my ($attr, $reader, $writer) = @_;
92 $reader->($_[0])->[-1]
104 MooseX::AttributeHelpers::MethodProvider::List
110 use MooseX::AttributeHelpers;
113 metaclass => 'Collection::List',
115 isa => 'ArrayRef[Str]',
116 default => sub { [] },
119 map => 'map_options',
120 grep => 'filter_options',
121 find => 'find_option',
122 first => 'first_option',
123 last => 'last_option',
125 join => 'join_options',
126 count => 'count_options',
127 empty => 'do_i_have_options',
128 sort => 'sort_options',
138 This is a role which provides the method generators for
139 L<MooseX::AttributeHelpers::Collection::List>.
149 =head1 PROVIDED METHODS
154 Returns the number of elements of the list.
157 $stuff->options(["foo", "bar", "baz", "boo"]);
159 my $count = $stuff->count_options;
160 print "$count\n"; # prints 4
163 If the list is populated, returns true. Otherwise, returns false.
165 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
168 Returns the first element that returns true in the anonymous subroutine
171 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
172 print "$found\n"; # prints "bar"
175 Returns every element of the list that returns true in the anonymous
176 subroutine passed as argument.
178 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
179 print "@found\n"; # prints "bar baz boo"
182 Executes the anonymous subroutine given as argument sequentially
183 for each element of the list.
185 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
186 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
189 Returns a sorted list of the elements, using the anonymous subroutine
192 This subroutine should perform a comparison between the two arguments passed
193 to it, and return a numeric list with the results of such comparison:
195 # Descending alphabetical order
196 my @sorted_options = $stuff->sort_options( sub { $_[1] cmp $_[0] } );
197 print "@sorted_options\n"; # prints "foo boo baz bar"
200 Returns an element of the list by its index.
202 my $option = $stuff->get_option(1);
203 print "$option\n"; # prints "bar"
206 Joins every element of the list using the separator given as argument.
208 my $joined = $stuff->join_options( ':' );
209 print "$joined\n"; # prints "foo:bar:baz:boo"
212 Returns an element of the list by its index.
214 my $option = $stuff->get_option(1);
215 print "$option\n"; # prints "bar"
218 Returns the first element.
220 my $first = $stuff->first_option;
221 print "$first\n"; # prints "foo"
224 Returns the last item.
226 my $last = $stuff->last_option;
227 print "$last\n"; # prints "boo"
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
239 Stevan Little E<lt>stevan@iinteractive.comE<gt>
241 =head1 COPYRIGHT AND LICENSE
243 Copyright 2007-2008 by Infinity Interactive, Inc.
245 L<http://www.iinteractive.com>
247 This library is free software; you can redistribute it and/or modify
248 it under the same terms as Perl itself.