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