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') ) {
58 = Moose::Util::TypeConstraints::find_or_create_type_constraint(
62 ( $isa->is_a_type_of($type) )
64 "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
68 before '_process_options' => sub {
69 my ( $self, $name, $options ) = @_;
70 $self->process_options_for_handles( $options, $name );
73 around '_canonicalize_handles' => sub {
76 my $handles = $self->handles;
78 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_accessors' => 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";
111 around '_make_delegation_method' => sub {
113 my ( $self, $handle_name, $method_to_call ) = @_;
115 my ( $name, $curried_args ) = @$method_to_call;
117 $curried_args ||= [];
119 my $method_constructors = $self->method_constructors;
121 my $code = $method_constructors->{$name}->(
123 $self->get_read_method_ref,
124 $self->get_write_method_ref,
131 my $instance = shift;
132 return $code->( $instance, @$curried_args, @_ );
138 no Moose::Util::TypeConstraints;
146 Moose::AttributeHelpers::Trait::Base - base role for helpers
150 =head2 check_handles_values
152 Confirms that handles has all valid possibilities in it.
154 =head2 process_options_for_handles
156 Ensures that the type constraint (C<isa>) matches the helper type.
160 All complex software has bugs lurking in it, and this module is no
161 exception. If you find a bug please either email me, or add the bug
172 =head1 COPYRIGHT AND LICENSE
174 Copyright 2007-2009 by Infinity Interactive, Inc.
176 L<http://www.iinteractive.com>
178 This library is free software; you can redistribute it and/or modify
179 it under the same terms as Perl itself.