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