2 package Moose::AttributeHelpers::Trait::Base;
4 use Moose::Util::TypeConstraints;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
10 requires 'helper_type';
12 # these next two are the possible methods
13 # you can use in the 'handles' map.
15 # provide a Class or Role which we can
16 # collect the method providers from
18 # requires_attr 'method_provider'
20 # or you can provide a HASH ref of anon subs
21 # yourself. This will also collect and store
22 # the methods from a method_provider as well
23 has 'method_constructors' => (
29 return +{} unless $self->has_method_provider;
30 # or grab them from the role/class
31 my $method_provider = $self->method_provider->meta;
34 $_ => $method_provider->get_method($_)
35 } $method_provider->get_method_list
40 # extend the parents stuff to make sure
41 # certain bits are now required ...
42 has '+default' => (required => 1);
43 has '+type_constraint' => (required => 1);
45 ## Methods called prior to instantiation
47 sub process_options_for_handles {
48 my ($self, $options) = @_;
50 if (my $type = $self->helper_type) {
51 (exists $options->{isa})
52 || confess "You must define a type with the $type metaclass";
54 my $isa = $options->{isa};
56 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
57 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
60 ($isa->is_a_type_of($type))
61 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
65 before '_process_options' => sub {
66 my ($self, $name, $options) = @_;
67 $self->process_options_for_handles($options, $name);
70 ## methods called after instantiation
72 sub check_handles_values {
75 my $method_constructors = $self->method_constructors;
77 foreach my $key (keys %{$self->handles}) {
78 (exists $method_constructors->{$key})
79 || confess "$key is an unsupported method type";
84 after 'install_accessors' => sub {
86 my $class = $attr->associated_class;
88 # grab the reader and writer methods
89 # as well, this will be useful for
90 # our method provider constructors
91 my $attr_reader = $attr->get_read_method_ref;
92 my $attr_writer = $attr->get_write_method_ref;
94 # before we install them, lets
95 # make sure they are valid
96 $attr->check_handles_values;
98 my $method_constructors = $attr->method_constructors;
100 my $class_name = $class->name;
102 foreach my $key (keys %{$attr->handles}) {
104 my $method_name = $attr->handles->{$key};
106 if ($class->has_method($method_name)) {
107 confess "The method ($method_name) already exists in class (" . $class->name . ")";
110 my $method = Moose::AttributeHelpers::Meta::Method::Provided->wrap(
111 $method_constructors->{$key}->(
116 package_name => $class_name,
117 name => $method_name,
120 $attr->associate_method($method);
121 $class->add_method($method_name => $method);
125 after 'remove_accessors' => sub {
127 my $class = $attr->associated_class;
130 foreach my $key (keys %{$attr->handles}) {
131 my $method_name = $attr->handles->{$key};
132 my $method = $class->get_method($method_name);
133 $class->remove_method($method_name)
134 if blessed($method) &&
135 $method->isa('Moose::AttributeHelpers::Meta::Method::Provided');
140 no Moose::Util::TypeConstraints;
148 Moose::AttributeHelpers::Trait::Base - base role for helpers
152 =head2 check_handles_values
154 Confirms that handles has all valid possibilities in it.
156 =head2 process_options_for_handles
158 Ensures that the type constraint (C<isa>) matches the helper type.
162 All complex software has bugs lurking in it, and this module is no
163 exception. If you find a bug please either email me, or add the bug
174 =head1 COPYRIGHT AND LICENSE
176 Copyright 2007-2009 by Infinity Interactive, Inc.
178 L<http://www.iinteractive.com>
180 This library is free software; you can redistribute it and/or modify
181 it under the same terms as Perl itself.