Revert fixing of empty for now
[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
8e3fab6d 18
999f34a9 19# these next two are the possible methods
8e3fab6d 20# you can use in the 'provides' map.
21
999f34a9 22# provide a Class or Role which we can
23# collect the method providers from
8e3fab6d 24has 'method_provider' => (
25 is => 'ro',
26 isa => 'ClassName',
27 predicate => 'has_method_provider',
28);
29
30# or you can provide a HASH ref of anon subs
31# yourself. This will also collect and store
999f34a9 32# the methods from a method_provider as well
8e3fab6d 33has 'method_constructors' => (
34 is => 'ro',
35 isa => 'HashRef',
36 lazy => 1,
37 default => sub {
38 my $self = shift;
39 return +{} unless $self->has_method_provider;
40 # or grab them from the role/class
41 my $method_provider = $self->method_provider->meta;
42 return +{
999f34a9 43 map {
8e3fab6d 44 $_ => $method_provider->get_method($_)
45 } $method_provider->get_method_list
999f34a9 46 };
e295d072 47 },
8e3fab6d 48);
49
999f34a9 50# extend the parents stuff to make sure
8881a8d3 51# certain bits are now required ...
d26633fc 52has '+$!default' => (required => 1);
53has '+type_constraint' => (required => 1);
54
8ba40fb0 55## Methods called prior to instantiation
56
57sub helper_type { () }
d26633fc 58
8ba40fb0 59sub process_options_for_provides {
d26633fc 60 my ($self, $options) = @_;
999f34a9 61
8ba40fb0 62 if (my $type = $self->helper_type) {
63 (exists $options->{isa})
999f34a9 64 || confess "You must define a type with the $type metaclass";
8ba40fb0 65
999f34a9 66 my $isa = $options->{isa};
8ba40fb0 67
68 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
999f34a9 69 $isa = Moose::Util::TypeConstraints::find_or_create_type_constraint($isa);
8ba40fb0 70 }
71
72 ($isa->is_a_type_of($type))
73 || confess "The type constraint for a $type ($options->{isa}) must be a subtype of $type";
74 }
d26633fc 75}
76
77before '_process_options' => sub {
88aaf2bd 78 my ($self, $name, $options) = @_;
38abf787 79 $self->process_options_for_provides($options, $name);
d26633fc 80};
81
8ba40fb0 82## methods called after instantiation
83
999f34a9 84# this confirms that provides has
8ba40fb0 85# all valid possibilities in it
86sub check_provides_values {
87 my $self = shift;
999f34a9 88
8ba40fb0 89 my $method_constructors = $self->method_constructors;
999f34a9 90
8ba40fb0 91 foreach my $key (keys %{$self->provides}) {
92 (exists $method_constructors->{$key})
93 || confess "$key is an unsupported method type";
94 }
95}
96
d26633fc 97after 'install_accessors' => sub {
98 my $attr = shift;
99 my $class = $attr->associated_class;
999f34a9 100
457dc4fb 101 # grab the reader and writer methods
999f34a9 102 # as well, this will be useful for
457dc4fb 103 # our method provider constructors
9a976497 104 my $attr_reader = $attr->get_read_method_ref;
105 my $attr_writer = $attr->get_write_method_ref;
999f34a9 106
d26633fc 107
88aaf2bd 108 # before we install them, lets
109 # make sure they are valid
999f34a9 110 $attr->check_provides_values;
88aaf2bd 111
d26633fc 112 my $method_constructors = $attr->method_constructors;
999f34a9 113
d26633fc 114 foreach my $key (keys %{$attr->provides}) {
999f34a9 115
116 my $method_name = $attr->provides->{$key};
117
9a976497 118 if ($class->has_method($method_name)) {
119 confess "The method ($method_name) already exists in class (" . $class->name . ")";
999f34a9 120 }
121
122 my $method = MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
123 $method_constructors->{$key}->(
124 $attr,
125 $attr_reader,
126 $attr_writer,
8f7951c9 127 )
d26633fc 128 );
999f34a9 129
130 $attr->associate_method($method);
131 $class->add_method($method_name => $method);
132 }
133};
134
135after 'remove_accessors' => sub {
136 my $attr = shift;
137 my $class = $attr->associated_class;
138 foreach my $key (keys %{$attr->provides}) {
139 my $method_name = $attr->provides->{$key};
140 my $method = $class->get_method($method_name);
141 $class->remove_method($method_name)
142 if blessed($method) &&
143 $method->isa('MooseX::AttributeHelpers::Meta::Method::Provided');
d26633fc 144 }
145};
146
147no Moose;
8ba40fb0 148no Moose::Util::TypeConstraints;
d26633fc 149
1501;
151
152__END__
153
154=pod
155
156=head1 NAME
157
5431dff2 158MooseX::AttributeHelpers::Base - Base class for attribute helpers
999f34a9 159
d26633fc 160=head1 DESCRIPTION
161
5431dff2 162Documentation to come.
e295d072 163
164=head1 ATTRIBUTES
165
5431dff2 166=over 4
167
168=item B<provides>
e295d072 169
5431dff2 170=item B<method_provider>
e295d072 171
5431dff2 172=item B<method_constructors>
173
174=back
e295d072 175
176=head1 EXTENDED ATTRIBUTES
177
5431dff2 178=over 4
179
180=item B<$!default>
e295d072 181
182C<$!default> is now required.
183
5431dff2 184=item B<type_constraint>
e295d072 185
186C<type_constraint> is now required.
187
5431dff2 188=back
189
d26633fc 190=head1 METHODS
191
5431dff2 192=over 4
193
b91f57af 194=item B<meta>
195
5431dff2 196=item B<helper_type>
197
198=item B<check_provides_values>
199
200=item B<has_default>
201
202=item B<has_method_provider>
203
204=item B<has_type_constraint>
205
206=item B<install_accessors>
207
999f34a9 208=item B<remove_accessors>
209
5431dff2 210=item B<process_options_for_provides>
211
212=back
e295d072 213
d26633fc 214=head1 BUGS
215
999f34a9 216All complex software has bugs lurking in it, and this module is no
d26633fc 217exception. If you find a bug please either email me, or add the bug
218to cpan-RT.
219
220=head1 AUTHOR
221
222Stevan Little E<lt>stevan@iinteractive.comE<gt>
223
224=head1 COPYRIGHT AND LICENSE
225
99c62fb8 226Copyright 2007-2008 by Infinity Interactive, Inc.
d26633fc 227
228L<http://www.iinteractive.com>
229
230This library is free software; you can redistribute it and/or modify
231it under the same terms as Perl itself.
232
8a9cea9b 233=cut