start replacing provides/curries with handles
[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
70## methods called after instantiation
71
d09498fb 72sub check_handles_values {
e3c07b19 73 my $self = shift;
74
75 my $method_constructors = $self->method_constructors;
76
d09498fb 77 foreach my $key (keys %{$self->handles}) {
e3c07b19 78 (exists $method_constructors->{$key})
79 || confess "$key is an unsupported method type";
80 }
81
e3c07b19 82}
83
84after 'install_accessors' => sub {
85 my $attr = shift;
86 my $class = $attr->associated_class;
87
88 # grab the reader and writer methods
89 # as well, this will be useful for
90 # our method provider constructors
91 my $attr_reader = $attr->get_read_method_ref;
92 my $attr_writer = $attr->get_write_method_ref;
93
e3c07b19 94 # before we install them, lets
95 # make sure they are valid
d09498fb 96 $attr->check_handles_values;
e3c07b19 97
98 my $method_constructors = $attr->method_constructors;
99
100 my $class_name = $class->name;
101
d09498fb 102 foreach my $key (keys %{$attr->handles}) {
e3c07b19 103
d09498fb 104 my $method_name = $attr->handles->{$key};
e3c07b19 105
106 if ($class->has_method($method_name)) {
107 confess "The method ($method_name) already exists in class (" . $class->name . ")";
108 }
109
110 my $method = Moose::AttributeHelpers::Meta::Method::Provided->wrap(
111 $method_constructors->{$key}->(
112 $attr,
113 $attr_reader,
114 $attr_writer,
115 ),
116 package_name => $class_name,
117 name => $method_name,
118 );
119
120 $attr->associate_method($method);
121 $class->add_method($method_name => $method);
122 }
123};
124
125after 'remove_accessors' => sub {
126 my $attr = shift;
127 my $class = $attr->associated_class;
128
129 # provides accessors
d09498fb 130 foreach my $key (keys %{$attr->handles}) {
131 my $method_name = $attr->handles->{$key};
e3c07b19 132 my $method = $class->get_method($method_name);
133 $class->remove_method($method_name)
134 if blessed($method) &&
135 $method->isa('Moose::AttributeHelpers::Meta::Method::Provided');
136 }
137};
138
139no Moose::Role;
140no Moose::Util::TypeConstraints;
141
1421;
143
144__END__
145
146=head1 NAME
147
148Moose::AttributeHelpers::Trait::Base - base role for helpers
149
150=head1 METHODS
151
d09498fb 152=head2 check_handles_values
e3c07b19 153
d09498fb 154Confirms that handles has all valid possibilities in it.
e3c07b19 155
d09498fb 156=head2 process_options_for_handles
e3c07b19 157
158Ensures that the type constraint (C<isa>) matches the helper type.
159
160=head1 BUGS
161
162All complex software has bugs lurking in it, and this module is no
163exception. If you find a bug please either email me, or add the bug
164to cpan-RT.
165
166=head1 AUTHORS
167
168Yuval Kogman
169
170Shawn M Moore
171
172Jesse Luehrs
173
174=head1 COPYRIGHT AND LICENSE
175
176Copyright 2007-2009 by Infinity Interactive, Inc.
177
178L<http://www.iinteractive.com>
179
180This library is free software; you can redistribute it and/or modify
181it under the same terms as Perl itself.
182
183=cut