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