0.01
[gitmo/MooseX-AttributeHelpers.git] / lib / MooseX / AttributeHelpers / Base.pm
1
2 package MooseX::AttributeHelpers::Base;
3 use Moose;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.01';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 extends 'Moose::Meta::Attribute';
10
11 # this is the method map you define ...
12 has 'provides' => (
13     is      => 'ro',
14     isa     => 'HashRef',
15     default => sub {{}}
16 );
17
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 
24 has '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 
33 has '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         };            
47     },
48 );
49
50 # extend the parents stuff to make sure 
51 # certain bits are now required ...
52 has '+$!default'       => (required => 1);
53 has '+type_constraint' => (required => 1);
54
55 ## Methods called prior to instantiation
56
57 sub helper_type { () }
58
59 sub process_options_for_provides {
60     my ($self, $options) = @_;
61     
62     if (my $type = $self->helper_type) {
63         (exists $options->{isa})
64             || confess "You must define a type with the $type metaclass";  
65
66         my $isa = $options->{isa};       
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     }
75 }
76
77 before '_process_options' => sub {
78     my ($self, $name, $options) = @_;
79     if (exists $options->{provides} || 
80         exists $options->{isa}      && $options->{isa} =~ /^.*?\[.*?\]$/) {
81         $self->process_options_for_provides($options);
82     }
83 };
84
85 ## methods called after instantiation
86
87 # this confirms that provides has 
88 # all valid possibilities in it
89 sub check_provides_values {
90     my $self = shift;
91     
92     my $method_constructors = $self->method_constructors;
93     
94     foreach my $key (keys %{$self->provides}) {
95         (exists $method_constructors->{$key})
96             || confess "$key is an unsupported method type";
97     }
98 }
99
100 after 'install_accessors' => sub {
101     my $attr  = shift;
102     my $class = $attr->associated_class;
103
104     # before we install them, lets
105     # make sure they are valid
106     $attr->check_provides_values;    
107
108     my $method_constructors = $attr->method_constructors;
109     
110     foreach my $key (keys %{$attr->provides}) {
111         
112         my $method_name = $attr->provides->{$key};
113         my $method_body = $method_constructors->{$key}->($attr);
114         
115         if ($class->has_method($method_name)) {
116             confess "The method ($method_name) already exists in class (" . $class->name . ")";
117         }
118         
119         $class->add_method($method_name => 
120             MooseX::AttributeHelpers::Meta::Method::Provided->wrap(
121                 $method_body,
122             )
123         );
124     }
125 };
126
127 no Moose;
128 no Moose::Util::TypeConstraints;
129
130 1;
131
132 __END__
133
134 =pod
135
136 =head1 NAME
137
138 MooseX::AttributeHelpers::Base - Base class for attribute helpers
139   
140 =head1 DESCRIPTION
141
142 Documentation to come.
143
144 =head1 ATTRIBUTES
145
146 =over 4
147
148 =item B<provides>
149
150 =item B<method_provider>
151
152 =item B<method_constructors>
153
154 =back
155
156 =head1 EXTENDED ATTRIBUTES
157
158 =over 4
159
160 =item B<$!default>
161
162 C<$!default> is now required.
163
164 =item B<type_constraint>
165
166 C<type_constraint> is now required.
167
168 =back
169
170 =head1 METHODS
171
172 =over 4
173
174 =item B<helper_type>
175
176 =item B<check_provides_values>
177
178 =item B<has_default>
179
180 =item B<has_method_provider>
181
182 =item B<has_type_constraint>
183
184 =item B<install_accessors>
185
186 =item B<process_options_for_provides>
187
188 =back
189
190 =head1 BUGS
191
192 All complex software has bugs lurking in it, and this module is no 
193 exception. If you find a bug please either email me, or add the bug
194 to cpan-RT.
195
196 =head1 AUTHOR
197
198 Stevan Little E<lt>stevan@iinteractive.comE<gt>
199
200 =head1 COPYRIGHT AND LICENSE
201
202 Copyright 2007 by Infinity Interactive, Inc.
203
204 L<http://www.iinteractive.com>
205
206 This library is free software; you can redistribute it and/or modify
207 it under the same terms as Perl itself.
208
209 =cut