fix the $! bullshit
[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.13';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 extends 'Moose::Meta::Attribute';
10
11 # this is the method map you define ...
12 has 'provides' => (
13     is      => 'ro',
14     isa     => 'HashRef',
15     default => sub {{}}
16 );
17
18 has 'curries' => (
19     is      => 'ro',
20     isa     => 'HashRef',
21     default => sub {{}}
22 );
23
24 # these next two are the possible methods
25 # you can use in the 'provides' map.
26
27 # provide a Class or Role which we can
28 # collect the method providers from
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
37 # the methods from a method_provider as well
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 +{
48             map {
49                 $_ => $method_provider->get_method($_)
50             } $method_provider->get_method_list
51         };
52     },
53 );
54
55 # extend the parents stuff to make sure
56 # certain bits are now required ...
57 has '+default'         => (required => 1);
58 has '+type_constraint' => (required => 1);
59
60 ## Methods called prior to instantiation
61
62 sub helper_type { () }
63
64 sub process_options_for_provides {
65     my ($self, $options) = @_;
66
67     if (my $type = $self->helper_type) {
68         (exists $options->{isa})
69             || confess "You must define a type with the $type metaclass";
70
71         my $isa = $options->{isa};
72
73         unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
74             $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
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     }
80 }
81
82 before '_process_options' => sub {
83     my ($self, $name, $options) = @_;
84     $self->process_options_for_provides($options, $name);
85 };
86
87 ## methods called after instantiation
88
89 # this confirms that provides (and curries) has
90 # all valid possibilities in it
91 sub check_provides_values {
92     my $self = shift;
93
94     my $method_constructors = $self->method_constructors;
95
96     foreach my $key (keys %{$self->provides}) {
97         (exists $method_constructors->{$key})
98             || confess "$key is an unsupported method type";
99     }
100
101     foreach my $key (keys %{$self->curries}) {
102         (exists $method_constructors->{$key})
103             || confess "$key is an unsupported method type";
104     }
105 }
106
107 sub _curry {
108     my $self = shift;
109     my $code = shift;
110
111     my @args = @_;
112     return sub {
113         my $self = shift;
114         $code->($self, @args, @_)
115     };
116 }
117
118 sub _curry_sub {
119     my $self = shift;
120     my $body = shift;
121     my $code = shift;
122
123     return sub {
124         my $self = shift;
125         $code->($self, $body, @_)
126     };
127 }
128
129 after 'install_accessors' => sub {
130     my $attr  = shift;
131     my $class = $attr->associated_class;
132
133     # grab the reader and writer methods
134     # as well, this will be useful for
135     # our method provider constructors
136     my $attr_reader = $attr->get_read_method_ref;
137     my $attr_writer = $attr->get_write_method_ref;
138
139
140     # before we install them, lets
141     # make sure they are valid
142     $attr->check_provides_values;
143
144     my $method_constructors = $attr->method_constructors;
145
146     my $class_name = $class->name;
147
148     while (my ($constructor, $constructed) = each %{$attr->curries}) {
149         my $method_code;
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 . ")";
155             }
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);
180         }
181     }
182
183     foreach my $key (keys %{$attr->provides}) {
184
185         my $method_name = $attr->provides->{$key};
186
187         if ($class->has_method($method_name)) {
188             confess "The method ($method_name) already exists in class (" . $class->name . ")";
189         }
190
191         my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
192             $method_constructors->{$key}->(
193                 $attr,
194                 $attr_reader,
195                 $attr_writer,
196             ),
197             package_name => $class_name,
198             name => $method_name,
199         );
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;
209
210     # provides accessors
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');
217     }
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     }
227 };
228
229 no Moose;
230 no Moose::Util::TypeConstraints;
231
232 1;
233
234 __END__
235
236 =pod
237
238 =head1 NAME
239
240 MooseX::AttributeHelpers::Base - Base class for attribute helpers
241
242 =head1 DESCRIPTION
243
244 Documentation to come.
245
246 =head1 ATTRIBUTES
247
248 =over 4
249
250 =item B<provides>
251
252 =item B<curries>
253
254 =item B<method_provider>
255
256 =item B<method_constructors>
257
258 =back
259
260 =head1 EXTENDED ATTRIBUTES
261
262 =over 4
263
264 =item B<default>
265
266 C<default> is now required.
267
268 =item B<type_constraint>
269
270 C<type_constraint> is now required.
271
272 =back
273
274 =head1 METHODS
275
276 =over 4
277
278 =item B<meta>
279
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
292 =item B<remove_accessors>
293
294 =item B<process_options_for_provides>
295
296 =back
297
298 =head1 BUGS
299
300 All complex software has bugs lurking in it, and this module is no
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
310 Copyright 2007-2008 by Infinity Interactive, Inc.
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
317 =cut