allow method => $coderef for a curries parameter
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers.pm
CommitLineData
22d869ff 1
2package MooseX::AttributeHelpers;
3
e8013350 4our $VERSION = '0.09';
22d869ff 5our $AUTHORITY = 'cpan:STEVAN';
6
8f7951c9 7use MooseX::AttributeHelpers::Meta::Method::Provided;
c43a2317 8use MooseX::AttributeHelpers::Meta::Method::Curried;
8f7951c9 9
22d869ff 10use MooseX::AttributeHelpers::Counter;
565fe238 11use MooseX::AttributeHelpers::Number;
190b1c02 12use MooseX::AttributeHelpers::String;
063fae5e 13use MooseX::AttributeHelpers::Bool;
457dc4fb 14use MooseX::AttributeHelpers::Collection::List;
22d869ff 15use MooseX::AttributeHelpers::Collection::Array;
16use MooseX::AttributeHelpers::Collection::Hash;
9a976497 17use MooseX::AttributeHelpers::Collection::ImmutableHash;
18use MooseX::AttributeHelpers::Collection::Bag;
22d869ff 19
201;
21
22__END__
23
24=pod
25
26=head1 NAME
27
e295d072 28MooseX::AttributeHelpers - Extend your attribute interfaces
fa4df2e6 29
22d869ff 30=head1 SYNOPSIS
31
e295d072 32 package MyClass;
33 use Moose;
5431dff2 34 use MooseX::AttributeHelpers;
e295d072 35
5431dff2 36 has 'mapping' => (
e295d072 37 metaclass => 'Collection::Hash',
38 is => 'rw',
5431dff2 39 isa => 'HashRef[Str]',
e295d072 40 default => sub { {} },
41 provides => {
42 exists => 'exists_in_mapping',
43 keys => 'ids_in_mapping',
44 get => 'get_mapping',
45 set => 'set_mapping',
46 },
c43a2317 47 curries => {
3656a0d7 48 set => { set_quantity => [ 'quantity' ] }
c43a2317 49 }
e295d072 50 );
51
696d4dc7 52
e295d072 53 # ...
54
55 my $obj = MyClass->new;
c43a2317 56 $obj->set_quantity(10); # quantity => 10
57 $obj->set_mapping(4, 'foo'); # 4 => 'foo'
58 $obj->set_mapping(5, 'bar'); # 5 => 'bar'
59 $obj->set_mapping(6, 'baz'); # 6 => 'baz'
e295d072 60
696d4dc7 61
e295d072 62 # prints 'bar'
63 print $obj->get_mapping(5) if $obj->exists_in_mapping(5);
64
65 # prints '4, 5, 6'
66 print join ', ', $obj->ids_in_mapping;
67
22d869ff 68=head1 DESCRIPTION
69
e295d072 70While L<Moose> attributes provide you with a way to name your accessors,
71readers, writers, clearers and predicates, this library provides commonly
72used attribute helper methods for more specific types of data.
73
74As seen in the L</SYNOPSIS>, you specify the extension via the
75C<metaclass> parameter. Available meta classes are:
76
c43a2317 77=head1 PARAMETERS
78
79=head2 provides
80
81This points to a hashref that uses C<provider> for the keys and
3656a0d7 82C<method> for the values. The method will be added to
c43a2317 83the object itself and do what you want.
84
85=head2 curries
86
87This points to a hashref that uses C<provider> for the keys and
696d4dc7 88has two choices for the value:
89
90You can supply C<< {method => [ @args ]} >> for the values. The method will be
91added to the object itself (always using C<@args> as the beginning arguments).
92
93Another approach to curry a method provider is to supply a coderef instead of an
94arrayref. The code ref takes C<$self>, C<$body>, and any additional arguments
95passed to the final method.
96
97 # ...
98
99 curries => {
100 grep => {
101 times_with_day => sub {
102 my ($self, $body, $datetime) = @_;
103 $body->($self, sub { $_->ymd eq $datetime->ymd });
104 }
105 }
106 }
107
108 # ...
109
110 $obj->times_with_day(DateTime->now); # takes datetime argument, checks day
111
c43a2317 112
113=head1 METHOD PROVIDERS
114
e295d072 115=over
116
117=item L<Number|MooseX::AttributeHelpers::Number>
118
119Common numerical operations.
120
121=item L<Counter|MooseX::AttributeHelpers::Counter>
122
123Methods for incrementing and decrementing a counter attribute.
124
ebef5ea9 125=item L<Counter|MooseX::AttributeHelpers::Bool>
126
127Common methods for boolean values.
128
e295d072 129=item L<Collection::Hash|MooseX::AttributeHelpers::Collection::Hash>
130
131Common methods for hash references.
132
133=item L<Collection::Array|MooseX::AttributeHelpers::Collection::Array>
134
135Common methods for array references.
136
72a41843 137=item L<Collection::List|MooseX::AttributeHelpers::Collection::List>
457dc4fb 138
139Common list methods for array references.
140
e295d072 141=back
142
5431dff2 143=head1 CAVEAT
144
145This is an early release of this module. Right now it is in great need
146of documentation and tests in the test suite. However, we have used this
c91a1347 147module to great success at C<$work> where it has been tested very thoroughly
5431dff2 148and deployed into a major production site.
149
150I plan on getting better docs and tests in the next few releases, but until
151then please refer to the few tests we do have and feel free email and/or
152message me on irc.perl.org if you have any questions.
153
154=head1 TODO
155
156We need tests and docs badly.
157
22d869ff 158=head1 BUGS
159
160All complex software has bugs lurking in it, and this module is no
161exception. If you find a bug please either email me, or add the bug
162to cpan-RT.
163
164=head1 AUTHOR
165
166Stevan Little E<lt>stevan@iinteractive.comE<gt>
167
5431dff2 168B<with contributions from:>
169
170Robert (rlb3) Boone
171
e8013350 172Paul (frodwith) Driver
173
174Shawn (Sartak) Moore
175
5431dff2 176Chris (perigrin) Prather
177
178Robert (phaylon) Sedlacek
179
38abf787 180Tom (dec) Lanyon
181
eca30395 182Yuval Kogman
183
ebef5ea9 184Jason May
185
22d869ff 186=head1 COPYRIGHT AND LICENSE
187
eca30395 188Copyright 2007, 2008 by Infinity Interactive, Inc.
22d869ff 189
190L<http://www.iinteractive.com>
191
192This library is free software; you can redistribute it and/or modify
193it under the same terms as Perl itself.
194
e295d072 195=cut