Commit | Line | Data |
fb1e11d5 |
1 | package Moose::Meta::Role::Application::ToRole; |
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, $role1, $role2) = @_; |
19 | $self->SUPER::apply($role1, $role2); |
20 | $role2->add_role($role1); |
21 | } |
22 | |
23 | sub check_role_exclusions { |
24 | my ($self, $role1, $role2) = @_; |
25 | confess "Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'" |
26 | if $role2->excludes_role($role1->name); |
27 | foreach my $excluded_role_name ($role1->get_excluded_roles_list) { |
28 | confess "The class " . $role2->name . " does the excluded role '$excluded_role_name'" |
29 | if $role2->does_role($excluded_role_name); |
30 | $role2->add_excluded_roles($excluded_role_name); |
31 | } |
32 | } |
33 | |
34 | sub check_required_methods { |
35 | my ($self, $role1, $role2) = @_; |
36 | foreach my $required_method_name ($role1->get_required_method_list) { |
37 | $role2->add_required_methods($required_method_name) |
38 | unless $role2->find_method_by_name($required_method_name); |
39 | } |
40 | } |
41 | |
42 | sub apply_attributes { |
43 | my ($self, $role1, $role2) = @_; |
44 | foreach my $attribute_name ($role1->get_attribute_list) { |
45 | # it if it has one already |
46 | if ($role2->has_attribute($attribute_name) && |
47 | # make sure we haven't seen this one already too |
48 | $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) { |
49 | confess "Role '" . $role1->name . "' has encountered an attribute conflict " . |
50 | "during composition. This is fatal error and cannot be disambiguated."; |
51 | } |
52 | else { |
53 | $role2->add_attribute( |
54 | $attribute_name, |
55 | $role1->get_attribute($attribute_name) |
56 | ); |
57 | } |
58 | } |
59 | } |
60 | |
61 | sub apply_methods { |
62 | my ($self, $role1, $role2) = @_; |
63 | foreach my $method_name ($role1->get_method_list) { |
64 | # it if it has one already |
65 | if ($role2->has_method($method_name) && |
66 | # and if they are not the same thing ... |
67 | $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) { |
68 | # method conflicts between roles result |
69 | # in the method becoming a requirement |
70 | $role2->add_required_methods($method_name); |
71 | } |
72 | else { |
73 | # add it, although it could be overriden |
74 | $role2->alias_method( |
75 | $method_name, |
76 | $role1->get_method($method_name) |
77 | ); |
78 | } |
79 | } |
80 | } |
81 | |
82 | sub apply_override_method_modifiers { |
83 | my ($self, $role1, $role2) = @_; |
84 | foreach my $method_name ($role1->get_method_modifier_list('override')) { |
85 | # it if it has one already then ... |
86 | if ($role2->has_method($method_name)) { |
87 | # if it is being composed into another role |
88 | # we have a conflict here, because you cannot |
89 | # combine an overriden method with a locally |
90 | # defined one |
91 | confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " . |
92 | "during composition (A local method of the same name as been found). This " . |
93 | "is fatal error."; |
94 | } |
95 | else { |
96 | # if we are a role, we need to make sure |
97 | # we dont have a conflict with the role |
98 | # we are composing into |
99 | if ($role2->has_override_method_modifier($method_name) && |
100 | $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) { |
101 | confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " . |
102 | "during composition (Two 'override' methods of the same name encountered). " . |
103 | "This is fatal error."; |
104 | } |
105 | else { |
106 | # if there is no conflict, |
107 | # just add it to the role |
108 | $role2->add_override_method_modifier( |
109 | $method_name, |
110 | $role1->get_override_method_modifier($method_name) |
111 | ); |
112 | } |
113 | } |
114 | } |
115 | } |
116 | |
117 | sub apply_method_modifiers { |
118 | my ($self, $modifier_type, $role1, $role2) = @_; |
119 | my $add = "add_${modifier_type}_method_modifier"; |
120 | my $get = "get_${modifier_type}_method_modifiers"; |
121 | foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) { |
122 | $role2->$add( |
123 | $method_name, |
124 | $_ |
125 | ) foreach $role1->$get($method_name); |
126 | } |
127 | } |
128 | |
129 | sub apply_before_method_modifiers { (shift)->apply_method_modifiers('before' => @_) } |
130 | sub apply_around_method_modifiers { (shift)->apply_method_modifiers('around' => @_) } |
131 | sub apply_after_method_modifiers { (shift)->apply_method_modifiers('after' => @_) } |
132 | |
133 | |
fb1e11d5 |
134 | 1; |
135 | |
136 | __END__ |
137 | |
138 | =pod |
139 | |
140 | =head1 NAME |
141 | |
142 | Moose::Meta::Role::Application::ToRole |
143 | |
144 | =head1 DESCRIPTION |
145 | |
146 | =head2 METHODS |
147 | |
148 | =over 4 |
149 | |
150 | =item B<new> |
151 | |
152 | =item B<meta> |
153 | |
1c9db35c |
154 | =item B<apply> |
155 | |
156 | =item B<check_required_methods> |
157 | |
158 | =item B<check_role_exclusions> |
159 | |
160 | =item B<apply_attributes> |
161 | |
162 | =item B<apply_methods> |
163 | |
164 | =item B<apply_method_modifiers> |
165 | |
166 | =item B<apply_before_method_modifiers> |
167 | |
168 | =item B<apply_after_method_modifiers> |
169 | |
170 | =item B<apply_around_method_modifiers> |
171 | |
172 | =item B<apply_override_method_modifiers> |
173 | |
fb1e11d5 |
174 | =back |
175 | |
176 | =head1 BUGS |
177 | |
178 | All complex software has bugs lurking in it, and this module is no |
179 | exception. If you find a bug please either email me, or add the bug |
180 | to cpan-RT. |
181 | |
182 | =head1 AUTHOR |
183 | |
184 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
185 | |
186 | =head1 COPYRIGHT AND LICENSE |
187 | |
188 | Copyright 2006, 2007 by Infinity Interactive, Inc. |
189 | |
190 | L<http://www.iinteractive.com> |
191 | |
192 | This library is free software; you can redistribute it and/or modify |
193 | it under the same terms as Perl itself. |
194 | |
195 | =cut |
196 | |