2 package Moose::AttributeHelpers::Trait::Base;
4 use Moose::Util::TypeConstraints;
5 use Moose::AttributeHelpers::Meta::Method::Delegation;
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
11 requires 'helper_type';
13 # these next two are the possible methods
14 # you can use in the 'handles' map.
16 # provide a Class or Role which we can
17 # collect the method providers from
19 # requires_attr 'method_provider'
21 # or you can provide a HASH ref of anon subs
22 # yourself. This will also collect and store
23 # the methods from a method_provider as well
24 has 'method_constructors' => (
30 return +{} unless $self->has_method_provider;
31 # or grab them from the role/class
32 my $method_provider = $self->method_provider->meta;
35 $_ => $method_provider->get_method($_)
36 } $method_provider->get_method_list
41 # extend the parents stuff to make sure
42 # certain bits are now required ...
43 has '+default' => (required => 1);
44 has '+type_constraint' => (required => 1);
46 ## Methods called prior to instantiation
48 sub process_options_for_handles {
49 my ($self, $options) = @_;
51 if (my $type = $self->helper_type) {
52 (exists $options->{isa})
53 || confess "You must define a type with the $type metaclass";
55 my $isa = $options->{isa};
57 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
58 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
61 ($isa->is_a_type_of($type))
62 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
66 sub delegation_metaclass {
67 'Moose::AttributeHelpers::Meta::Method::Delegation'
70 before '_process_options' => sub {
71 my ($self, $name, $options) = @_;
72 $self->process_options_for_handles($options, $name);
75 around '_canonicalize_handles' => sub {
78 my $handles = $self->handles;
79 return unless $handles;
80 unless ('HASH' eq ref $handles) {
82 "The 'handles' option must be a HASH reference, not $handles"
86 my $to = $handles->{$_};
87 $to = [ $to ] unless ref $to;
92 ## methods called after instantiation
94 before 'install_delegation' => sub { (shift)->check_handles_values };
96 sub check_handles_values {
99 my $method_constructors = $self->method_constructors;
101 my %handles = $self->_canonicalize_handles;
103 for my $original_method (values %handles) {
104 my $name = $original_method->[0];
105 (exists $method_constructors->{$name})
106 || confess "$name is an unsupported method type";
112 no Moose::Util::TypeConstraints;
120 Moose::AttributeHelpers::Trait::Base - base role for helpers
124 =head2 check_handles_values
126 Confirms that handles has all valid possibilities in it.
128 =head2 process_options_for_handles
130 Ensures that the type constraint (C<isa>) matches the helper type.
134 All complex software has bugs lurking in it, and this module is no
135 exception. If you find a bug please either email me, or add the bug
146 =head1 COPYRIGHT AND LICENSE
148 Copyright 2007-2009 by Infinity Interactive, Inc.
150 L<http://www.iinteractive.com>
152 This library is free software; you can redistribute it and/or modify
153 it under the same terms as Perl itself.