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