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