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) = @_; |
720fa35b |
9 | return sub { scalar @{$reader->($_[0])} }; |
457dc4fb |
10 | } |
11 | |
720fa35b |
12 | # Deprecated. The author was thinking backwardsly when this was written. |
457dc4fb |
13 | sub empty : method { |
14 | my ($attr, $reader, $writer) = @_; |
720fa35b |
15 | return sub { scalar @{$reader->($_[0])} ? 1 : 0 }; |
16 | } |
17 | |
18 | sub is_empty : method { |
19 | my ($attr, $reader, $writer) = @_; |
20 | return sub { @{ $reader->($_[0]) } == 0 }; |
21 | } |
22 | |
23 | sub has_items : method { |
24 | my ($attr, $reader, $writer) = @_; |
25 | return sub { @{ $reader->($_[0]) } > 0 }; |
457dc4fb |
26 | } |
27 | |
28 | sub find : method { |
29 | my ($attr, $reader, $writer) = @_; |
30 | return sub { |
31 | my ($instance, $predicate) = @_; |
32 | foreach my $val (@{$reader->($instance)}) { |
33 | return $val if $predicate->($val); |
34 | } |
35 | return; |
36 | }; |
37 | } |
38 | |
39 | sub map : method { |
40 | my ($attr, $reader, $writer) = @_; |
41 | return sub { |
42 | my ($instance, $f) = @_; |
43 | CORE::map { $f->($_) } @{$reader->($instance)} |
44 | }; |
45 | } |
46 | |
47 | sub grep : method { |
48 | my ($attr, $reader, $writer) = @_; |
49 | return sub { |
50 | my ($instance, $predicate) = @_; |
51 | CORE::grep { $predicate->($_) } @{$reader->($instance)} |
52 | }; |
53 | } |
54 | |
55 | 1; |
56 | |
57 | __END__ |
58 | |
59 | =pod |
60 | |
61 | =head1 NAME |
62 | |
63 | MooseX::AttributeHelpers::MethodProvider::List |
64 | |
65 | =head1 DESCRIPTION |
66 | |
67 | This is a role which provides the method generators for |
68 | L<MooseX::AttributeHelpers::Collection::List>. |
69 | |
720fa35b |
70 | =head1 PROVIDED METHODS |
457dc4fb |
71 | |
72 | =over 4 |
73 | |
720fa35b |
74 | =item B<count> |
457dc4fb |
75 | |
720fa35b |
76 | Returns the number of items in the list. |
457dc4fb |
77 | |
720fa35b |
78 | =item B<empty> |
457dc4fb |
79 | |
720fa35b |
80 | DEPRECATED. This was a misleading name for what it does (returns a boolean |
81 | indicating whether the list is NOT empty), but we're keeping it for backwards |
82 | compatibility. Do not use it in new code. Use is_empty or has_items instead, |
83 | depending on what you meant. |
457dc4fb |
84 | |
720fa35b |
85 | =item B<is_empty> |
457dc4fb |
86 | |
720fa35b |
87 | Returns a boolean which is true if and only if the list has no items in it. |
88 | |
89 | =item B<has_items> |
457dc4fb |
90 | |
720fa35b |
91 | Returns a boolean which is true if and only if the list has at least one item. |
92 | |
93 | =item B<find($predicate)> |
94 | |
95 | Returns the first item in the list that satisfies $predicate. |
457dc4fb |
96 | |
97 | =item B<grep> |
98 | |
720fa35b |
99 | L<perlfunc/grep> |
100 | |
457dc4fb |
101 | =item B<map> |
102 | |
720fa35b |
103 | L<perlfunc/map> |
104 | |
457dc4fb |
105 | =back |
106 | |
107 | =head1 BUGS |
108 | |
109 | All complex software has bugs lurking in it, and this module is no |
110 | exception. If you find a bug please either email me, or add the bug |
111 | to cpan-RT. |
112 | |
113 | =head1 AUTHOR |
114 | |
115 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
116 | |
117 | =head1 COPYRIGHT AND LICENSE |
118 | |
99c62fb8 |
119 | Copyright 2007-2008 by Infinity Interactive, Inc. |
457dc4fb |
120 | |
121 | L<http://www.iinteractive.com> |
122 | |
123 | This library is free software; you can redistribute it and/or modify |
124 | it under the same terms as Perl itself. |
125 | |
126 | =cut |