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