2 package MooseX::AttributeHelpers::Base;
4 use Moose::Util::TypeConstraints;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
10 extends 'Moose::Meta::Attribute';
12 # this is the method map you define ...
25 # these next two are the possible methods
26 # you can use in the 'provides' map.
28 # provide a Class or Role which we can
29 # collect the method providers from
30 has 'method_provider' => (
33 predicate => 'has_method_provider',
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' => (
45 return +{} unless $self->has_method_provider;
46 # or grab them from the role/class
47 my $method_provider = $self->method_provider->meta;
50 $_ => $method_provider->get_method($_)
51 } $method_provider->get_method_list
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);
61 ## Methods called prior to instantiation
63 sub helper_type { () }
65 sub process_options_for_provides {
66 my ($self, $options) = @_;
68 if (my $type = $self->helper_type) {
69 (exists $options->{isa})
70 || confess "You must define a type with the $type metaclass";
72 my $isa = $options->{isa};
74 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
75 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
78 ($isa->is_a_type_of($type))
79 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
83 before '_process_options' => sub {
84 my ($self, $name, $options) = @_;
85 $self->process_options_for_provides($options, $name);
88 ## methods called after instantiation
90 # this confirms that provides (and curries) has
91 # all valid possibilities in it
92 sub check_provides_values {
95 my $method_constructors = $self->method_constructors;
97 foreach my $key (keys %{$self->provides}) {
98 (exists $method_constructors->{$key})
99 || confess "$key is an unsupported method type";
102 foreach my $key (keys %{$self->curries}) {
103 (exists $method_constructors->{$key})
104 || confess "$key is an unsupported method type";
115 $code->($self, @args, @_)
126 $code->($self, $body, @_)
130 after 'install_accessors' => sub {
132 my $class = $attr->associated_class;
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;
141 # before we install them, lets
142 # make sure they are valid
143 $attr->check_provides_values;
145 my $method_constructors = $attr->method_constructors;
147 my $class_name = $class->name;
149 while (my ($constructor, $constructed) = each %{$attr->curries}) {
151 while (my ($curried_name, $curried_arg) = each(%$constructed)) {
152 if ($class->has_method($curried_name)) {
154 "The method ($curried_name) already ".
155 "exists in class (" . $class->name . ")";
157 my $body = $method_constructors->{$constructor}->(
163 if (ref $curried_arg eq 'ARRAY') {
164 $method_code = $attr->_curry($body, @$curried_arg);
166 elsif (ref $curried_arg eq 'CODE') {
167 $method_code = $attr->_curry_sub($body, $curried_arg);
170 confess "curries parameter must be ref type HASH or CODE";
173 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
175 package_name => $class_name,
176 name => $curried_name,
179 $attr->associate_method($method);
180 $class->add_method($curried_name => $method);
184 foreach my $key (keys %{$attr->provides}) {
186 my $method_name = $attr->provides->{$key};
188 if ($class->has_method($method_name)) {
189 confess "The method ($method_name) already exists in class (" . $class->name . ")";
192 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
193 $method_constructors->{$key}->(
198 package_name => $class_name,
199 name => $method_name,
202 $attr->associate_method($method);
203 $class->add_method($method_name => $method);
207 after 'remove_accessors' => sub {
209 my $class = $attr->associated_class;
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');
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');
231 no Moose::Util::TypeConstraints;
241 MooseX::AttributeHelpers::Base - Base class for attribute helpers
245 Documentation to come.
255 =item B<method_provider>
257 =item B<method_constructors>
261 =head1 EXTENDED ATTRIBUTES
267 C<default> is now required.
269 =item B<type_constraint>
271 C<type_constraint> is now required.
283 =item B<check_provides_values>
287 =item B<has_method_provider>
289 =item B<has_type_constraint>
291 =item B<install_accessors>
293 =item B<remove_accessors>
295 =item B<process_options_for_provides>
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
307 Stevan Little E<lt>stevan@iinteractive.comE<gt>
309 =head1 COPYRIGHT AND LICENSE
311 Copyright 2007-2008 by Infinity Interactive, Inc.
313 L<http://www.iinteractive.com>
315 This library is free software; you can redistribute it and/or modify
316 it under the same terms as Perl itself.