add contains method to MethodProvider/List
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / List.pm
CommitLineData
457dc4fb 1package MooseX::AttributeHelpers::MethodProvider::List;
2use Moose::Role;
3
7a93b96e 4our $VERSION = '0.14';
38430345 5$VERSION = eval $VERSION;
457dc4fb 6our $AUTHORITY = 'cpan:STEVAN';
7
8sub count : method {
9 my ($attr, $reader, $writer) = @_;
10 return sub {
11 scalar @{$reader->($_[0])}
12 };
13}
14
15sub empty : method {
16 my ($attr, $reader, $writer) = @_;
17 return sub {
facf2175 18 scalar @{$reader->($_[0])} ? 1 : 0
457dc4fb 19 };
20}
21
22sub 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
33sub map : method {
34 my ($attr, $reader, $writer) = @_;
35 return sub {
36 my ($instance, $f) = @_;
37 CORE::map { $f->($_) } @{$reader->($instance)}
38 };
39}
40
41sub grep : method {
42 my ($attr, $reader, $writer) = @_;
43 return sub {
44 my ($instance, $predicate) = @_;
45 CORE::grep { $predicate->($_) } @{$reader->($instance)}
46 };
47}
48
6f60a71e 49sub elements : method {
50 my ($attr, $reader, $writer) = @_;
51 return sub {
c80e1668 52 my ($instance) = @_;
6f60a71e 53 @{$reader->($instance)}
54 };
55}
56
d57bba34 57sub 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 68sub join : method {
69 my ($attr, $reader, $writer) = @_;
70 return sub {
71 my ($instance, $separator) = @_;
72 join $separator, @{$reader->($instance)}
73 };
74}
75
b77cfe61 76sub get : method {
77 my ($attr, $reader, $writer) = @_;
78 return sub {
79 $reader->($_[0])->[$_[1]]
80 };
81}
82
83sub first : method {
84 my ($attr, $reader, $writer) = @_;
85 return sub {
86 $reader->($_[0])->[0]
87 };
88}
89
90sub last : method {
91 my ($attr, $reader, $writer) = @_;
92 return sub {
93 $reader->($_[0])->[-1]
94 };
95}
96
457dc4fb 971;
98
99__END__
100
101=pod
102
103=head1 NAME
104
105MooseX::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
139This is a role which provides the method generators for
140L<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 155Returns 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>
164If 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 169Returns the first element that returns true in the anonymous subroutine
170passed 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 176Returns every element of the list that returns true in the anonymous
1b5dc533 177subroutine 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 183Executes the anonymous subroutine given as argument sequentially
1b5dc533 184for each element of the list.
185
186my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
187print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
457dc4fb 188
6f60a71e 189=item B<elements>
1b5dc533 190Returns 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>
196Returns a true value if the list contains the passed value, otherwise returns
197a false value. The return value is actually the number of times the passed
198value 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
203Note that this works even when looking for the undefined value.
204
654096bc 205=item B<join>
b290a465 206Joins 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 212Returns 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 218Returns the first element.
219
220 my $first = $stuff->first_option;
221 print "$first\n"; # prints "foo"
b77cfe61 222
223=item B<last>
1b5dc533 224Returns 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
233All complex software has bugs lurking in it, and this module is no
234exception. If you find a bug please either email me, or add the bug
235to cpan-RT.
236
237=head1 AUTHOR
238
239Stevan Little E<lt>stevan@iinteractive.comE<gt>
240
241=head1 COPYRIGHT AND LICENSE
242
99c62fb8 243Copyright 2007-2008 by Infinity Interactive, Inc.
457dc4fb 244
245L<http://www.iinteractive.com>
246
247This library is free software; you can redistribute it and/or modify
248it under the same terms as Perl itself.
249
250=cut