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