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($_)
48 } $method_provider->get_method_list
53 # extend the parents stuff to make sure
54 # certain bits are now required ...
55 has '+default' => (required => 1);
56 has '+type_constraint' => (required => 1);
58 ## Methods called prior to instantiation
60 sub process_options_for_provides {
61 my ($self, $options) = @_;
63 if (my $type = $self->helper_type) {
64 (exists $options->{isa})
65 || confess "You must define a type with the $type metaclass";
67 my $isa = $options->{isa};
69 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
70 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
73 ($isa->is_a_type_of($type))
74 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
78 before '_process_options' => sub {
79 my ($self, $name, $options) = @_;
80 $self->process_options_for_provides($options, $name);
83 ## methods called after instantiation
85 sub check_provides_values {
88 my $method_constructors = $self->method_constructors;
90 foreach my $key (keys %{$self->provides}) {
91 (exists $method_constructors->{$key})
92 || confess "$key is an unsupported method type";
95 foreach my $key (keys %{$self->curries}) {
96 (exists $method_constructors->{$key})
97 || confess "$key is an unsupported method type";
108 $code->($self, @args, @_)
119 $code->($self, $body, @_)
123 after 'install_accessors' => sub {
125 my $class = $attr->associated_class;
127 # grab the reader and writer methods
128 # as well, this will be useful for
129 # our method provider constructors
130 my $attr_reader = $attr->get_read_method_ref;
131 my $attr_writer = $attr->get_write_method_ref;
134 # before we install them, lets
135 # make sure they are valid
136 $attr->check_provides_values;
138 my $method_constructors = $attr->method_constructors;
140 my $class_name = $class->name;
142 while (my ($constructor, $constructed) = each %{$attr->curries}) {
144 while (my ($curried_name, $curried_arg) = each(%$constructed)) {
145 if ($class->has_method($curried_name)) {
147 "The method ($curried_name) already ".
148 "exists in class (" . $class->name . ")";
150 my $body = $method_constructors->{$constructor}->(
156 if (ref $curried_arg eq 'ARRAY') {
157 $method_code = $attr->_curry($body, @$curried_arg);
159 elsif (ref $curried_arg eq 'CODE') {
160 $method_code = $attr->_curry_sub($body, $curried_arg);
163 confess "curries parameter must be ref type HASH or CODE";
166 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
168 package_name => $class_name,
169 name => $curried_name,
172 $attr->associate_method($method);
173 $class->add_method($curried_name => $method);
177 foreach my $key (keys %{$attr->provides}) {
179 my $method_name = $attr->provides->{$key};
181 if ($class->has_method($method_name)) {
182 confess "The method ($method_name) already exists in class (" . $class->name . ")";
185 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
186 $method_constructors->{$key}->(
191 package_name => $class_name,
192 name => $method_name,
195 $attr->associate_method($method);
196 $class->add_method($method_name => $method);
200 after 'remove_accessors' => sub {
202 my $class = $attr->associated_class;
205 foreach my $key (keys %{$attr->provides}) {
206 my $method_name = $attr->provides->{$key};
207 my $method = $class->get_method($method_name);
208 $class->remove_method($method_name)
209 if blessed($method) &&
210 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
214 foreach my $key (keys %{$attr->curries}) {
215 my $method_name = $attr->curries->{$key};
216 my $method = $class->get_method($method_name);
217 $class->remove_method($method_name)
218 if blessed($method) &&
219 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
224 no Moose::Util::TypeConstraints;
232 MooseX::AttributeHelpers::Trait::Base - base role for helpers
236 =head2 check_provides_values
238 Confirms that provides (and curries) has all valid possibilities in it.
240 =head2 process_options_for_provides
242 Ensures that the type constraint (C<isa>) matches the helper type.
246 All complex software has bugs lurking in it, and this module is no
247 exception. If you find a bug please either email me, or add the bug
258 =head1 COPYRIGHT AND LICENSE
260 Copyright 2007-2009 by Infinity Interactive, Inc.
262 L<http://www.iinteractive.com>
264 This library is free software; you can redistribute it and/or modify
265 it under the same terms as Perl itself.