some new Array methods and some pod-coverage cleanup
[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
457dc4fb 6our $VERSION = '0.02';
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
19# these next two are the possible methods
20# you can use in the 'provides' map.
21
22# provide a Class or Role which we can
23# collect the method providers from
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
32# the methods from a method_provider as well
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 +{
43 map {
44 $_ => $method_provider->get_method($_)
45 } $method_provider->get_method_list
46 };
e295d072 47 },
8e3fab6d 48);
49
8881a8d3 50# extend the parents stuff to make sure
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) = @_;
21c7045b 61
8ba40fb0 62 if (my $type = $self->helper_type) {
63 (exists $options->{isa})
64 || confess "You must define a type with the $type metaclass";
65
8c651099 66 my $isa = $options->{isa};
8ba40fb0 67
68 unless (blessed($isa) && $isa->isa('Moose::Meta::TypeConstraint')) {
69 $isa = find_type_constraint($isa);
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
84# this confirms that provides has
85# all valid possibilities in it
86sub check_provides_values {
87 my $self = shift;
8e3fab6d 88
8ba40fb0 89 my $method_constructors = $self->method_constructors;
8e3fab6d 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;
457dc4fb 100
101 # grab the reader and writer methods
102 # as well, this will be useful for
103 # our method provider constructors
104 my ($attr_reader, $attr_writer);
105 if (my $reader = $attr->get_read_method) {
106 $attr_reader = $class->get_method($reader);
107 }
108 else {
109 $attr_reader = sub { $attr->get_value(@_) };
110 }
111
112 if (my $writer = $attr->get_write_method) {
113 $attr_writer = $class->get_method($writer);
114 }
115 else {
116 $attr_writer = sub { $attr->set_value(@_) };
117 }
d26633fc 118
88aaf2bd 119 # before we install them, lets
120 # make sure they are valid
8ba40fb0 121 $attr->check_provides_values;
88aaf2bd 122
d26633fc 123 my $method_constructors = $attr->method_constructors;
124
125 foreach my $key (keys %{$attr->provides}) {
8f7951c9 126
457dc4fb 127 my $method_name = $attr->provides->{$key};
128 my $method_body = $method_constructors->{$key}->(
129 $attr,
130 $attr_reader,
131 $attr_writer,
132 );
8f7951c9 133
134 if ($class->has_method($method_name)) {
135 confess "The method ($method_name) already exists in class (" . $class->name . ")";
136 }
137
138 $class->add_method($method_name =>
139 MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
140 $method_body,
141 )
d26633fc 142 );
143 }
144};
145
146no Moose;
8ba40fb0 147no Moose::Util::TypeConstraints;
d26633fc 148
1491;
150
151__END__
152
153=pod
154
155=head1 NAME
156
5431dff2 157MooseX::AttributeHelpers::Base - Base class for attribute helpers
d26633fc 158
159=head1 DESCRIPTION
160
5431dff2 161Documentation to come.
e295d072 162
163=head1 ATTRIBUTES
164
5431dff2 165=over 4
166
167=item B<provides>
e295d072 168
5431dff2 169=item B<method_provider>
e295d072 170
5431dff2 171=item B<method_constructors>
172
173=back
e295d072 174
175=head1 EXTENDED ATTRIBUTES
176
5431dff2 177=over 4
178
179=item B<$!default>
e295d072 180
181C<$!default> is now required.
182
5431dff2 183=item B<type_constraint>
e295d072 184
185C<type_constraint> is now required.
186
5431dff2 187=back
188
d26633fc 189=head1 METHODS
190
5431dff2 191=over 4
192
b91f57af 193=item B<meta>
194
5431dff2 195=item B<helper_type>
196
197=item B<check_provides_values>
198
199=item B<has_default>
200
201=item B<has_method_provider>
202
203=item B<has_type_constraint>
204
205=item B<install_accessors>
206
207=item B<process_options_for_provides>
208
209=back
e295d072 210
d26633fc 211=head1 BUGS
212
213All complex software has bugs lurking in it, and this module is no
214exception. If you find a bug please either email me, or add the bug
215to cpan-RT.
216
217=head1 AUTHOR
218
219Stevan Little E<lt>stevan@iinteractive.comE<gt>
220
221=head1 COPYRIGHT AND LICENSE
222
223Copyright 2007 by Infinity Interactive, Inc.
224
225L<http://www.iinteractive.com>
226
227This library is free software; you can redistribute it and/or modify
228it under the same terms as Perl itself.
229
8a9cea9b 230=cut