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