1 package Moose::Meta::Role::Application::ToRole;
7 use Scalar::Util 'blessed';
10 $VERSION = eval $VERSION;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Role::Application';
16 my ($self, $role1, $role2) = @_;
17 $self->SUPER::apply($role1, $role2);
18 $role2->add_role($role1);
21 sub check_role_exclusions {
22 my ($self, $role1, $role2) = @_;
23 if ( $role2->excludes_role($role1->name) ) {
25 Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'");
27 foreach my $excluded_role_name ($role1->get_excluded_roles_list) {
28 if ( $role2->does_role($excluded_role_name) ) {
30 Moose->throw_error("The class " . $role2->name . " does the excluded role '$excluded_role_name'");
32 $role2->add_excluded_roles($excluded_role_name);
36 sub check_required_methods {
37 my ($self, $role1, $role2) = @_;
38 foreach my $required_method ($role1->get_required_method_list) {
39 my $required_method_name = $required_method->name;
41 next if $self->is_aliased_method($required_method_name);
43 $role2->add_required_methods($required_method)
44 unless $role2->find_method_by_name($required_method_name);
48 sub check_required_attributes {
52 sub 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)) {
61 Moose->throw_error("Role '" . $role1->name . "' has encountered an attribute conflict " .
62 "during composition. This is fatal error and cannot be disambiguated.");
65 $role2->add_attribute(
66 $role1->get_attribute($attribute_name)->clone
73 my ($self, $role1, $role2) = @_;
74 foreach my $method_name ($role1->get_method_list) {
75 next if $method_name eq 'meta';
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 ) {
82 # method conflicts between roles result in the method becoming
84 $role2->add_conflicting_method(
86 roles => [ $role1->name, $role2->name ],
92 $role1->get_method($method_name)
97 if ($self->is_method_aliased($method_name)) {
98 my $aliased_method_name = $self->get_method_aliases->{$method_name};
100 if ($role2->has_method($aliased_method_name) &&
101 $role2->get_method($aliased_method_name)->body != $role1->get_method($method_name)->body) {
104 Moose->throw_error("Cannot create a method alias if a local method of the same name exists");
108 $aliased_method_name,
109 $role1->get_method($method_name)
112 if (!$role2->has_method($method_name)) {
113 $role2->add_required_methods($method_name)
114 unless $self->is_method_excluded($method_name);
120 sub 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
127 # combine an overridden method with a locally
130 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
131 "during composition (A local method of the same name as been found). This " .
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)) {
142 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
143 "during composition (Two 'override' methods of the same name encountered). " .
144 "This is fatal error.");
147 # if there is no conflict,
148 # just add it to the role
149 $role2->add_override_method_modifier(
151 $role1->get_override_method_modifier($method_name)
158 sub 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)) {
166 ) foreach $role1->$get($method_name);
179 Moose::Meta::Role::Application::ToRole - Compose a role into another role
193 =item B<check_role_exclusions>
195 =item B<check_required_methods>
197 =item B<check_required_attributes>
199 =item B<apply_attributes>
201 =item B<apply_methods>
203 =item B<apply_method_modifiers>
205 =item B<apply_override_method_modifiers>
211 See L<Moose/BUGS> for details on reporting bugs.
215 Stevan Little E<lt>stevan@iinteractive.comE<gt>
217 =head1 COPYRIGHT AND LICENSE
219 Copyright 2006-2010 by Infinity Interactive, Inc.
221 L<http://www.iinteractive.com>
223 This library is free software; you can redistribute it and/or modify
224 it under the same terms as Perl itself.