Update for new Class::MOP wrapping stuff
[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
19 # these next two are the possible methods
20 # you can use in the 'provides' map.
21
22 # provide a Class or Role which we can
23 # collect the method providers from
24 has 'method_provider' => (
25     is        => 'ro',
26     isa       => 'ClassName',
27     predicate => 'has_method_provider',
28 );
29
30 # or you can provide a HASH ref of anon subs
31 # yourself. This will also collect and store
32 # the methods from a method_provider as well
33 has 'method_constructors' => (
34     is      => 'ro',
35     isa     => 'HashRef',
36     lazy    => 1,
37     default => sub {
38         my $self = shift;
39         return +{} unless $self->has_method_provider;
40         # or grab them from the role/class
41         my $method_provider = $self->method_provider->meta;
42         return +{
43             map {
44                 $_ => $method_provider->get_method($_)
45             } $method_provider->get_method_list
46         };
47     },
48 );
49
50 # extend the parents stuff to make sure
51 # certain bits are now required ...
52 has '+$!default'       => (required => 1);
53 has '+type_constraint' => (required => 1);
54
55 ## Methods called prior to instantiation
56
57 sub helper_type { () }
58
59 sub process_options_for_provides {
60     my ($self, $options) = @_;
61
62     if (my $type = $self->helper_type) {
63         (exists $options->{isa})
64             || confess "You must define a type with the $type metaclass";
65
66         my $isa = $options->{isa};
67
68         unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
69             $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
70         }
71
72         ($isa->is_a_type_of($type))
73             || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
74     }
75 }
76
77 before '_process_options' => sub {
78     my ($self, $name, $options) = @_;
79     $self->process_options_for_provides($options, $name);
80 };
81
82 ## methods called after instantiation
83
84 # this confirms that provides has
85 # all valid possibilities in it
86 sub check_provides_values {
87     my $self = shift;
88
89     my $method_constructors = $self->method_constructors;
90
91     foreach my $key (keys %{$self->provides}) {
92         (exists $method_constructors->{$key})
93             || confess "$key is an unsupported method type";
94     }
95 }
96
97 after 'install_accessors' => sub {
98     my $attr  = shift;
99     my $class = $attr->associated_class;
100
101     # grab the reader and writer methods
102     # as well, this will be useful for
103     # our method provider constructors
104     my $attr_reader = $attr->get_read_method_ref;
105     my $attr_writer = $attr->get_write_method_ref;
106
107
108     # before we install them, lets
109     # make sure they are valid
110     $attr->check_provides_values;
111
112     my $method_constructors = $attr->method_constructors;
113
114     my $class_name = $class->name;
115
116     foreach my $key (keys %{$attr->provides}) {
117
118         my $method_name = $attr->provides->{$key};
119
120         if ($class->has_method($method_name)) {
121             confess "The method ($method_name) already exists in class (" . $class->name . ")";
122         }
123
124         my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
125             $method_constructors->{$key}->(
126                 $attr,
127                 $attr_reader,
128                 $attr_writer,
129             ),
130             package_name => $class_name,
131             name => $method_name,
132         );
133         
134         $attr->associate_method($method);
135         $class->add_method($method_name => $method);
136     }
137 };
138
139 after 'remove_accessors' => sub {
140     my $attr  = shift;
141     my $class = $attr->associated_class;
142     foreach my $key (keys %{$attr->provides}) {
143         my $method_name = $attr->provides->{$key};
144         my $method = $class->get_method($method_name);
145         $class->remove_method($method_name)
146             if blessed($method) &&
147                $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
148     }
149 };
150
151 no Moose;
152 no Moose::Util::TypeConstraints;
153
154 1;
155
156 __END__
157
158 =pod
159
160 =head1 NAME
161
162 MooseX::AttributeHelpers::Base - Base class for attribute helpers
163
164 =head1 DESCRIPTION
165
166 Documentation to come.
167
168 =head1 ATTRIBUTES
169
170 =over 4
171
172 =item B<provides>
173
174 =item B<method_provider>
175
176 =item B<method_constructors>
177
178 =back
179
180 =head1 EXTENDED ATTRIBUTES
181
182 =over 4
183
184 =item B<$!default>
185
186 C<$!default> is now required.
187
188 =item B<type_constraint>
189
190 C<type_constraint> is now required.
191
192 =back
193
194 =head1 METHODS
195
196 =over 4
197
198 =item B<meta>
199
200 =item B<helper_type>
201
202 =item B<check_provides_values>
203
204 =item B<has_default>
205
206 =item B<has_method_provider>
207
208 =item B<has_type_constraint>
209
210 =item B<install_accessors>
211
212 =item B<remove_accessors>
213
214 =item B<process_options_for_provides>
215
216 =back
217
218 =head1 BUGS
219
220 All complex software has bugs lurking in it, and this module is no
221 exception. If you find a bug please either email me, or add the bug
222 to cpan-RT.
223
224 =head1 AUTHOR
225
226 Stevan Little E<lt>stevan@iinteractive.comE<gt>
227
228 =head1 COPYRIGHT AND LICENSE
229
230 Copyright 2007-2008 by Infinity Interactive, Inc.
231
232 L<http://www.iinteractive.com>
233
234 This library is free software; you can redistribute it and/or modify
235 it under the same terms as Perl itself.
236
237 =cut