moving stuff around and some cleanup
[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         $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
130 1;
131
132 __END__
133
134 =pod
135
136 =head1 NAME
137
138 Moose::Meta::Role::Application::ToRole
139
140 =head1 DESCRIPTION
141
142 =head2 METHODS
143
144 =over 4
145
146 =item B<new>
147
148 =item B<meta>
149
150 =item B<apply>
151
152 =item B<check_required_methods>
153
154 =item B<check_role_exclusions>
155
156 =item B<apply_attributes>
157
158 =item B<apply_methods>
159
160 =item B<apply_method_modifiers>
161
162 =item B<apply_override_method_modifiers>
163
164 =back
165
166 =head1 BUGS
167
168 All complex software has bugs lurking in it, and this module is no
169 exception. If you find a bug please either email me, or add the bug
170 to cpan-RT.
171
172 =head1 AUTHOR
173
174 Stevan Little E<lt>stevan@iinteractive.comE<gt>
175
176 =head1 COPYRIGHT AND LICENSE
177
178 Copyright 2006, 2007 by Infinity Interactive, Inc.
179
180 L<http://www.iinteractive.com>
181
182 This library is free software; you can redistribute it and/or modify
183 it under the same terms as Perl itself.
184
185 =cut
186