Commit | Line | Data |
457dc4fb |
1 | package MooseX::AttributeHelpers::MethodProvider::List; |
2 | use Moose::Role; |
3 | |
c91a1347 |
4 | our $VERSION = '0.01'; |
457dc4fb |
5 | our $AUTHORITY = 'cpan:STEVAN'; |
6 | |
7 | sub count : method { |
8 | my ($attr, $reader, $writer) = @_; |
9 | return sub { |
10 | scalar @{$reader->($_[0])} |
11 | }; |
12 | } |
13 | |
14 | sub empty : method { |
15 | my ($attr, $reader, $writer) = @_; |
16 | return sub { |
17 | scalar @{$reader->($_[0])} ? 1 : 0 |
18 | }; |
19 | } |
20 | |
21 | sub 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 | |
32 | sub map : method { |
33 | my ($attr, $reader, $writer) = @_; |
34 | return sub { |
35 | my ($instance, $f) = @_; |
36 | CORE::map { $f->($_) } @{$reader->($instance)} |
37 | }; |
38 | } |
39 | |
40 | sub grep : method { |
41 | my ($attr, $reader, $writer) = @_; |
42 | return sub { |
43 | my ($instance, $predicate) = @_; |
44 | CORE::grep { $predicate->($_) } @{$reader->($instance)} |
45 | }; |
46 | } |
47 | |
48 | 1; |
49 | |
50 | __END__ |
51 | |
52 | =pod |
53 | |
54 | =head1 NAME |
55 | |
56 | MooseX::AttributeHelpers::MethodProvider::List |
57 | |
58 | =head1 DESCRIPTION |
59 | |
60 | This is a role which provides the method generators for |
61 | L<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 | |
89 | All complex software has bugs lurking in it, and this module is no |
90 | exception. If you find a bug please either email me, or add the bug |
91 | to cpan-RT. |
92 | |
93 | =head1 AUTHOR |
94 | |
95 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
96 | |
97 | =head1 COPYRIGHT AND LICENSE |
98 | |
99 | Copyright 2007 by Infinity Interactive, Inc. |
100 | |
101 | L<http://www.iinteractive.com> |
102 | |
103 | This library is free software; you can redistribute it and/or modify |
104 | it under the same terms as Perl itself. |
105 | |
106 | =cut |