Merge branch 'master' into attribute_helpers
[gitmo/Moose.git] / lib / Moose / AttributeHelpers / Trait / Base.pm
1
2 package Moose::AttributeHelpers::Trait::Base;
3 use Moose::Role;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.84';
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 requires 'helper_type';
11
12 # these next two are the possible methods
13 # you can use in the 'handles' map.
14
15 # provide a Class or Role which we can
16 # collect the method providers from
17
18 # requires_attr 'method_provider'
19
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' => (
24     is      => 'ro',
25     isa     => 'HashRef',
26     lazy    => 1,
27     default => sub {
28         my $self = shift;
29         return +{} unless $self->has_method_provider;
30         # or grab them from the role/class
31         my $method_provider = $self->method_provider->meta;
32         return +{
33             map {
34                 $_ => $method_provider->get_method($_)
35             } $method_provider->get_method_list
36         };
37     },
38 );
39
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 );
44
45 ## Methods called prior to instantiation
46
47 sub process_options_for_handles {
48     my ( $self, $options ) = @_;
49
50     if ( my $type = $self->helper_type ) {
51         ( exists $options->{isa} )
52             || confess "You must define a type with the $type metaclass";
53
54         my $isa = $options->{isa};
55
56         unless ( blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint') ) {
57             $isa
58                 = Moose::Util::TypeConstraints::find_or_create_type_constraint(
59                 $isa);
60         }
61
62         ( $isa->is_a_type_of($type) )
63             || confess
64             "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
65     }
66 }
67
68 before '_process_options' => sub {
69     my ( $self, $name, $options ) = @_;
70     $self->process_options_for_handles( $options, $name );
71 };
72
73 around '_canonicalize_handles' => sub {
74     my $next    = shift;
75     my $self    = shift;
76     my $handles = $self->handles;
77
78     return unless $handles;
79
80     unless ( 'HASH' eq ref $handles ) {
81         $self->throw_error(
82             "The 'handles' option must be a HASH reference, not $handles" );
83     }
84
85     return map {
86         my $to = $handles->{$_};
87         $to = [$to] unless ref $to;
88         $_ => $to
89     } keys %$handles;
90 };
91
92 ## methods called after instantiation
93
94 before 'install_accessors' => sub { (shift)->check_handles_values };
95
96 sub check_handles_values {
97     my $self = shift;
98
99     my $method_constructors = $self->method_constructors;
100
101     my %handles = $self->_canonicalize_handles;
102
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";
107     }
108
109 }
110
111 around '_make_delegation_method' => sub {
112     my $next = shift;
113     my ( $self, $handle_name, $method_to_call ) = @_;
114
115     my ( $name, $curried_args ) = @$method_to_call;
116
117     $curried_args ||= [];
118
119     my $method_constructors = $self->method_constructors;
120
121     my $code = $method_constructors->{$name}->(
122         $self,
123         $self->get_read_method_ref,
124         $self->get_write_method_ref,
125     );
126
127     return $next->(
128         $self,
129         $handle_name,
130         sub {
131             my $instance = shift;
132             return $code->( $instance, @$curried_args, @_ );
133         },
134     );
135 };
136
137 no Moose::Role;
138 no Moose::Util::TypeConstraints;
139
140 1;
141
142 __END__
143
144 =head1 NAME
145
146 Moose::AttributeHelpers::Trait::Base - base role for helpers
147
148 =head1 METHODS
149
150 =head2 check_handles_values
151
152 Confirms that handles has all valid possibilities in it.
153
154 =head2 process_options_for_handles
155
156 Ensures that the type constraint (C<isa>) matches the helper type.
157
158 =head1 BUGS
159
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
162 to cpan-RT.
163
164 =head1 AUTHORS
165
166 Yuval Kogman
167
168 Shawn M Moore
169
170 Jesse Luehrs
171
172 =head1 COPYRIGHT AND LICENSE
173
174 Copyright 2007-2009 by Infinity Interactive, Inc.
175
176 L<http://www.iinteractive.com>
177
178 This library is free software; you can redistribute it and/or modify
179 it under the same terms as Perl itself.
180
181 =cut