1 package Moose::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 Moose::AttributeHelpers::MethodProvider::List
116 use Moose::AttributeHelpers;
119 metaclass => 'Collection::List',
121 isa => 'ArrayRef[Str]',
122 default => sub { [] },
125 elements => 'all_options',
126 map => 'map_options',
127 grep => 'filter_options',
128 find => 'find_option',
129 first => 'first_option',
130 last => 'last_option',
132 join => 'join_options',
133 count => 'count_options',
134 empty => 'do_i_have_options',
135 sort => 'sorted_options',
144 This is a role which provides the method generators for
145 L<Moose::AttributeHelpers::Collection::List>.
155 =head1 PROVIDED METHODS
161 Returns the number of elements in the list.
164 $stuff->options(["foo", "bar", "baz", "boo"]);
166 my $count = $stuff->count_options;
167 print "$count\n"; # prints 4
171 If the list is populated, returns true. Otherwise, returns false.
173 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
177 This method accepts a subroutine reference as its argument. That sub
178 will receive each element of the list in turn. If it returns true for
179 an element, that element will be returned by the C<find> method.
181 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
182 print "$found\n"; # prints "bar"
186 This method accepts a subroutine reference as its argument. This
187 method returns every element for which that subroutine reference
188 returns a true value.
190 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
191 print "@found\n"; # prints "bar baz boo"
195 This method accepts a subroutine reference as its argument. The
196 subroutine will be executed for each element of the list. It is
197 expected to return a modified version of that element. The return
198 value of the method is a list of the modified options.
200 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
201 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
205 Sorts and returns the elements of the list.
207 You can provide an optional subroutine reference to sort with (as you
208 can with the core C<sort> function). However, instead of using C<$a>
209 and C<$b>, you will need to use C<$_[0]> and C<$_[1]> instead.
211 # ascending ASCIIbetical
212 my @sorted = $stuff->sort_options();
214 # Descending alphabetical order
215 my @sorted_options = $stuff->sort_options( sub { lc $_[1] cmp lc $_[0] } );
216 print "@sorted_options\n"; # prints "foo boo baz bar"
220 Returns all of the elements of the list
222 my @option = $stuff->all_options;
223 print "@options\n"; # prints "foo bar baz boo"
227 Joins every element of the list using the separator given as argument.
229 my $joined = $stuff->join_options( ':' );
230 print "$joined\n"; # prints "foo:bar:baz:boo"
234 Returns an element of the list by its index.
236 my $option = $stuff->get_option(1);
237 print "$option\n"; # prints "bar"
241 Returns the first element of the list.
243 my $first = $stuff->first_option;
244 print "$first\n"; # prints "foo"
248 Returns the last element of the list.
250 my $last = $stuff->last_option;
251 print "$last\n"; # prints "boo"
257 All complex software has bugs lurking in it, and this module is no
258 exception. If you find a bug please either email me, or add the bug
263 Stevan Little E<lt>stevan@iinteractive.comE<gt>
265 =head1 COPYRIGHT AND LICENSE
267 Copyright 2007-2009 by Infinity Interactive, Inc.
269 L<http://www.iinteractive.com>
271 This library is free software; you can redistribute it and/or modify
272 it under the same terms as Perl itself.