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