copy in and rename AttributeHelpers
[gitmo/Moose.git] / lib / Moose / AttributeHelpers.pm
1
2 package Moose::AttributeHelpers;
3
4 our $VERSION   = '0.19';
5 $VERSION = eval $VERSION;
6 our $AUTHORITY = 'cpan:STEVAN';
7
8 use Moose 0.56 ();
9
10 use Moose::AttributeHelpers::Meta::Method::Provided;
11 use Moose::AttributeHelpers::Meta::Method::Curried;
12
13 use Moose::AttributeHelpers::Trait::Bool;
14 use Moose::AttributeHelpers::Trait::Counter;
15 use Moose::AttributeHelpers::Trait::Number;
16 use Moose::AttributeHelpers::Trait::String;
17 use Moose::AttributeHelpers::Trait::Collection::List;
18 use Moose::AttributeHelpers::Trait::Collection::Array;
19 use Moose::AttributeHelpers::Trait::Collection::Hash;
20 use Moose::AttributeHelpers::Trait::Collection::ImmutableHash;
21 use Moose::AttributeHelpers::Trait::Collection::Bag;
22
23 use Moose::AttributeHelpers::Counter;
24 use Moose::AttributeHelpers::Number;
25 use Moose::AttributeHelpers::String;
26 use Moose::AttributeHelpers::Bool;
27 use Moose::AttributeHelpers::Collection::List;
28 use Moose::AttributeHelpers::Collection::Array;
29 use Moose::AttributeHelpers::Collection::Hash;
30 use Moose::AttributeHelpers::Collection::ImmutableHash;
31 use Moose::AttributeHelpers::Collection::Bag;
32
33 1;
34
35 __END__
36
37 =pod
38
39 =head1 NAME
40
41 Moose::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
81 While L<Moose> attributes provide you with a way to name your accessors,
82 readers, writers, clearers and predicates, this library provides commonly
83 used attribute helper methods for more specific types of data.
84
85 As seen in the L</SYNOPSIS>, you specify the extension via the
86 C<metaclass> parameter. Available meta classes are:
87
88 =head1 PARAMETERS
89
90 =head2 handles
91
92 This points to a hashref that uses C<method> for the keys and
93 C<stuff> for the values.  The method will be added to
94 the object itself and do what you want.
95
96 =head2 curries
97
98 This points to a hashref that uses C<provider> for the keys and
99 has two choices for the value:
100
101 You can supply C<< {method => [ @args ]} >> for the values.  The method will be
102 added to the object itself (always using C<@args> as the beginning arguments).
103
104 Another approach to curry a method provider is to supply a coderef instead of an
105 arrayref. The code ref takes C<$self>, C<$body>, and any additional arguments
106 passed 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
130 Common numerical operations.
131
132 =item L<String|Moose::AttributeHelpers::String>
133
134 Common methods for string operations.
135
136 =item L<Counter|Moose::AttributeHelpers::Counter>
137
138 Methods for incrementing and decrementing a counter attribute.
139
140 =item L<Bool|Moose::AttributeHelpers::Bool>
141
142 Common methods for boolean values.
143
144 =item L<Collection::Hash|Moose::AttributeHelpers::Collection::Hash>
145
146 Common methods for hash references.
147
148 =item L<Collection::ImmutableHash|Moose::AttributeHelpers::Collection::ImmutableHash>
149
150 Common methods for inspecting hash references.
151
152 =item L<Collection::Array|Moose::AttributeHelpers::Collection::Array>
153
154 Common methods for array references.
155
156 =item L<Collection::List|Moose::AttributeHelpers::Collection::List>
157
158 Common list methods for array references.
159
160 =back
161
162 =head1 CAVEAT
163
164 This is an early release of this module. Right now it is in great need
165 of documentation and tests in the test suite. However, we have used this
166 module to great success at C<$work> where it has been tested very thoroughly
167 and deployed into a major production site.
168
169 I plan on getting better docs and tests in the next few releases, but until
170 then please refer to the few tests we do have and feel free email and/or
171 message me on irc.perl.org if you have any questions.
172
173 =head1 TODO
174
175 We need tests and docs badly.
176
177 =head1 BUGS
178
179 All complex software has bugs lurking in it, and this module is no
180 exception. If you find a bug please either email me, or add the bug
181 to cpan-RT.
182
183 =head1 AUTHOR
184
185 Stevan Little E<lt>stevan@iinteractive.comE<gt>
186
187 B<with contributions from:>
188
189 Robert (rlb3) Boone
190
191 Paul (frodwith) Driver
192
193 Shawn (Sartak) Moore
194
195 Chris (perigrin) Prather
196
197 Robert (phaylon) Sedlacek
198
199 Tom (dec) Lanyon
200
201 Yuval Kogman
202
203 Jason May
204
205 Cory (gphat) Watson
206
207 Florian (rafl) Ragwitz
208
209 Evan Carroll
210
211 Jesse (doy) Luehrs
212
213 =head1 COPYRIGHT AND LICENSE
214
215 Copyright 2007-2009 by Infinity Interactive, Inc.
216
217 L<http://www.iinteractive.com>
218
219 This library is free software; you can redistribute it and/or modify
220 it under the same terms as Perl itself.
221
222 =cut