bb3b5d5eb0f1807c26aa1bae76180ff881a493f4
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / MethodProvider / List.pm
1 package MooseX::AttributeHelpers::MethodProvider::List;
2 use Moose::Role;
3
4 our $VERSION   = '0.01';
5 our $AUTHORITY = 'cpan:STEVAN';
6  
7 sub count : method {
8     my ($attr, $reader, $writer) = @_;
9     return sub { scalar @{$reader->($_[0])} };        
10 }
11
12 # Deprecated.  The author was thinking backwardsly when this was written.
13 sub empty : method {
14     my ($attr, $reader, $writer) = @_;
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 };
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
70 =head1 PROVIDED METHODS
71
72 =over 4
73
74 =item B<count>
75
76 Returns the number of items in the list.
77
78 =item B<empty>
79
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.
84
85 =item B<is_empty>
86
87 Returns a boolean which is true if and only if the list has no items in it.
88
89 =item B<has_items>
90
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.
96
97 =item B<grep>
98
99 L<perlfunc/grep>
100
101 =item B<map>
102
103 L<perlfunc/map>
104
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
119 Copyright 2007-2008 by Infinity Interactive, Inc.
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