allow method => $coderef for a curries parameter
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Base.pm
CommitLineData
d26633fc 1
2package MooseX::AttributeHelpers::Base;
3use Moose;
8ba40fb0 4use Moose::Util::TypeConstraints;
d26633fc 5
999f34a9 6our $VERSION = '0.04';
d26633fc 7our $AUTHORITY = 'cpan:STEVAN';
8
9extends 'Moose::Meta::Attribute';
10
8e3fab6d 11# this is the method map you define ...
d26633fc 12has 'provides' => (
9810162d 13 is => 'ro',
14 isa => 'HashRef',
15 default => sub {{}}
d26633fc 16);
17
c43a2317 18has 'curries' => (
19 is => 'ro',
20 isa => 'HashRef',
21 default => sub {{}}
22);
23
8e3fab6d 24
999f34a9 25# these next two are the possible methods
8e3fab6d 26# you can use in the 'provides' map.
27
999f34a9 28# provide a Class or Role which we can
29# collect the method providers from
8e3fab6d 30has 'method_provider' => (
31 is => 'ro',
32 isa => 'ClassName',
33 predicate => 'has_method_provider',
34);
35
36# or you can provide a HASH ref of anon subs
37# yourself. This will also collect and store
999f34a9 38# the methods from a method_provider as well
8e3fab6d 39has 'method_constructors' => (
40 is => 'ro',
41 isa => 'HashRef',
42 lazy => 1,
43 default => sub {
44 my $self = shift;
45 return +{} unless $self->has_method_provider;
46 # or grab them from the role/class
47 my $method_provider = $self->method_provider->meta;
48 return +{
999f34a9 49 map {
8e3fab6d 50 $_ => $method_provider->get_method($_)
51 } $method_provider->get_method_list
999f34a9 52 };
e295d072 53 },
8e3fab6d 54);
55
999f34a9 56# extend the parents stuff to make sure
8881a8d3 57# certain bits are now required ...
d26633fc 58has '+$!default' => (required => 1);
59has '+type_constraint' => (required => 1);
60
8ba40fb0 61## Methods called prior to instantiation
62
63sub helper_type { () }
d26633fc 64
8ba40fb0 65sub process_options_for_provides {
d26633fc 66 my ($self, $options) = @_;
999f34a9 67
8ba40fb0 68 if (my $type = $self->helper_type) {
69 (exists $options->{isa})
999f34a9 70 || confess "You must define a type with the $type metaclass";
8ba40fb0 71
999f34a9 72 my $isa = $options->{isa};
8ba40fb0 73
74 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
999f34a9 75 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
8ba40fb0 76 }
77
78 ($isa->is_a_type_of($type))
79 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
80 }
d26633fc 81}
82
83before '_process_options' => sub {
88aaf2bd 84 my ($self, $name, $options) = @_;
38abf787 85 $self->process_options_for_provides($options, $name);
d26633fc 86};
87
8ba40fb0 88## methods called after instantiation
89
999f34a9 90# this confirms that provides has
8ba40fb0 91# all valid possibilities in it
92sub check_provides_values {
93 my $self = shift;
999f34a9 94
8ba40fb0 95 my $method_constructors = $self->method_constructors;
999f34a9 96
8ba40fb0 97 foreach my $key (keys %{$self->provides}) {
98 (exists $method_constructors->{$key})
99 || confess "$key is an unsupported method type";
100 }
101}
102
c43a2317 103sub _curry {
104 my $self = shift;
105 my $code = shift;
106
c43a2317 107 my @args = @_;
108 return sub { my $self = shift; $code->($self, @args, @_) };
109}
110
696d4dc7 111sub _curry_sub {
112 my $self = shift;
113 my $body = shift;
114 my $code = shift;
115
116 warn "installing sub!";
117
118 return sub { my $self = shift; $code->($self, $body, @_) };
119}
120
d26633fc 121after 'install_accessors' => sub {
122 my $attr = shift;
123 my $class = $attr->associated_class;
999f34a9 124
457dc4fb 125 # grab the reader and writer methods
999f34a9 126 # as well, this will be useful for
457dc4fb 127 # our method provider constructors
9a976497 128 my $attr_reader = $attr->get_read_method_ref;
129 my $attr_writer = $attr->get_write_method_ref;
999f34a9 130
d26633fc 131
88aaf2bd 132 # before we install them, lets
133 # make sure they are valid
999f34a9 134 $attr->check_provides_values;
c43a2317 135# $attr->check_curries_values;
88aaf2bd 136
d26633fc 137 my $method_constructors = $attr->method_constructors;
999f34a9 138
dc4333e9 139 my $class_name = $class->name;
140
3656a0d7 141 while (my ($constructor, $constructed) = each %{$attr->curries}) {
142 my $method_code;
696d4dc7 143 while (my ($curried_name, $curried_arg) = each(%$constructed)) {
144 if ($class->has_method($curried_name)) {
145 confess
146 "The method ($curried_name) already ".
147 "exists in class (" . $class->name . ")";
3656a0d7 148 }
696d4dc7 149 my $body = $method_constructors->{$constructor}->(
150 $attr,
151 $attr_reader,
152 $attr_writer,
153 );
154
155 if (ref $curried_arg eq 'ARRAY') {
156 $method_code = $attr->_curry($body, @$curried_arg);
157 }
158 elsif (ref $curried_arg eq 'CODE') {
159 $method_code = $attr->_curry_sub($body, $curried_arg);
160 }
161 else {
162 confess "curries parameter must be ref type HASH or CODE";
163 }
164
165 my $method = MooseX::AttributeHelpers::Meta::Method::Curried->wrap(
166 $method_code,
167 package_name => $class_name,
168 name => $curried_name,
169 );
170
171 $attr->associate_method($method);
172 $class->add_method($curried_name => $method);
c43a2317 173 }
c43a2317 174 }
175
d26633fc 176 foreach my $key (keys %{$attr->provides}) {
999f34a9 177
178 my $method_name = $attr->provides->{$key};
179
9a976497 180 if ($class->has_method($method_name)) {
181 confess "The method ($method_name) already exists in class (" . $class->name . ")";
999f34a9 182 }
183
184 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
185 $method_constructors->{$key}->(
186 $attr,
187 $attr_reader,
188 $attr_writer,
dc4333e9 189 ),
190 package_name => $class_name,
191 name => $method_name,
d26633fc 192 );
999f34a9 193
194 $attr->associate_method($method);
195 $class->add_method($method_name => $method);
196 }
197};
198
199after 'remove_accessors' => sub {
200 my $attr = shift;
201 my $class = $attr->associated_class;
202 foreach my $key (keys %{$attr->provides}) {
203 my $method_name = $attr->provides->{$key};
204 my $method = $class->get_method($method_name);
205 $class->remove_method($method_name)
206 if blessed($method) &&
207 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
d26633fc 208 }
209};
210
211no Moose;
8ba40fb0 212no Moose::Util::TypeConstraints;
d26633fc 213
2141;
215
216__END__
217
218=pod
219
220=head1 NAME
221
5431dff2 222MooseX::AttributeHelpers::Base - Base class for attribute helpers
999f34a9 223
d26633fc 224=head1 DESCRIPTION
225
5431dff2 226Documentation to come.
e295d072 227
228=head1 ATTRIBUTES
229
5431dff2 230=over 4
231
232=item B<provides>
e295d072 233
5431dff2 234=item B<method_provider>
e295d072 235
5431dff2 236=item B<method_constructors>
237
238=back
e295d072 239
240=head1 EXTENDED ATTRIBUTES
241
5431dff2 242=over 4
243
244=item B<$!default>
e295d072 245
246C<$!default> is now required.
247
5431dff2 248=item B<type_constraint>
e295d072 249
250C<type_constraint> is now required.
251
5431dff2 252=back
253
d26633fc 254=head1 METHODS
255
5431dff2 256=over 4
257
b91f57af 258=item B<meta>
259
5431dff2 260=item B<helper_type>
261
262=item B<check_provides_values>
263
264=item B<has_default>
265
266=item B<has_method_provider>
267
268=item B<has_type_constraint>
269
270=item B<install_accessors>
271
999f34a9 272=item B<remove_accessors>
273
5431dff2 274=item B<process_options_for_provides>
275
276=back
e295d072 277
d26633fc 278=head1 BUGS
279
999f34a9 280All complex software has bugs lurking in it, and this module is no
d26633fc 281exception. If you find a bug please either email me, or add the bug
282to cpan-RT.
283
284=head1 AUTHOR
285
286Stevan Little E<lt>stevan@iinteractive.comE<gt>
287
288=head1 COPYRIGHT AND LICENSE
289
99c62fb8 290Copyright 2007-2008 by Infinity Interactive, Inc.
d26633fc 291
292L<http://www.iinteractive.com>
293
294This library is free software; you can redistribute it and/or modify
295it under the same terms as Perl itself.
296
8a9cea9b 297=cut