Commit | Line | Data |
d26633fc |
1 | |
2 | package MooseX::AttributeHelpers::Base; |
3 | use Moose; |
8ba40fb0 |
4 | use Moose::Util::TypeConstraints; |
d26633fc |
5 | |
18d43c2c |
6 | our $VERSION = '0.13'; |
d26633fc |
7 | our $AUTHORITY = 'cpan:STEVAN'; |
8 | |
9 | extends 'Moose::Meta::Attribute'; |
10 | |
8e3fab6d |
11 | # this is the method map you define ... |
d26633fc |
12 | has 'provides' => ( |
9810162d |
13 | is => 'ro', |
14 | isa => 'HashRef', |
15 | default => sub {{}} |
d26633fc |
16 | ); |
17 | |
c43a2317 |
18 | has 'curries' => ( |
19 | is => 'ro', |
20 | isa => 'HashRef', |
21 | default => sub {{}} |
22 | ); |
23 | |
999f34a9 |
24 | # these next two are the possible methods |
8e3fab6d |
25 | # you can use in the 'provides' map. |
26 | |
999f34a9 |
27 | # provide a Class or Role which we can |
28 | # collect the method providers from |
8e3fab6d |
29 | has 'method_provider' => ( |
30 | is => 'ro', |
31 | isa => 'ClassName', |
32 | predicate => 'has_method_provider', |
33 | ); |
34 | |
35 | # or you can provide a HASH ref of anon subs |
36 | # yourself. This will also collect and store |
999f34a9 |
37 | # the methods from a method_provider as well |
8e3fab6d |
38 | has 'method_constructors' => ( |
39 | is => 'ro', |
40 | isa => 'HashRef', |
41 | lazy => 1, |
42 | default => sub { |
43 | my $self = shift; |
44 | return +{} unless $self->has_method_provider; |
45 | # or grab them from the role/class |
46 | my $method_provider = $self->method_provider->meta; |
47 | return +{ |
999f34a9 |
48 | map { |
8e3fab6d |
49 | $_ => $method_provider->get_method($_) |
50 | } $method_provider->get_method_list |
999f34a9 |
51 | }; |
e295d072 |
52 | }, |
8e3fab6d |
53 | ); |
54 | |
999f34a9 |
55 | # extend the parents stuff to make sure |
8881a8d3 |
56 | # certain bits are now required ... |
4e6acc23 |
57 | has '+default' => (required => 1); |
d26633fc |
58 | has '+type_constraint' => (required => 1); |
59 | |
8ba40fb0 |
60 | ## Methods called prior to instantiation |
61 | |
62 | sub helper_type { () } |
d26633fc |
63 | |
8ba40fb0 |
64 | sub process_options_for_provides { |
d26633fc |
65 | my ($self, $options) = @_; |
999f34a9 |
66 | |
8ba40fb0 |
67 | if (my $type = $self->helper_type) { |
68 | (exists $options->{isa}) |
999f34a9 |
69 | || confess "You must define a type with the $type metaclass"; |
8ba40fb0 |
70 | |
999f34a9 |
71 | my $isa = $options->{isa}; |
8ba40fb0 |
72 | |
73 | unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) { |
999f34a9 |
74 | $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa); |
8ba40fb0 |
75 | } |
76 | |
77 | ($isa->is_a_type_of($type)) |
78 | || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type"; |
79 | } |
d26633fc |
80 | } |
81 | |
82 | before '_process_options' => sub { |
88aaf2bd |
83 | my ($self, $name, $options) = @_; |
38abf787 |
84 | $self->process_options_for_provides($options, $name); |
d26633fc |
85 | }; |
86 | |
8ba40fb0 |
87 | ## methods called after instantiation |
88 | |
96c2370b |
89 | # this confirms that provides (and curries) has |
8ba40fb0 |
90 | # all valid possibilities in it |
91 | sub check_provides_values { |
92 | my $self = shift; |
999f34a9 |
93 | |
8ba40fb0 |
94 | my $method_constructors = $self->method_constructors; |
999f34a9 |
95 | |
8ba40fb0 |
96 | foreach my $key (keys %{$self->provides}) { |
97 | (exists $method_constructors->{$key}) |
98 | || confess "$key is an unsupported method type"; |
99 | } |
96c2370b |
100 | |
101 | foreach my $key (keys %{$self->curries}) { |
102 | (exists $method_constructors->{$key}) |
103 | || confess "$key is an unsupported method type"; |
104 | } |
8ba40fb0 |
105 | } |
106 | |
c43a2317 |
107 | sub _curry { |
108 | my $self = shift; |
109 | my $code = shift; |
110 | |
c43a2317 |
111 | my @args = @_; |
96c2370b |
112 | return sub { |
113 | my $self = shift; |
114 | $code->($self, @args, @_) |
115 | }; |
c43a2317 |
116 | } |
117 | |
696d4dc7 |
118 | sub _curry_sub { |
119 | my $self = shift; |
120 | my $body = shift; |
121 | my $code = shift; |
122 | |
96c2370b |
123 | return sub { |
124 | my $self = shift; |
125 | $code->($self, $body, @_) |
126 | }; |
696d4dc7 |
127 | } |
128 | |
d26633fc |
129 | after 'install_accessors' => sub { |
130 | my $attr = shift; |
131 | my $class = $attr->associated_class; |
999f34a9 |
132 | |
457dc4fb |
133 | # grab the reader and writer methods |
999f34a9 |
134 | # as well, this will be useful for |
457dc4fb |
135 | # our method provider constructors |
9a976497 |
136 | my $attr_reader = $attr->get_read_method_ref; |
137 | my $attr_writer = $attr->get_write_method_ref; |
999f34a9 |
138 | |
d26633fc |
139 | |
88aaf2bd |
140 | # before we install them, lets |
141 | # make sure they are valid |
999f34a9 |
142 | $attr->check_provides_values; |
88aaf2bd |
143 | |
d26633fc |
144 | my $method_constructors = $attr->method_constructors; |
999f34a9 |
145 | |
dc4333e9 |
146 | my $class_name = $class->name; |
147 | |
3656a0d7 |
148 | while (my ($constructor, $constructed) = each %{$attr->curries}) { |
149 | my $method_code; |
696d4dc7 |
150 | while (my ($curried_name, $curried_arg) = each(%$constructed)) { |
151 | if ($class->has_method($curried_name)) { |
152 | confess |
153 | "The method ($curried_name) already ". |
154 | "exists in class (" . $class->name . ")"; |
3656a0d7 |
155 | } |
696d4dc7 |
156 | my $body = $method_constructors->{$constructor}->( |
157 | $attr, |
158 | $attr_reader, |
159 | $attr_writer, |
160 | ); |
161 | |
162 | if (ref $curried_arg eq 'ARRAY') { |
163 | $method_code = $attr->_curry($body, @$curried_arg); |
164 | } |
165 | elsif (ref $curried_arg eq 'CODE') { |
166 | $method_code = $attr->_curry_sub($body, $curried_arg); |
167 | } |
168 | else { |
169 | confess "curries parameter must be ref type HASH or CODE"; |
170 | } |
171 | |
172 | my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap( |
173 | $method_code, |
174 | package_name => $class_name, |
175 | name => $curried_name, |
176 | ); |
177 | |
178 | $attr->associate_method($method); |
179 | $class->add_method($curried_name => $method); |
c43a2317 |
180 | } |
c43a2317 |
181 | } |
182 | |
d26633fc |
183 | foreach my $key (keys %{$attr->provides}) { |
999f34a9 |
184 | |
185 | my $method_name = $attr->provides->{$key}; |
186 | |
9a976497 |
187 | if ($class->has_method($method_name)) { |
188 | confess "The method ($method_name) already exists in class (" . $class->name . ")"; |
999f34a9 |
189 | } |
190 | |
191 | my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap( |
192 | $method_constructors->{$key}->( |
193 | $attr, |
194 | $attr_reader, |
195 | $attr_writer, |
dc4333e9 |
196 | ), |
197 | package_name => $class_name, |
198 | name => $method_name, |
d26633fc |
199 | ); |
999f34a9 |
200 | |
201 | $attr->associate_method($method); |
202 | $class->add_method($method_name => $method); |
203 | } |
204 | }; |
205 | |
206 | after 'remove_accessors' => sub { |
207 | my $attr = shift; |
208 | my $class = $attr->associated_class; |
96c2370b |
209 | |
210 | # provides accessors |
999f34a9 |
211 | foreach my $key (keys %{$attr->provides}) { |
212 | my $method_name = $attr->provides->{$key}; |
213 | my $method = $class->get_method($method_name); |
214 | $class->remove_method($method_name) |
215 | if blessed($method) && |
216 | $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided'); |
d26633fc |
217 | } |
96c2370b |
218 | |
219 | # curries accessors |
220 | foreach my $key (keys %{$attr->curries}) { |
221 | my $method_name = $attr->curries->{$key}; |
222 | my $method = $class->get_method($method_name); |
223 | $class->remove_method($method_name) |
224 | if blessed($method) && |
225 | $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided'); |
226 | } |
d26633fc |
227 | }; |
228 | |
229 | no Moose; |
8ba40fb0 |
230 | no Moose::Util::TypeConstraints; |
d26633fc |
231 | |
232 | 1; |
233 | |
234 | __END__ |
235 | |
236 | =pod |
237 | |
238 | =head1 NAME |
239 | |
5431dff2 |
240 | MooseX::AttributeHelpers::Base - Base class for attribute helpers |
999f34a9 |
241 | |
d26633fc |
242 | =head1 DESCRIPTION |
243 | |
5431dff2 |
244 | Documentation to come. |
e295d072 |
245 | |
246 | =head1 ATTRIBUTES |
247 | |
5431dff2 |
248 | =over 4 |
249 | |
250 | =item B<provides> |
e295d072 |
251 | |
c6f9edf8 |
252 | =item B<curries> |
253 | |
5431dff2 |
254 | =item B<method_provider> |
e295d072 |
255 | |
5431dff2 |
256 | =item B<method_constructors> |
257 | |
258 | =back |
e295d072 |
259 | |
260 | =head1 EXTENDED ATTRIBUTES |
261 | |
5431dff2 |
262 | =over 4 |
263 | |
4e6acc23 |
264 | =item B<default> |
e295d072 |
265 | |
4e6acc23 |
266 | C<default> is now required. |
e295d072 |
267 | |
5431dff2 |
268 | =item B<type_constraint> |
e295d072 |
269 | |
270 | C<type_constraint> is now required. |
271 | |
5431dff2 |
272 | =back |
273 | |
d26633fc |
274 | =head1 METHODS |
275 | |
5431dff2 |
276 | =over 4 |
277 | |
b91f57af |
278 | =item B<meta> |
279 | |
5431dff2 |
280 | =item B<helper_type> |
281 | |
282 | =item B<check_provides_values> |
283 | |
284 | =item B<has_default> |
285 | |
286 | =item B<has_method_provider> |
287 | |
288 | =item B<has_type_constraint> |
289 | |
290 | =item B<install_accessors> |
291 | |
999f34a9 |
292 | =item B<remove_accessors> |
293 | |
5431dff2 |
294 | =item B<process_options_for_provides> |
295 | |
296 | =back |
e295d072 |
297 | |
d26633fc |
298 | =head1 BUGS |
299 | |
999f34a9 |
300 | All complex software has bugs lurking in it, and this module is no |
d26633fc |
301 | exception. If you find a bug please either email me, or add the bug |
302 | to cpan-RT. |
303 | |
304 | =head1 AUTHOR |
305 | |
306 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
307 | |
308 | =head1 COPYRIGHT AND LICENSE |
309 | |
99c62fb8 |
310 | Copyright 2007-2008 by Infinity Interactive, Inc. |
d26633fc |
311 | |
312 | L<http://www.iinteractive.com> |
313 | |
314 | This library is free software; you can redistribute it and/or modify |
315 | it under the same terms as Perl itself. |
316 | |
8a9cea9b |
317 | =cut |