2 package MooseX::AttributeHelpers::Base;
4 use Moose::Util::TypeConstraints;
7 our $AUTHORITY = 'cpan:STEVAN';
9 extends 'Moose::Meta::Attribute';
11 # this is the method map you define ...
24 # these next two are the possible methods
25 # you can use in the 'provides' map.
27 # provide a Class or Role which we can
28 # collect the method providers from
29 has 'method_provider' => (
32 predicate => 'has_method_provider',
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' => (
44 return +{} unless $self->has_method_provider;
45 # or grab them from the role/class
46 my $method_provider = $self->method_provider->meta;
49 $_ => $method_provider->get_method($_)
50 } $method_provider->get_method_list
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);
60 ## Methods called prior to instantiation
62 sub helper_type { () }
64 sub process_options_for_provides {
65 my ($self, $options) = @_;
67 if (my $type = $self->helper_type) {
68 (exists $options->{isa})
69 || confess "You must define a type with the $type metaclass";
71 my $isa = $options->{isa};
73 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
74 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
77 ($isa->is_a_type_of($type))
78 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
82 before '_process_options' => sub {
83 my ($self, $name, $options) = @_;
84 $self->process_options_for_provides($options, $name);
87 ## methods called after instantiation
89 # this confirms that provides (and curries) has
90 # all valid possibilities in it
91 sub check_provides_values {
94 my $method_constructors = $self->method_constructors;
96 foreach my $key (keys %{$self->provides}) {
97 (exists $method_constructors->{$key})
98 || confess "$key is an unsupported method type";
101 foreach my $key (keys %{$self->curries}) {
102 (exists $method_constructors->{$key})
103 || confess "$key is an unsupported method type";
114 $code->($self, @args, @_)
125 $code->($self, $body, @_)
129 after 'install_accessors' => sub {
131 my $class = $attr->associated_class;
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;
140 # before we install them, lets
141 # make sure they are valid
142 $attr->check_provides_values;
144 my $method_constructors = $attr->method_constructors;
146 my $class_name = $class->name;
148 while (my ($constructor, $constructed) = each %{$attr->curries}) {
150 while (my ($curried_name, $curried_arg) = each(%$constructed)) {
151 if ($class->has_method($curried_name)) {
153 "The method ($curried_name) already ".
154 "exists in class (" . $class->name . ")";
156 my $body = $method_constructors->{$constructor}->(
162 if (ref $curried_arg eq 'ARRAY') {
163 $method_code = $attr->_curry($body, @$curried_arg);
165 elsif (ref $curried_arg eq 'CODE') {
166 $method_code = $attr->_curry_sub($body, $curried_arg);
169 confess "curries parameter must be ref type HASH or CODE";
172 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
174 package_name => $class_name,
175 name => $curried_name,
178 $attr->associate_method($method);
179 $class->add_method($curried_name => $method);
183 foreach my $key (keys %{$attr->provides}) {
185 my $method_name = $attr->provides->{$key};
187 if ($class->has_method($method_name)) {
188 confess "The method ($method_name) already exists in class (" . $class->name . ")";
191 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
192 $method_constructors->{$key}->(
197 package_name => $class_name,
198 name => $method_name,
201 $attr->associate_method($method);
202 $class->add_method($method_name => $method);
206 after 'remove_accessors' => sub {
208 my $class = $attr->associated_class;
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');
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');
230 no Moose::Util::TypeConstraints;
240 MooseX::AttributeHelpers::Base - Base class for attribute helpers
244 Documentation to come.
254 =item B<method_provider>
256 =item B<method_constructors>
260 =head1 EXTENDED ATTRIBUTES
266 C<$!default> is now required.
268 =item B<type_constraint>
270 C<type_constraint> is now required.
282 =item B<check_provides_values>
286 =item B<has_method_provider>
288 =item B<has_type_constraint>
290 =item B<install_accessors>
292 =item B<remove_accessors>
294 =item B<process_options_for_provides>
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
306 Stevan Little E<lt>stevan@iinteractive.comE<gt>
308 =head1 COPYRIGHT AND LICENSE
310 Copyright 2007-2008 by Infinity Interactive, Inc.
312 L<http://www.iinteractive.com>
314 This library is free software; you can redistribute it and/or modify
315 it under the same terms as Perl itself.