2 package MooseX::AttributeHelpers::Trait::Base;
4 use Moose::Util::TypeConstraints;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
10 requires 'helper_type';
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
31 # requires_attr 'method_provider'
33 # or you can provide a HASH ref of anon subs
34 # yourself. This will also collect and store
35 # the methods from a method_provider as well
36 has 'method_constructors' => (
42 return +{} unless $self->has_method_provider;
43 # or grab them from the role/class
44 my $method_provider = $self->method_provider->meta;
47 $_ => $method_provider->get_method($_)
49 grep { $_ ne 'meta' } $method_provider->get_method_list
54 ## Methods called prior to instantiation
56 sub process_options_for_provides {
57 my ($self, $options) = @_;
59 if (my $type = $self->helper_type) {
60 (exists $options->{isa})
61 || confess "You must define a type with the $type metaclass";
63 my $isa = $options->{isa};
65 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
66 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
69 ($isa->is_a_type_of($type))
70 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
74 before '_process_options' => sub {
75 my ($self, $name, $options) = @_;
76 $self->process_options_for_provides($options, $name);
79 ## methods called after instantiation
81 sub check_provides_values {
84 my $method_constructors = $self->method_constructors;
86 foreach my $key (keys %{$self->provides}) {
87 (exists $method_constructors->{$key})
88 || confess "$key is an unsupported method type";
91 foreach my $key (keys %{$self->curries}) {
92 (exists $method_constructors->{$key})
93 || confess "$key is an unsupported method type";
104 $code->($self, @args, @_)
115 $code->($self, $body, @_)
119 after 'install_accessors' => sub {
121 my $class = $attr->associated_class;
123 # grab the reader and writer methods
124 # as well, this will be useful for
125 # our method provider constructors
126 my $attr_reader = $attr->get_read_method_ref;
127 my $attr_writer = $attr->get_write_method_ref;
130 # before we install them, lets
131 # make sure they are valid
132 $attr->check_provides_values;
134 my $method_constructors = $attr->method_constructors;
136 my $class_name = $class->name;
138 while (my ($constructor, $constructed) = each %{$attr->curries}) {
140 while (my ($curried_name, $curried_arg) = each(%$constructed)) {
141 if ($class->has_method($curried_name)) {
143 "The method ($curried_name) already ".
144 "exists in class (" . $class->name . ")";
146 my $body = $method_constructors->{$constructor}->(
152 if (ref $curried_arg eq 'ARRAY') {
153 $method_code = $attr->_curry($body, @$curried_arg);
155 elsif (ref $curried_arg eq 'CODE') {
156 $method_code = $attr->_curry_sub($body, $curried_arg);
159 confess "curries parameter must be ref type ARRAY or CODE";
162 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
164 package_name => $class_name,
165 name => $curried_name,
168 $attr->associate_method($method);
169 $class->add_method($curried_name => $method);
173 foreach my $key (keys %{$attr->provides}) {
175 my $method_name = $attr->provides->{$key};
177 if ($class->has_method($method_name)) {
178 confess "The method ($method_name) already exists in class (" . $class->name . ")";
181 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
182 $method_constructors->{$key}->(
187 package_name => $class_name,
188 name => $method_name,
191 $attr->associate_method($method);
192 $class->add_method($method_name => $method);
196 after 'remove_accessors' => sub {
198 my $class = $attr->associated_class;
201 foreach my $key (keys %{$attr->provides}) {
202 my $method_name = $attr->provides->{$key};
203 my $method = $class->get_method($method_name);
204 $class->remove_method($method_name)
205 if blessed($method) &&
206 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
210 foreach my $key (keys %{$attr->curries}) {
211 my $method_name = $attr->curries->{$key};
212 my $method = $class->get_method($method_name);
213 $class->remove_method($method_name)
214 if blessed($method) &&
215 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
220 no Moose::Util::TypeConstraints;
228 MooseX::AttributeHelpers::Trait::Base - base role for helpers
232 =head2 check_provides_values
234 Confirms that provides (and curries) has all valid possibilities in it.
236 =head2 process_options_for_provides
238 Ensures that the type constraint (C<isa>) matches the helper type.
242 All complex software has bugs lurking in it, and this module is no
243 exception. If you find a bug please either email me, or add the bug
254 =head1 COPYRIGHT AND LICENSE
256 Copyright 2007-2009 by Infinity Interactive, Inc.
258 L<http://www.iinteractive.com>
260 This library is free software; you can redistribute it and/or modify
261 it under the same terms as Perl itself.