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