1 package Moose::Meta::Role::Application::ToRole;
7 use Scalar::Util 'blessed';
9 our $AUTHORITY = 'cpan:STEVAN';
11 use base 'Moose::Meta::Role::Application';
14 my ($self, $role1, $role2) = @_;
15 $self->SUPER::apply($role1, $role2);
16 $role2->add_role($role1);
19 sub check_role_exclusions {
20 my ($self, $role1, $role2) = @_;
21 if ( $role2->excludes_role($role1->name) ) {
23 Moose->throw_error("Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'");
25 foreach my $excluded_role_name ($role1->get_excluded_roles_list) {
26 if ( $role2->does_role($excluded_role_name) ) {
28 Moose->throw_error("The class " . $role2->name . " does the excluded role '$excluded_role_name'");
30 $role2->add_excluded_roles($excluded_role_name);
34 sub check_required_methods {
35 my ($self, $role1, $role2) = @_;
36 foreach my $required_method ($role1->get_required_method_list) {
37 my $required_method_name = $required_method->name;
39 next if $self->is_aliased_method($required_method_name);
41 $role2->add_required_methods($required_method)
42 unless $role2->find_method_by_name($required_method_name);
46 sub check_required_attributes {
50 sub apply_attributes {
51 my ($self, $role1, $role2) = @_;
52 foreach my $attribute_name ($role1->get_attribute_list) {
53 # it if it has one already
54 if ($role2->has_attribute($attribute_name) &&
55 # make sure we haven't seen this one already too
56 $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) {
58 my $role2_name = $role2->name;
61 Moose->throw_error( "Role '"
63 . "' has encountered an attribute conflict"
64 . " while being composed into '$role2_name'."
65 . " This is a fatal error and cannot be disambiguated."
66 . " The conflicting attribute is named '$attribute_name'." );
69 $role2->add_attribute(
70 $role1->get_attribute($attribute_name)->clone
77 my ( $self, $role1, $role2 ) = @_;
78 foreach my $method ( $role1->_get_local_methods ) {
80 my $method_name = $method->name;
82 next if $method->isa('Class::MOP::Method::Meta');
84 unless ( $self->is_method_excluded($method_name) ) {
86 my $role2_method = $role2->get_method($method_name);
88 && $role2_method->body != $method->body ) {
90 # method conflicts between roles result in the method becoming
92 $role2->add_conflicting_method(
94 roles => [ $role1->name, $role2->name ],
105 next unless $self->is_method_aliased($method_name);
107 my $aliased_method_name = $self->get_method_aliases->{$method_name};
109 my $role2_method = $role2->get_method($aliased_method_name);
112 && $role2_method->body != $method->body ) {
116 "Cannot create a method alias if a local method of the same name exists"
121 $aliased_method_name,
122 $role1->get_method($method_name)
125 if ( !$role2->has_method($method_name) ) {
126 $role2->add_required_methods($method_name)
127 unless $self->is_method_excluded($method_name);
132 sub apply_override_method_modifiers {
133 my ($self, $role1, $role2) = @_;
134 foreach my $method_name ($role1->get_method_modifier_list('override')) {
135 # it if it has one already then ...
136 if ($role2->has_method($method_name)) {
137 # if it is being composed into another role
138 # we have a conflict here, because you cannot
139 # combine an overridden method with a locally
142 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
143 "during composition (A local method of the same name as been found). This " .
147 # if we are a role, we need to make sure
148 # we dont have a conflict with the role
149 # we are composing into
150 if ($role2->has_override_method_modifier($method_name) &&
151 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
154 Moose->throw_error("Role '" . $role1->name . "' has encountered an 'override' method conflict " .
155 "during composition (Two 'override' methods of the same name encountered). " .
156 "This is fatal error.");
159 # if there is no conflict,
160 # just add it to the role
161 $role2->add_override_method_modifier(
163 $role1->get_override_method_modifier($method_name)
170 sub apply_method_modifiers {
171 my ($self, $modifier_type, $role1, $role2) = @_;
172 my $add = "add_${modifier_type}_method_modifier";
173 my $get = "get_${modifier_type}_method_modifiers";
174 foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
178 ) foreach $role1->$get($method_name);
185 # ABSTRACT: Compose a role into another role
203 =item B<check_role_exclusions>
205 =item B<check_required_methods>
207 =item B<check_required_attributes>
209 =item B<apply_attributes>
211 =item B<apply_methods>
213 =item B<apply_method_modifiers>
215 =item B<apply_override_method_modifiers>
221 See L<Moose/BUGS> for details on reporting bugs.