Commit | Line | Data |
22d869ff |
1 | |
2 | package MooseX::AttributeHelpers; |
3 | |
7a93b96e |
4 | our $VERSION = '0.14'; |
38430345 |
5 | $VERSION = eval $VERSION; |
22d869ff |
6 | our $AUTHORITY = 'cpan:STEVAN'; |
7 | |
2dc1c4f7 |
8 | use Moose 0.56 (); |
9 | |
8f7951c9 |
10 | use MooseX::AttributeHelpers::Meta::Method::Provided; |
c43a2317 |
11 | use MooseX::AttributeHelpers::Meta::Method::Curried; |
8f7951c9 |
12 | |
22d869ff |
13 | use MooseX::AttributeHelpers::Counter; |
565fe238 |
14 | use MooseX::AttributeHelpers::Number; |
190b1c02 |
15 | use MooseX::AttributeHelpers::String; |
063fae5e |
16 | use MooseX::AttributeHelpers::Bool; |
457dc4fb |
17 | use MooseX::AttributeHelpers::Collection::List; |
22d869ff |
18 | use MooseX::AttributeHelpers::Collection::Array; |
19 | use MooseX::AttributeHelpers::Collection::Hash; |
9a976497 |
20 | use MooseX::AttributeHelpers::Collection::ImmutableHash; |
21 | use MooseX::AttributeHelpers::Collection::Bag; |
22d869ff |
22 | |
23 | 1; |
24 | |
25 | __END__ |
26 | |
27 | =pod |
28 | |
29 | =head1 NAME |
30 | |
e295d072 |
31 | MooseX::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 |
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 | |
c43a2317 |
80 | =head1 PARAMETERS |
81 | |
82 | =head2 provides |
83 | |
84 | This points to a hashref that uses C<provider> for the keys and |
3656a0d7 |
85 | C<method> for the values. The method will be added to |
c43a2317 |
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 |
696d4dc7 |
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 | |
c43a2317 |
115 | |
116 | =head1 METHOD PROVIDERS |
117 | |
e295d072 |
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 | |
c9fd9db5 |
128 | =item L<Bool|MooseX::AttributeHelpers::Bool> |
ebef5ea9 |
129 | |
130 | Common methods for boolean values. |
131 | |
e295d072 |
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 | |
72a41843 |
140 | =item L<Collection::List|MooseX::AttributeHelpers::Collection::List> |
457dc4fb |
141 | |
142 | Common list methods for array references. |
143 | |
e295d072 |
144 | =back |
145 | |
5431dff2 |
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 |
c91a1347 |
150 | module to great success at C<$work> where it has been tested very thoroughly |
5431dff2 |
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 | |
22d869ff |
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 | |
5431dff2 |
171 | B<with contributions from:> |
172 | |
173 | Robert (rlb3) Boone |
174 | |
e8013350 |
175 | Paul (frodwith) Driver |
176 | |
177 | Shawn (Sartak) Moore |
178 | |
5431dff2 |
179 | Chris (perigrin) Prather |
180 | |
181 | Robert (phaylon) Sedlacek |
182 | |
38abf787 |
183 | Tom (dec) Lanyon |
184 | |
eca30395 |
185 | Yuval Kogman |
186 | |
ebef5ea9 |
187 | Jason May |
188 | |
491b1b07 |
189 | Cory (gphat) Watson |
190 | |
1245fd8e |
191 | Florian (rafl) Ragwitz |
192 | |
22d869ff |
193 | =head1 COPYRIGHT AND LICENSE |
194 | |
eca30395 |
195 | Copyright 2007, 2008 by Infinity Interactive, Inc. |
22d869ff |
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 | |
e295d072 |
202 | =cut |