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 ...
19 # these next two are the possible methods
20 # you can use in the 'provides' map.
22 # provide a Class or Role which we can
23 # collect the method providers from
24 has 'method_provider' => (
27 predicate => 'has_method_provider',
30 # or you can provide a HASH ref of anon subs
31 # yourself. This will also collect and store
32 # the methods from a method_provider as well
33 has 'method_constructors' => (
39 return +{} unless $self->has_method_provider;
40 # or grab them from the role/class
41 my $method_provider = $self->method_provider->meta;
44 $_ => $method_provider->get_method($_)
45 } $method_provider->get_method_list
50 # extend the parents stuff to make sure
51 # certain bits are now required ...
52 has '+$!default' => (required => 1);
53 has '+type_constraint' => (required => 1);
55 ## Methods called prior to instantiation
57 sub helper_type { () }
59 sub process_options_for_provides {
60 my ($self, $options) = @_;
62 if (my $type = $self->helper_type) {
63 (exists $options->{isa})
64 || confess "You must define a type with the $type metaclass";
66 my $isa = $options->{isa};
68 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
69 $isa = find_type_constraint($isa);
72 ($isa->is_a_type_of($type))
73 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
77 before '_process_options' => sub {
78 my ($self, $name, $options) = @_;
79 $self->process_options_for_provides($options, $name);
82 ## methods called after instantiation
84 # this confirms that provides has
85 # all valid possibilities in it
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";
97 after 'install_accessors' => sub {
99 my $class = $attr->associated_class;
101 # grab the reader and writer methods
102 # as well, this will be useful for
103 # our method provider constructors
104 my ($attr_reader, $attr_writer);
105 if (my $reader = $attr->get_read_method) {
106 $attr_reader = $class->get_method($reader);
109 $attr_reader = sub { $attr->get_value(@_) };
112 if (my $writer = $attr->get_write_method) {
113 $attr_writer = $class->get_method($writer);
116 $attr_writer = sub { $attr->set_value(@_) };
119 # before we install them, lets
120 # make sure they are valid
121 $attr->check_provides_values;
123 my $method_constructors = $attr->method_constructors;
125 foreach my $key (keys %{$attr->provides}) {
127 my $method_name = $attr->provides->{$key};
128 my $method_body = $method_constructors->{$key}->(
134 if ($class->has_method($method_name)) {
135 confess "The method ($method_name) already exists in class (" . $class->name . ")";
138 $class->add_method($method_name =>
139 MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
147 no Moose::Util::TypeConstraints;
157 MooseX::AttributeHelpers::Base - Base class for attribute helpers
161 Documentation to come.
169 =item B<method_provider>
171 =item B<method_constructors>
175 =head1 EXTENDED ATTRIBUTES
181 C<$!default> is now required.
183 =item B<type_constraint>
185 C<type_constraint> is now required.
197 =item B<check_provides_values>
201 =item B<has_method_provider>
203 =item B<has_type_constraint>
205 =item B<install_accessors>
207 =item B<process_options_for_provides>
213 All complex software has bugs lurking in it, and this module is no
214 exception. If you find a bug please either email me, or add the bug
219 Stevan Little E<lt>stevan@iinteractive.comE<gt>
221 =head1 COPYRIGHT AND LICENSE
223 Copyright 2007 by Infinity Interactive, Inc.
225 L<http://www.iinteractive.com>
227 This library is free software; you can redistribute it and/or modify
228 it under the same terms as Perl itself.