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 CORE::grep { $predicate->($_) } @{$reader->($instance)}
49 sub elements : method {
50 my ($attr, $reader, $writer) = @_;
53 @{$reader->($instance)}
58 my ($attr, $reader, $writer) = @_;
60 my ($instance, $separator) = @_;
61 join $separator, @{$reader->($instance)}
66 my ($attr, $reader, $writer) = @_;
68 $reader->($_[0])->[$_[1]]
73 my ($attr, $reader, $writer) = @_;
80 my ($attr, $reader, $writer) = @_;
82 $reader->($_[0])->[-1]
94 MooseX::AttributeHelpers::MethodProvider::List
100 use MooseX::AttributeHelpers;
103 metaclass => 'Collection::List',
105 isa => 'ArrayRef[Str]',
106 default => sub { [] },
109 map => 'map_options',
110 grep => 'filter_options',
111 find => 'find_option',
112 first => 'first_option',
113 last => 'last_option',
115 join => 'join_options',
116 count => 'count_options',
117 empty => 'do_i_have_options',
127 This is a role which provides the method generators for
128 L<MooseX::AttributeHelpers::Collection::List>.
138 =head1 PROVIDED METHODS
143 Returns the number of elements of the list.
146 $stuff->options(["foo", "bar", "baz", "boo"]);
148 my $count = $stuff->count_options;
149 print "$count\n"; # prints 4
152 If the list is populated, returns true. Otherwise, returns false.
154 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
157 Returns the first element that returns true in the anonymous subroutine
160 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
161 print "$found\n"; # prints "bar"
164 Returns every elemnt of the list that returns true in the anonymous
165 subroutine passed as argument.
167 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
168 print "@found\n"; # prints "bar baz boo"
171 Executes the anonymouse subroutine given as argument sequentially
172 for each element of the list.
174 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
175 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
178 Returns an element of the list by its index.
180 my $option = $stuff->get_option(1);
181 print "$option\n"; # prints "bar"
184 Joins every elemnt of the list using the separator given as argument.
186 my $joined = $stuff->join_options( ':' );
187 print "$joined\n"; # prints "foo:bar:baz:boo"
190 Returns an element of the list by its index.
192 my $option = $stuff->get_option(1);
193 print "$option\n"; # prints "bar"
196 Returns the first element.
198 my $first = $stuff->first_option;
199 print "$first\n"; # prints "foo"
202 Returns the last item.
204 my $last = $stuff->last_option;
205 print "$last\n"; # prints "boo"
211 All complex software has bugs lurking in it, and this module is no
212 exception. If you find a bug please either email me, or add the bug
217 Stevan Little E<lt>stevan@iinteractive.comE<gt>
219 =head1 COPYRIGHT AND LICENSE
221 Copyright 2007-2008 by Infinity Interactive, Inc.
223 L<http://www.iinteractive.com>
225 This library is free software; you can redistribute it and/or modify
226 it under the same terms as Perl itself.