more tests passing
[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.19';
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 = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
58         }
59
60         ($isa->is_a_type_of($type))
61             || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
62     }
63 }
64
65 before '_process_options' => sub {
66     my ($self, $name, $options) = @_;
67     $self->process_options_for_handles($options, $name);
68 };
69
70 around '_canonicalize_handles' => sub {
71     my $next    = shift;
72     my $self    = shift;
73     my $handles = $self->handles;
74     return unless $handles;
75     unless ('HASH' eq ref $handles) {
76         $self->throw_error(
77             "The 'handles' option must be a HASH reference, not $handles"
78         );
79     }
80     return map {
81         my $to = $handles->{$_};
82         $to = [ $to ] unless ref $to;
83         $_ => $to
84     } keys %$handles;
85 };
86
87 ## methods called after instantiation
88
89 before 'install_accessors' => sub { (shift)->check_handles_values };
90
91 sub check_handles_values {
92     my $self = shift;
93
94     my $method_constructors = $self->method_constructors;
95
96     my %handles = $self->_canonicalize_handles;
97
98     for my $original_method (values %handles) {
99         my $name = $original_method->[0];
100         (exists $method_constructors->{$name})
101             || confess "$name is an unsupported method type";
102     }
103
104 }
105
106 around '_make_delegation_method' => sub {
107     my $next = shift;
108     my ($self, $handle_name, $method_to_call) = @_;
109
110     my ($name, $curried_args) = @$method_to_call;
111
112     $curried_args ||= [];
113
114     my $method_constructors = $self->method_constructors;
115
116     my $code = $method_constructors->{$name}->(
117         $self,
118         $self->get_read_method_ref,
119         $self->get_write_method_ref,
120     );
121
122     return $next->(
123         $self,
124         $handle_name,
125         sub {
126             my $instance = shift;
127             return $code->($instance, @$curried_args, @_);
128         },
129     );
130 };
131
132 no Moose::Role;
133 no Moose::Util::TypeConstraints;
134
135 1;
136
137 __END__
138
139 =head1 NAME
140
141 Moose::AttributeHelpers::Trait::Base - base role for helpers
142
143 =head1 METHODS
144
145 =head2 check_handles_values
146
147 Confirms that handles has all valid possibilities in it.
148
149 =head2 process_options_for_handles
150
151 Ensures that the type constraint (C<isa>) matches the helper type.
152
153 =head1 BUGS
154
155 All complex software has bugs lurking in it, and this module is no
156 exception. If you find a bug please either email me, or add the bug
157 to cpan-RT.
158
159 =head1 AUTHORS
160
161 Yuval Kogman
162
163 Shawn M Moore
164
165 Jesse Luehrs
166
167 =head1 COPYRIGHT AND LICENSE
168
169 Copyright 2007-2009 by Infinity Interactive, Inc.
170
171 L<http://www.iinteractive.com>
172
173 This library is free software; you can redistribute it and/or modify
174 it under the same terms as Perl itself.
175
176 =cut