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