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 ...
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 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";
108 return sub { my $self = shift; $code->($self, @args, @_) };
116 warn "installing sub!";
118 return sub { my $self = shift; $code->($self, $body, @_) };
121 after 'install_accessors' => sub {
123 my $class = $attr->associated_class;
125 # grab the reader and writer methods
126 # as well, this will be useful for
127 # our method provider constructors
128 my $attr_reader = $attr->get_read_method_ref;
129 my $attr_writer = $attr->get_write_method_ref;
132 # before we install them, lets
133 # make sure they are valid
134 $attr->check_provides_values;
135 # $attr->check_curries_values;
137 my $method_constructors = $attr->method_constructors;
139 my $class_name = $class->name;
141 while (my ($constructor, $constructed) = each %{$attr->curries}) {
143 while (my ($curried_name, $curried_arg) = each(%$constructed)) {
144 if ($class->has_method($curried_name)) {
146 "The method ($curried_name) already ".
147 "exists in class (" . $class->name . ")";
149 my $body = $method_constructors->{$constructor}->(
155 if (ref $curried_arg eq 'ARRAY') {
156 $method_code = $attr->_curry($body, @$curried_arg);
158 elsif (ref $curried_arg eq 'CODE') {
159 $method_code = $attr->_curry_sub($body, $curried_arg);
162 confess "curries parameter must be ref type HASH or CODE";
165 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
167 package_name => $class_name,
168 name => $curried_name,
171 $attr->associate_method($method);
172 $class->add_method($curried_name => $method);
176 foreach my $key (keys %{$attr->provides}) {
178 my $method_name = $attr->provides->{$key};
180 if ($class->has_method($method_name)) {
181 confess "The method ($method_name) already exists in class (" . $class->name . ")";
184 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
185 $method_constructors->{$key}->(
190 package_name => $class_name,
191 name => $method_name,
194 $attr->associate_method($method);
195 $class->add_method($method_name => $method);
199 after 'remove_accessors' => sub {
201 my $class = $attr->associated_class;
202 foreach my $key (keys %{$attr->provides}) {
203 my $method_name = $attr->provides->{$key};
204 my $method = $class->get_method($method_name);
205 $class->remove_method($method_name)
206 if blessed($method) &&
207 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
212 no Moose::Util::TypeConstraints;
222 MooseX::AttributeHelpers::Base - Base class for attribute helpers
226 Documentation to come.
234 =item B<method_provider>
236 =item B<method_constructors>
240 =head1 EXTENDED ATTRIBUTES
246 C<$!default> is now required.
248 =item B<type_constraint>
250 C<type_constraint> is now required.
262 =item B<check_provides_values>
266 =item B<has_method_provider>
268 =item B<has_type_constraint>
270 =item B<install_accessors>
272 =item B<remove_accessors>
274 =item B<process_options_for_provides>
280 All complex software has bugs lurking in it, and this module is no
281 exception. If you find a bug please either email me, or add the bug
286 Stevan Little E<lt>stevan@iinteractive.comE<gt>
288 =head1 COPYRIGHT AND LICENSE
290 Copyright 2007-2008 by Infinity Interactive, Inc.
292 L<http://www.iinteractive.com>
294 This library is free software; you can redistribute it and/or modify
295 it under the same terms as Perl itself.