This change gets Recipe 11 working. Here are the details ...
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToRole.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::ToRole;
2
3use strict;
4use warnings;
5use metaclass;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10use Data::Dumper;
11
eaa35e6e 12our $VERSION = '0.50';
fb1e11d5 13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Moose::Meta::Role::Application';
16
1c9db35c 17sub apply {
c4538447 18 my ($self, $role1, $role2) = @_;
19 $self->SUPER::apply($role1, $role2);
20 $role2->add_role($role1);
1c9db35c 21}
22
23sub 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
34sub check_required_methods {
35 my ($self, $role1, $role2) = @_;
36 foreach my $required_method_name ($role1->get_required_method_list) {
3e19778d 37
38 next if $self->is_aliased_method($required_method_name);
39
1c9db35c 40 $role2->add_required_methods($required_method_name)
41 unless $role2->find_method_by_name($required_method_name);
42 }
43}
44
709c321c 45sub check_required_attributes {
46
47}
48
1c9db35c 49sub 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
68sub apply_methods {
69 my ($self, $role1, $role2) = @_;
70 foreach my $method_name ($role1->get_method_list) {
c4538447 71
43a41ede 72 next if $self->is_method_excluded($method_name);
db9476b1 73
74 if ($self->is_method_aliased($method_name)) {
75 my $aliased_method_name = $self->get_method_aliases->{$method_name};
76 # it if it has one already
77 if ($role2->has_method($aliased_method_name) &&
78 # and if they are not the same thing ...
79 $role2->get_method($aliased_method_name)->body != $role1->get_method($method_name)->body) {
80 confess "Cannot create a method alias if a local method of the same name exists";
81 }
82
83 $role2->alias_method(
84 $aliased_method_name,
85 $role1->get_method($method_name)
86 );
87
88 if (!$role2->has_method($method_name)) {
89 $role2->add_required_methods($method_name);
90 }
91
92 next;
93 }
3e19778d 94
1c9db35c 95 # it if it has one already
96 if ($role2->has_method($method_name) &&
97 # and if they are not the same thing ...
98 $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) {
99 # method conflicts between roles result
100 # in the method becoming a requirement
101 $role2->add_required_methods($method_name);
102 }
103 else {
104 # add it, although it could be overriden
105 $role2->alias_method(
106 $method_name,
43a41ede 107 $role1->get_method($method_name)
1c9db35c 108 );
43a41ede 109
1c9db35c 110 }
43a41ede 111
1c9db35c 112 }
113}
114
115sub apply_override_method_modifiers {
116 my ($self, $role1, $role2) = @_;
117 foreach my $method_name ($role1->get_method_modifier_list('override')) {
118 # it if it has one already then ...
119 if ($role2->has_method($method_name)) {
120 # if it is being composed into another role
121 # we have a conflict here, because you cannot
122 # combine an overriden method with a locally
123 # defined one
124 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
125 "during composition (A local method of the same name as been found). This " .
126 "is fatal error.";
127 }
128 else {
129 # if we are a role, we need to make sure
130 # we dont have a conflict with the role
131 # we are composing into
132 if ($role2->has_override_method_modifier($method_name) &&
133 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
134 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
135 "during composition (Two 'override' methods of the same name encountered). " .
136 "This is fatal error.";
137 }
138 else {
139 # if there is no conflict,
140 # just add it to the role
141 $role2->add_override_method_modifier(
142 $method_name,
143 $role1->get_override_method_modifier($method_name)
144 );
145 }
146 }
147 }
148}
149
150sub apply_method_modifiers {
151 my ($self, $modifier_type, $role1, $role2) = @_;
152 my $add = "add_${modifier_type}_method_modifier";
153 my $get = "get_${modifier_type}_method_modifiers";
154 foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
155 $role2->$add(
156 $method_name,
157 $_
158 ) foreach $role1->$get($method_name);
159 }
160}
161
1c9db35c 162
fb1e11d5 1631;
164
165__END__
166
167=pod
168
169=head1 NAME
170
ab76842e 171Moose::Meta::Role::Application::ToRole - Compose a role into another role
fb1e11d5 172
173=head1 DESCRIPTION
174
175=head2 METHODS
176
177=over 4
178
179=item B<new>
180
181=item B<meta>
182
1c9db35c 183=item B<apply>
184
709c321c 185=item B<check_role_exclusions>
186
1c9db35c 187=item B<check_required_methods>
188
709c321c 189=item B<check_required_attributes>
1c9db35c 190
191=item B<apply_attributes>
192
193=item B<apply_methods>
194
195=item B<apply_method_modifiers>
196
1c9db35c 197=item B<apply_override_method_modifiers>
198
fb1e11d5 199=back
200
201=head1 BUGS
202
203All complex software has bugs lurking in it, and this module is no
204exception. If you find a bug please either email me, or add the bug
205to cpan-RT.
206
207=head1 AUTHOR
208
209Stevan Little E<lt>stevan@iinteractive.comE<gt>
210
211=head1 COPYRIGHT AND LICENSE
212
778db3ac 213Copyright 2006-2008 by Infinity Interactive, Inc.
fb1e11d5 214
215L<http://www.iinteractive.com>
216
217This library is free software; you can redistribute it and/or modify
218it under the same terms as Perl itself.
219
220=cut
221