cleaning up and working on the spec a bit
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToClass.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::ToClass;
2
3use strict;
4use warnings;
5use metaclass;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10use Data::Dumper;
11
12our $VERSION = '0.01';
13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Moose::Meta::Role::Application';
16
1c9db35c 17sub apply {
18 my ($self, $role, $class) = @_;
19 $self->SUPER::apply($role, $class);
20 $class->add_role($role);
21}
22
23sub check_role_exclusions {
24 my ($self, $role, $class) = @_;
25 if ($class->excludes_role($role->name)) {
26 confess "Conflict detected: " . $class->name . " excludes role '" . $role->name . "'";
27 }
28 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
29 if ($class->does_role($excluded_role_name)) {
30 confess "The class " . $class->name . " does the excluded role '$excluded_role_name'";
31 }
32 }
33}
34
35sub check_required_methods {
36 my ($self, $role, $class) = @_;
37 # NOTE:
38 # we might need to move this down below the
39 # the attributes so that we can require any
40 # attribute accessors. However I am thinking
41 # that maybe those are somehow exempt from
42 # the require methods stuff.
43 foreach my $required_method_name ($role->get_required_method_list) {
44
45 unless ($class->find_method_by_name($required_method_name)) {
46 confess "'" . $role->name . "' requires the method '$required_method_name' " .
47 "to be implemented by '" . $class->name . "'";
48 }
49 else {
50 # NOTE:
51 # we need to make sure that the method is
52 # not a method modifier, because those do
53 # not satisfy the requirements ...
54 my $method = $class->find_method_by_name($required_method_name);
55
56 # check if it is a generated accessor ...
57 (!$method->isa('Class::MOP::Method::Accessor'))
58 || confess "'" . $role->name . "' requires the method '$required_method_name' " .
59 "to be implemented by '" . $class->name . "', the method is only an attribute accessor";
60
61 # NOTE:
62 # All other tests here have been removed, they were tests
63 # for overriden methods and before/after/around modifiers.
64 # But we realized that for classes any overriden or modified
65 # methods would be backed by a real method of that name
66 # (and therefore meet the requirement). And for roles, the
67 # overriden and modified methods are "in statis" and so would
68 # not show up in this test anyway (and as a side-effect they
69 # would not fufill the requirement, which is exactly what we
70 # want them to do anyway).
71 # - SL
72 }
73 }
74}
75
709c321c 76sub check_required_attributes {
77
78}
79
1c9db35c 80sub apply_attributes {
81 my ($self, $role, $class) = @_;
82 foreach my $attribute_name ($role->get_attribute_list) {
83 # it if it has one already
84 if ($class->has_attribute($attribute_name) &&
85 # make sure we haven't seen this one already too
86 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
87 next;
88 }
89 else {
90 # NOTE:
91 # this is kinda ugly ...
92 if ($class->isa('Moose::Meta::Class')) {
93 $class->_process_attribute(
94 $attribute_name,
95 %{$role->get_attribute($attribute_name)}
96 );
97 }
98 else {
99 $class->add_attribute(
100 $attribute_name,
101 $role->get_attribute($attribute_name)
102 );
103 }
104 }
105 }
106}
107
108sub apply_methods {
109 my ($self, $role, $class) = @_;
110 foreach my $method_name ($role->get_method_list) {
111 # it if it has one already
112 if ($class->has_method($method_name) &&
113 # and if they are not the same thing ...
114 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
115 next;
116 }
117 else {
118 # add it, although it could be overriden
119 $class->alias_method(
120 $method_name,
121 $role->get_method($method_name)
122 );
123 }
124 }
125 # we must reset the cache here since
126 # we are just aliasing methods, otherwise
127 # the modifiers go wonky.
128 $class->reset_package_cache_flag;
129}
130
131sub apply_override_method_modifiers {
132 my ($self, $role, $class) = @_;
133 foreach my $method_name ($role->get_method_modifier_list('override')) {
134 # it if it has one already then ...
135 if ($class->has_method($method_name)) {
136 next;
137 }
138 else {
139 # if this is not a role, then we need to
140 # find the original package of the method
141 # so that we can tell the class were to
142 # find the right super() method
143 my $method = $role->get_override_method_modifier($method_name);
144 my ($package) = Class::MOP::get_code_info($method);
145 # if it is a class, we just add it
146 $class->add_override_method_modifier($method_name, $method, $package);
147 }
148 }
149}
150
151sub apply_method_modifiers {
152 my ($self, $modifier_type, $role, $class) = @_;
153 my $add = "add_${modifier_type}_method_modifier";
154 my $get = "get_${modifier_type}_method_modifiers";
155 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
156 $class->$add(
157 $method_name,
158 $_
159 ) foreach $role->$get($method_name);
160 }
161}
162
fb1e11d5 1631;
164
165__END__
166
167=pod
168
169=head1 NAME
170
171Moose::Meta::Role::Application::ToClass
172
173=head1 DESCRIPTION
174
175=head2 METHODS
176
177=over 4
178
179=item B<new>
180
181=item B<meta>
182
1c9db35c 183=item B<apply>
184
709c321c 185=item B<check_role_exclusions>
186
1c9db35c 187=item B<check_required_methods>
188
709c321c 189=item B<check_required_attributes>
1c9db35c 190
191=item B<apply_attributes>
192
193=item B<apply_methods>
194
195=item B<apply_method_modifiers>
196
1c9db35c 197=item B<apply_override_method_modifiers>
198
fb1e11d5 199=back
200
201=head1 BUGS
202
203All complex software has bugs lurking in it, and this module is no
204exception. If you find a bug please either email me, or add the bug
205to cpan-RT.
206
207=head1 AUTHOR
208
209Stevan Little E<lt>stevan@iinteractive.comE<gt>
210
211=head1 COPYRIGHT AND LICENSE
212
778db3ac 213Copyright 2006-2008 by Infinity Interactive, Inc.
fb1e11d5 214
215L<http://www.iinteractive.com>
216
217This library is free software; you can redistribute it and/or modify
218it under the same terms as Perl itself.
219
220=cut
221