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 # extend the parents stuff to make sure
55 # certain bits are now required ...
56 has '+default' => (required => 1);
57 has '+type_constraint' => (required => 1);
59 ## Methods called prior to instantiation
61 sub process_options_for_provides {
62 my ($self, $options) = @_;
64 if (my $type = $self->helper_type) {
65 (exists $options->{isa})
66 || confess "You must define a type with the $type metaclass";
68 my $isa = $options->{isa};
70 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
71 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
74 ($isa->is_a_type_of($type))
75 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
79 before '_process_options' => sub {
80 my ($self, $name, $options) = @_;
81 $self->process_options_for_provides($options, $name);
84 ## methods called after instantiation
86 sub check_provides_values {
89 my $method_constructors = $self->method_constructors;
91 foreach my $key (keys %{$self->provides}) {
92 (exists $method_constructors->{$key})
93 || confess "$key is an unsupported method type";
96 foreach my $key (keys %{$self->curries}) {
97 (exists $method_constructors->{$key})
98 || confess "$key is an unsupported method type";
109 $code->($self, @args, @_)
120 $code->($self, $body, @_)
124 after 'install_accessors' => sub {
126 my $class = $attr->associated_class;
128 # grab the reader and writer methods
129 # as well, this will be useful for
130 # our method provider constructors
131 my $attr_reader = $attr->get_read_method_ref;
132 my $attr_writer = $attr->get_write_method_ref;
135 # before we install them, lets
136 # make sure they are valid
137 $attr->check_provides_values;
139 my $method_constructors = $attr->method_constructors;
141 my $class_name = $class->name;
143 while (my ($constructor, $constructed) = each %{$attr->curries}) {
145 while (my ($curried_name, $curried_arg) = each(%$constructed)) {
146 if ($class->has_method($curried_name)) {
148 "The method ($curried_name) already ".
149 "exists in class (" . $class->name . ")";
151 my $body = $method_constructors->{$constructor}->(
157 if (ref $curried_arg eq 'ARRAY') {
158 $method_code = $attr->_curry($body, @$curried_arg);
160 elsif (ref $curried_arg eq 'CODE') {
161 $method_code = $attr->_curry_sub($body, $curried_arg);
164 confess "curries parameter must be ref type ARRAY or CODE";
167 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
169 package_name => $class_name,
170 name => $curried_name,
173 $attr->associate_method($method);
174 $class->add_method($curried_name => $method);
178 foreach my $key (keys %{$attr->provides}) {
180 my $method_name = $attr->provides->{$key};
182 if ($class->has_method($method_name)) {
183 confess "The method ($method_name) already exists in class (" . $class->name . ")";
186 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
187 $method_constructors->{$key}->(
192 package_name => $class_name,
193 name => $method_name,
196 $attr->associate_method($method);
197 $class->add_method($method_name => $method);
201 after 'remove_accessors' => sub {
203 my $class = $attr->associated_class;
206 foreach my $key (keys %{$attr->provides}) {
207 my $method_name = $attr->provides->{$key};
208 my $method = $class->get_method($method_name);
209 $class->remove_method($method_name)
210 if blessed($method) &&
211 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
215 foreach my $key (keys %{$attr->curries}) {
216 my $method_name = $attr->curries->{$key};
217 my $method = $class->get_method($method_name);
218 $class->remove_method($method_name)
219 if blessed($method) &&
220 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
225 no Moose::Util::TypeConstraints;
233 MooseX::AttributeHelpers::Trait::Base - base role for helpers
237 =head2 check_provides_values
239 Confirms that provides (and curries) has all valid possibilities in it.
241 =head2 process_options_for_provides
243 Ensures that the type constraint (C<isa>) matches the helper type.
247 All complex software has bugs lurking in it, and this module is no
248 exception. If you find a bug please either email me, or add the bug
259 =head1 COPYRIGHT AND LICENSE
261 Copyright 2007-2009 by Infinity Interactive, Inc.
263 L<http://www.iinteractive.com>
265 This library is free software; you can redistribute it and/or modify
266 it under the same terms as Perl itself.