0.02
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / List.pm
CommitLineData
457dc4fb 1package MooseX::AttributeHelpers::MethodProvider::List;
2use Moose::Role;
3
c91a1347 4our $VERSION = '0.01';
457dc4fb 5our $AUTHORITY = 'cpan:STEVAN';
6
7sub count : method {
8 my ($attr, $reader, $writer) = @_;
9 return sub {
10 scalar @{$reader->($_[0])}
11 };
12}
13
14sub empty : method {
15 my ($attr, $reader, $writer) = @_;
16 return sub {
17 scalar @{$reader->($_[0])} ? 1 : 0
18 };
19}
20
21sub find : method {
22 my ($attr, $reader, $writer) = @_;
23 return sub {
24 my ($instance, $predicate) = @_;
25 foreach my $val (@{$reader->($instance)}) {
26 return $val if $predicate->($val);
27 }
28 return;
29 };
30}
31
32sub map : method {
33 my ($attr, $reader, $writer) = @_;
34 return sub {
35 my ($instance, $f) = @_;
36 CORE::map { $f->($_) } @{$reader->($instance)}
37 };
38}
39
40sub grep : method {
41 my ($attr, $reader, $writer) = @_;
42 return sub {
43 my ($instance, $predicate) = @_;
44 CORE::grep { $predicate->($_) } @{$reader->($instance)}
45 };
46}
47
481;
49
50__END__
51
52=pod
53
54=head1 NAME
55
56MooseX::AttributeHelpers::MethodProvider::List
57
58=head1 DESCRIPTION
59
60This is a role which provides the method generators for
61L<MooseX::AttributeHelpers::Collection::List>.
62
63=head1 METHODS
64
65=over 4
66
67=item B<meta>
68
69=back
70
71=head1 PROVIDED METHODS
72
73=over 4
74
75=item B<count>
76
77=item B<empty>
78
79=item B<find>
80
81=item B<grep>
82
83=item B<map>
84
85=back
86
87=head1 BUGS
88
89All complex software has bugs lurking in it, and this module is no
90exception. If you find a bug please either email me, or add the bug
91to cpan-RT.
92
93=head1 AUTHOR
94
95Stevan Little E<lt>stevan@iinteractive.comE<gt>
96
97=head1 COPYRIGHT AND LICENSE
98
99Copyright 2007 by Infinity Interactive, Inc.
100
101L<http://www.iinteractive.com>
102
103This library is free software; you can redistribute it and/or modify
104it under the same terms as Perl itself.
105
106=cut