bump version to 0.82
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToRole.pm
1 package Moose::Meta::Role::Application::ToRole;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Scalar::Util    'blessed';
8
9 our $VERSION   = '0.82';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 use base 'Moose::Meta::Role::Application';
14
15 sub apply {
16     my ($self, $role1, $role2) = @_;
17     $self->SUPER::apply($role1, $role2);
18     $role2->add_role($role1);
19 }
20
21 sub check_role_exclusions {
22     my ($self, $role1, $role2) = @_;
23     if ( $role2->excludes_role($role1->name) ) {
24         require Moose;
25         Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'");
26     }
27     foreach my $excluded_role_name ($role1->get_excluded_roles_list) {
28         if ( $role2->does_role($excluded_role_name) ) {
29             require Moose;
30             Moose->throw_error("The class " . $role2->name . " does the excluded role '$excluded_role_name'");
31         }
32         $role2->add_excluded_roles($excluded_role_name);
33     }
34 }
35
36 sub check_required_methods {
37     my ($self, $role1, $role2) = @_;
38     foreach my $required_method ($role1->get_required_method_list) {
39         my $required_method_name = $required_method->name;
40
41         next if $self->is_aliased_method($required_method_name);
42
43         $role2->add_required_methods($required_method)
44             unless $role2->find_method_by_name($required_method_name);
45     }
46 }
47
48 sub check_required_attributes {
49
50 }
51
52 sub apply_attributes {
53     my ($self, $role1, $role2) = @_;
54     foreach my $attribute_name ($role1->get_attribute_list) {
55         # it if it has one already
56         if ($role2->has_attribute($attribute_name) &&
57             # make sure we haven't seen this one already too
58             $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) {
59
60             require Moose;
61             Moose->throw_error("Role '" . $role1->name . "' has encountered an attribute conflict " .
62                     "during composition. This is fatal error and cannot be disambiguated.");
63         }
64         else {
65             $role2->add_attribute(
66                 $attribute_name,
67                 $role1->get_attribute($attribute_name)
68             );
69         }
70     }
71 }
72
73 sub apply_methods {
74     my ($self, $role1, $role2) = @_;
75     foreach my $method_name ($role1->get_method_list) {
76
77         if ($self->is_method_aliased($method_name)) {
78             my $aliased_method_name = $self->get_method_aliases->{$method_name};
79             # it if it has one already
80             if ($role2->has_method($aliased_method_name) &&
81                 # and if they are not the same thing ...
82                 $role2->get_method($aliased_method_name)->body != $role1->get_method($method_name)->body) {
83
84                 require Moose;
85                 Moose->throw_error("Cannot create a method alias if a local method of the same name exists");
86             }
87
88             $role2->add_method(
89                 $aliased_method_name,
90                 $role1->get_method($method_name)
91             );
92
93             if (!$role2->has_method($method_name)) {
94                 $role2->add_required_methods($method_name)
95                     unless $self->is_method_excluded($method_name);
96             }
97
98             next;
99         }
100
101         next if $self->is_method_excluded($method_name);
102
103         # it if it has one already
104         if ($role2->has_method($method_name) &&
105             # and if they are not the same thing ...
106             $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) {
107             # method conflicts between roles result
108             # in the method becoming a requirement
109             $role2->add_conflicting_method(
110                 name  => $method_name,
111                 roles => [$role1->name, $role2->name],
112             );
113         }
114         else {
115             # add it, although it could be overridden
116             $role2->add_method(
117                 $method_name,
118                 $role1->get_method($method_name)
119             );
120
121         }
122
123     }
124 }
125
126 sub apply_override_method_modifiers {
127     my ($self, $role1, $role2) = @_;
128     foreach my $method_name ($role1->get_method_modifier_list('override')) {
129         # it if it has one already then ...
130         if ($role2->has_method($method_name)) {
131             # if it is being composed into another role
132             # we have a conflict here, because you cannot
133             # combine an overridden method with a locally
134             # defined one
135             require Moose;
136             Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
137                     "during composition (A local method of the same name as been found). This " .
138                     "is fatal error.");
139         }
140         else {
141             # if we are a role, we need to make sure
142             # we dont have a conflict with the role
143             # we are composing into
144             if ($role2->has_override_method_modifier($method_name) &&
145                 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
146
147                 require Moose;
148                 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
149                         "during composition (Two 'override' methods of the same name encountered). " .
150                         "This is fatal error.");
151             }
152             else {
153                 # if there is no conflict,
154                 # just add it to the role
155                 $role2->add_override_method_modifier(
156                     $method_name,
157                     $role1->get_override_method_modifier($method_name)
158                 );
159             }
160         }
161     }
162 }
163
164 sub apply_method_modifiers {
165     my ($self, $modifier_type, $role1, $role2) = @_;
166     my $add = "add_${modifier_type}_method_modifier";
167     my $get = "get_${modifier_type}_method_modifiers";
168     foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
169         $role2->$add(
170             $method_name,
171             $_
172         ) foreach $role1->$get($method_name);
173     }
174 }
175
176
177 1;
178
179 __END__
180
181 =pod
182
183 =head1 NAME
184
185 Moose::Meta::Role::Application::ToRole - Compose a role into another role
186
187 =head1 DESCRIPTION
188
189 =head2 METHODS
190
191 =over 4
192
193 =item B<new>
194
195 =item B<meta>
196
197 =item B<apply>
198
199 =item B<check_role_exclusions>
200
201 =item B<check_required_methods>
202
203 =item B<check_required_attributes>
204
205 =item B<apply_attributes>
206
207 =item B<apply_methods>
208
209 =item B<apply_method_modifiers>
210
211 =item B<apply_override_method_modifiers>
212
213 =back
214
215 =head1 BUGS
216
217 All complex software has bugs lurking in it, and this module is no
218 exception. If you find a bug please either email me, or add the bug
219 to cpan-RT.
220
221 =head1 AUTHOR
222
223 Stevan Little E<lt>stevan@iinteractive.comE<gt>
224
225 =head1 COPYRIGHT AND LICENSE
226
227 Copyright 2006-2009 by Infinity Interactive, Inc.
228
229 L<http://www.iinteractive.com>
230
231 This library is free software; you can redistribute it and/or modify
232 it under the same terms as Perl itself.
233
234 =cut
235