more tests passing
[gitmo/Moose.git] / lib / Moose / AttributeHelpers / Trait / Base.pm
CommitLineData
e3c07b19 1
2package Moose::AttributeHelpers::Trait::Base;
3use Moose::Role;
4use Moose::Util::TypeConstraints;
5
6our $VERSION = '0.19';
7$VERSION = eval $VERSION;
8our $AUTHORITY = 'cpan:STEVAN';
9
10requires 'helper_type';
11
e3c07b19 12# these next two are the possible methods
d09498fb 13# you can use in the 'handles' map.
e3c07b19 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
23has '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 ...
42has '+default' => (required => 1);
43has '+type_constraint' => (required => 1);
44
45## Methods called prior to instantiation
46
d09498fb 47sub process_options_for_handles {
e3c07b19 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
65before '_process_options' => sub {
66 my ($self, $name, $options) = @_;
d09498fb 67 $self->process_options_for_handles($options, $name);
e3c07b19 68};
69
5404f169 70around '_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
e3c07b19 87## methods called after instantiation
88
40ef30a5 89before 'install_accessors' => sub { (shift)->check_handles_values };
5404f169 90
d09498fb 91sub check_handles_values {
e3c07b19 92 my $self = shift;
93
94 my $method_constructors = $self->method_constructors;
95
5404f169 96 my %handles = $self->_canonicalize_handles;
e3c07b19 97
5404f169 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";
e3c07b19 102 }
e3c07b19 103
5404f169 104}
e3c07b19 105
18281451 106around '_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
e3c07b19 132no Moose::Role;
133no Moose::Util::TypeConstraints;
134
1351;
136
137__END__
138
139=head1 NAME
140
141Moose::AttributeHelpers::Trait::Base - base role for helpers
142
143=head1 METHODS
144
d09498fb 145=head2 check_handles_values
e3c07b19 146
d09498fb 147Confirms that handles has all valid possibilities in it.
e3c07b19 148
d09498fb 149=head2 process_options_for_handles
e3c07b19 150
151Ensures that the type constraint (C<isa>) matches the helper type.
152
153=head1 BUGS
154
155All complex software has bugs lurking in it, and this module is no
156exception. If you find a bug please either email me, or add the bug
157to cpan-RT.
158
159=head1 AUTHORS
160
161Yuval Kogman
162
163Shawn M Moore
164
165Jesse Luehrs
166
167=head1 COPYRIGHT AND LICENSE
168
169Copyright 2007-2009 by Infinity Interactive, Inc.
170
171L<http://www.iinteractive.com>
172
173This library is free software; you can redistribute it and/or modify
174it under the same terms as Perl itself.
175
176=cut