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 | |
10 | use Data::Dumper; |
11 | |
12 | our $VERSION = '0.01'; |
13 | our $AUTHORITY = 'cpan:STEVAN'; |
14 | |
15 | use base 'Moose::Meta::Role::Application'; |
16 | |
1c9db35c |
17 | sub apply { |
18 | my ($self, $role, $class) = @_; |
19 | $self->SUPER::apply($role, $class); |
20 | $class->add_role($role); |
21 | } |
22 | |
23 | sub 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 | |
35 | sub 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 |
76 | sub check_required_attributes { |
77 | |
78 | } |
79 | |
1c9db35c |
80 | sub 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 | |
108 | sub 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 | |
131 | sub 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 | |
151 | sub 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 |
163 | 1; |
164 | |
165 | __END__ |
166 | |
167 | =pod |
168 | |
169 | =head1 NAME |
170 | |
171 | Moose::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 | |
203 | All complex software has bugs lurking in it, and this module is no |
204 | exception. If you find a bug please either email me, or add the bug |
205 | to cpan-RT. |
206 | |
207 | =head1 AUTHOR |
208 | |
209 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
210 | |
211 | =head1 COPYRIGHT AND LICENSE |
212 | |
778db3ac |
213 | Copyright 2006-2008 by Infinity Interactive, Inc. |
fb1e11d5 |
214 | |
215 | L<http://www.iinteractive.com> |
216 | |
217 | This library is free software; you can redistribute it and/or modify |
218 | it under the same terms as Perl itself. |
219 | |
220 | =cut |
221 | |