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 if $predicate && ref $predicate ne 'CODE';
49 CORE::sort { $predicate->($a, $b) } @{$reader->($instance)};
52 CORE::sort @{$reader->($instance)};
58 my ($attr, $reader, $writer) = @_;
60 my ($instance, $predicate) = @_;
61 CORE::grep { $predicate->($_) } @{$reader->($instance)}
65 sub elements : method {
66 my ($attr, $reader, $writer) = @_;
69 @{$reader->($instance)}
74 my ($attr, $reader, $writer) = @_;
76 my ($instance, $separator) = @_;
77 join $separator, @{$reader->($instance)}
82 my ($attr, $reader, $writer) = @_;
84 $reader->($_[0])->[$_[1]]
89 my ($attr, $reader, $writer) = @_;
96 my ($attr, $reader, $writer) = @_;
98 $reader->($_[0])->[-1]
110 MooseX::AttributeHelpers::MethodProvider::List
116 use MooseX::AttributeHelpers;
119 metaclass => 'Collection::List',
121 isa => 'ArrayRef[Str]',
122 default => sub { [] },
125 map => 'map_options',
126 grep => 'filter_options',
127 find => 'find_option',
128 first => 'first_option',
129 last => 'last_option',
131 join => 'join_options',
132 count => 'count_options',
133 empty => 'do_i_have_options',
134 sort => 'sorted_options',
144 This is a role which provides the method generators for
145 L<MooseX::AttributeHelpers::Collection::List>.
155 =head1 PROVIDED METHODS
160 Returns the number of elements of the list.
163 $stuff->options(["foo", "bar", "baz", "boo"]);
165 my $count = $stuff->count_options;
166 print "$count\n"; # prints 4
169 If the list is populated, returns true. Otherwise, returns false.
171 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
174 Returns the first element that returns true in the anonymous subroutine
177 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
178 print "$found\n"; # prints "bar"
181 Returns every element of the list that returns true in the anonymous
182 subroutine passed as argument.
184 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
185 print "@found\n"; # prints "bar baz boo"
188 Executes the anonymous subroutine given as argument sequentially
189 for each element of the list.
191 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
192 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
196 Returns a sorted list of the elements. You can optionally provide a
197 subroutine reference to sort with (as you can with the core C<sort>
198 function). However, instead of using C<$a> and C<$b>, you will need to
199 use C<$_[0]> and C<$_[1]> instead.
201 # ascending ASCIIbetical
202 my @sorted = $stuff->sort_options();
204 # Descending alphabetical order
205 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
206 print "@sorted_options\n"; # prints "foo boo baz bar"
209 Returns an element of the list by its index.
211 my $option = $stuff->get_option(1);
212 print "$option\n"; # prints "bar"
215 Joins every element of the list using the separator given as argument.
217 my $joined = $stuff->join_options( ':' );
218 print "$joined\n"; # prints "foo:bar:baz:boo"
221 Returns an element of the list by its index.
223 my $option = $stuff->get_option(1);
224 print "$option\n"; # prints "bar"
227 Returns the first element.
229 my $first = $stuff->first_option;
230 print "$first\n"; # prints "foo"
233 Returns the last item.
235 my $last = $stuff->last_option;
236 print "$last\n"; # prints "boo"
242 All complex software has bugs lurking in it, and this module is no
243 exception. If you find a bug please either email me, or add the bug
248 Stevan Little E<lt>stevan@iinteractive.comE<gt>
250 =head1 COPYRIGHT AND LICENSE
252 Copyright 2007-2008 by Infinity Interactive, Inc.
254 L<http://www.iinteractive.com>
256 This library is free software; you can redistribute it and/or modify
257 it under the same terms as Perl itself.