add method provider currying support
[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.04';
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
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 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
103 sub _curry {
104     my $self = shift;
105     my $code = shift;
106
107     #warn "_curry: "; use DDS; warn Dump($self);
108     my @args = @_;
109     return sub { my $self = shift; $code->($self, @args, @_) };
110 }
111
112 after 'install_accessors' => sub {
113     my $attr  = shift;
114     my $class = $attr->associated_class;
115
116     # grab the reader and writer methods
117     # as well, this will be useful for
118     # our method provider constructors
119     my $attr_reader = $attr->get_read_method_ref;
120     my $attr_writer = $attr->get_write_method_ref;
121
122
123     # before we install them, lets
124     # make sure they are valid
125     $attr->check_provides_values;
126 #    $attr->check_curries_values;
127
128     my $method_constructors = $attr->method_constructors;
129
130     my $class_name = $class->name;
131
132     foreach my $key (keys %{$attr->curries}) {
133
134         my ($curried_name, @curried_args) = @{ $attr->curries->{$key} };
135
136         if ($class->has_method($curried_name)) {
137             confess "The method ($curried_name) already exists in class (" . $class->name . ")";
138         }
139
140         my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
141             $attr->_curry($method_constructors->{$key}->(
142                 $attr,
143                 $attr_reader,
144                 $attr_writer,
145             ), @curried_args),
146             package_name => $class_name,
147             name => $curried_name,
148         );
149         
150 #use DDS; warn Dump($method);
151
152         $attr->associate_method($method);
153         $class->add_method($curried_name => $method);
154     }
155
156     foreach my $key (keys %{$attr->provides}) {
157
158         my $method_name = $attr->provides->{$key};
159
160         if ($class->has_method($method_name)) {
161             confess "The method ($method_name) already exists in class (" . $class->name . ")";
162         }
163
164         my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
165             $method_constructors->{$key}->(
166                 $attr,
167                 $attr_reader,
168                 $attr_writer,
169             ),
170             package_name => $class_name,
171             name => $method_name,
172         );
173         
174         $attr->associate_method($method);
175         $class->add_method($method_name => $method);
176     }
177 };
178
179 after 'remove_accessors' => sub {
180     my $attr  = shift;
181     my $class = $attr->associated_class;
182     foreach my $key (keys %{$attr->provides}) {
183         my $method_name = $attr->provides->{$key};
184         my $method = $class->get_method($method_name);
185         $class->remove_method($method_name)
186             if blessed($method) &&
187                $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
188     }
189 };
190
191 no Moose;
192 no Moose::Util::TypeConstraints;
193
194 1;
195
196 __END__
197
198 =pod
199
200 =head1 NAME
201
202 MooseX::AttributeHelpers::Base - Base class for attribute helpers
203
204 =head1 DESCRIPTION
205
206 Documentation to come.
207
208 =head1 ATTRIBUTES
209
210 =over 4
211
212 =item B<provides>
213
214 =item B<method_provider>
215
216 =item B<method_constructors>
217
218 =back
219
220 =head1 EXTENDED ATTRIBUTES
221
222 =over 4
223
224 =item B<$!default>
225
226 C<$!default> is now required.
227
228 =item B<type_constraint>
229
230 C<type_constraint> is now required.
231
232 =back
233
234 =head1 METHODS
235
236 =over 4
237
238 =item B<meta>
239
240 =item B<helper_type>
241
242 =item B<check_provides_values>
243
244 =item B<has_default>
245
246 =item B<has_method_provider>
247
248 =item B<has_type_constraint>
249
250 =item B<install_accessors>
251
252 =item B<remove_accessors>
253
254 =item B<process_options_for_provides>
255
256 =back
257
258 =head1 BUGS
259
260 All complex software has bugs lurking in it, and this module is no
261 exception. If you find a bug please either email me, or add the bug
262 to cpan-RT.
263
264 =head1 AUTHOR
265
266 Stevan Little E<lt>stevan@iinteractive.comE<gt>
267
268 =head1 COPYRIGHT AND LICENSE
269
270 Copyright 2007-2008 by Infinity Interactive, Inc.
271
272 L<http://www.iinteractive.com>
273
274 This library is free software; you can redistribute it and/or modify
275 it under the same terms as Perl itself.
276
277 =cut