Implemented List::sort and Array::sort_in_place. Added basic tests and pod.
[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
80894c0a 41sub sort : method {
42 my ($attr, $reader, $writer) = @_;
43 return sub {
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)};
48 };
49}
50
457dc4fb 51sub grep : method {
52 my ($attr, $reader, $writer) = @_;
53 return sub {
54 my ($instance, $predicate) = @_;
55 CORE::grep { $predicate->($_) } @{$reader->($instance)}
56 };
57}
58
6f60a71e 59sub elements : method {
60 my ($attr, $reader, $writer) = @_;
61 return sub {
c80e1668 62 my ($instance) = @_;
6f60a71e 63 @{$reader->($instance)}
64 };
65}
66
654096bc 67sub join : method {
68 my ($attr, $reader, $writer) = @_;
69 return sub {
70 my ($instance, $separator) = @_;
71 join $separator, @{$reader->($instance)}
72 };
73}
74
b77cfe61 75sub get : method {
76 my ($attr, $reader, $writer) = @_;
77 return sub {
78 $reader->($_[0])->[$_[1]]
79 };
80}
81
82sub first : method {
83 my ($attr, $reader, $writer) = @_;
84 return sub {
85 $reader->($_[0])->[0]
86 };
87}
88
89sub last : method {
90 my ($attr, $reader, $writer) = @_;
91 return sub {
92 $reader->($_[0])->[-1]
93 };
94}
95
457dc4fb 961;
97
98__END__
99
100=pod
101
102=head1 NAME
103
104MooseX::AttributeHelpers::MethodProvider::List
1b5dc533 105
80894c0a 106=head1 SYNOPSIS
1b5dc533 107
108 package Stuff;
109 use Moose;
110 use MooseX::AttributeHelpers;
111
112 has 'options' => (
113 metaclass => 'Collection::List',
114 is => 'rw',
115 isa => 'ArrayRef[Str]',
116 default => sub { [] },
117 auto_deref => 1,
118 provides => {
119 map => 'map_options',
120 grep => 'filter_options',
121 find => 'find_option',
122 first => 'first_option',
123 last => 'last_option',
124 get => 'get_option',
125 join => 'join_options',
126 count => 'count_options',
127 empty => 'do_i_have_options',
80894c0a 128 sort => 'sort_options',
1b5dc533 129
130 }
131 );
132
133 no Moose;
134 1;
135
457dc4fb 136=head1 DESCRIPTION
137
138This is a role which provides the method generators for
139L<MooseX::AttributeHelpers::Collection::List>.
140
141=head1 METHODS
142
143=over 4
144
145=item B<meta>
146
147=back
148
149=head1 PROVIDED METHODS
150
151=over 4
152
153=item B<count>
1b5dc533 154Returns the number of elements of the list.
155
156 $stuff = Stuff->new;
157 $stuff->options(["foo", "bar", "baz", "boo"]);
158
159 my $count = $stuff->count_options;
160 print "$count\n"; # prints 4
161
162=item B<empty>
163If the list is populated, returns true. Otherwise, returns false.
457dc4fb 164
1b5dc533 165 $stuff->do_i_have_options ? print "Good boy.\n" : die "No options!\n" ;
457dc4fb 166
167=item B<find>
1b5dc533 168Returns the first element that returns true in the anonymous subroutine
169passed as argument.
170
171 my $found = $stuff->find_option( sub { $_[0] =~ /^b/ } );
172 print "$found\n"; # prints "bar"
457dc4fb 173
174=item B<grep>
b290a465 175Returns every element of the list that returns true in the anonymous
1b5dc533 176subroutine passed as argument.
177
178 my @found = $stuff->filter_options( sub { $_[0] =~ /^b/ } );
179 print "@found\n"; # prints "bar baz boo"
457dc4fb 180
181=item B<map>
b290a465 182Executes the anonymous subroutine given as argument sequentially
1b5dc533 183for each element of the list.
184
80894c0a 185 my @mod_options = $stuff->map_options( sub { $_[0] . "-tag" } );
186 print "@mod_options\n"; # prints "foo-tag bar-tag baz-tag boo-tag"
187
188=item B<sort>
189Returns a sorted list of the elements, using the anonymous subroutine
190given as argument.
191
192This subroutine should perform a comparison between the two arguments passed
193to it, and return a numeric list with the results of such comparison:
194
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"
457dc4fb 198
6f60a71e 199=item B<elements>
1b5dc533 200Returns an element of the list by its index.
201
202 my $option = $stuff->get_option(1);
203 print "$option\n"; # prints "bar"
6f60a71e 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