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";
107 #warn "_curry: "; use DDS; warn Dump($self);
109 return sub { my $self = shift; $code->($self, @args, @_) };
112 after 'install_accessors' => sub {
114 my $class = $attr->associated_class;
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;
123 # before we install them, lets
124 # make sure they are valid
125 $attr->check_provides_values;
126 # $attr->check_curries_values;
128 my $method_constructors = $attr->method_constructors;
130 my $class_name = $class->name;
132 foreach my $key (keys %{$attr->curries}) {
134 my ($curried_name, @curried_args) = @{ $attr->curries->{$key} };
136 if ($class->has_method($curried_name)) {
137 confess "The method ($curried_name) already exists in class (" . $class->name . ")";
140 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
141 $attr->_curry($method_constructors->{$key}->(
146 package_name => $class_name,
147 name => $curried_name,
150 #use DDS; warn Dump($method);
152 $attr->associate_method($method);
153 $class->add_method($curried_name => $method);
156 foreach my $key (keys %{$attr->provides}) {
158 my $method_name = $attr->provides->{$key};
160 if ($class->has_method($method_name)) {
161 confess "The method ($method_name) already exists in class (" . $class->name . ")";
164 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
165 $method_constructors->{$key}->(
170 package_name => $class_name,
171 name => $method_name,
174 $attr->associate_method($method);
175 $class->add_method($method_name => $method);
179 after 'remove_accessors' => sub {
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');
192 no Moose::Util::TypeConstraints;
202 MooseX::AttributeHelpers::Base - Base class for attribute helpers
206 Documentation to come.
214 =item B<method_provider>
216 =item B<method_constructors>
220 =head1 EXTENDED ATTRIBUTES
226 C<$!default> is now required.
228 =item B<type_constraint>
230 C<type_constraint> is now required.
242 =item B<check_provides_values>
246 =item B<has_method_provider>
248 =item B<has_type_constraint>
250 =item B<install_accessors>
252 =item B<remove_accessors>
254 =item B<process_options_for_provides>
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
266 Stevan Little E<lt>stevan@iinteractive.comE<gt>
268 =head1 COPYRIGHT AND LICENSE
270 Copyright 2007-2008 by Infinity Interactive, Inc.
272 L<http://www.iinteractive.com>
274 This library is free software; you can redistribute it and/or modify
275 it under the same terms as Perl itself.