1 package Moose::Meta::Role::Application::ToRole;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.55_04';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Moose::Meta::Role::Application';
17 my ($self, $role1, $role2) = @_;
18 $self->SUPER::apply($role1, $role2);
19 $role2->add_role($role1);
22 sub check_role_exclusions {
23 my ($self, $role1, $role2) = @_;
24 confess "Conflict detected: " . $role2->name . " excludes role '" . $role1->name . "'"
25 if $role2->excludes_role($role1->name);
26 foreach my $excluded_role_name ($role1->get_excluded_roles_list) {
27 confess "The class " . $role2->name . " does the excluded role '$excluded_role_name'"
28 if $role2->does_role($excluded_role_name);
29 $role2->add_excluded_roles($excluded_role_name);
33 sub check_required_methods {
34 my ($self, $role1, $role2) = @_;
35 foreach my $required_method_name ($role1->get_required_method_list) {
37 next if $self->is_aliased_method($required_method_name);
39 $role2->add_required_methods($required_method_name)
40 unless $role2->find_method_by_name($required_method_name);
44 sub check_required_attributes {
48 sub apply_attributes {
49 my ($self, $role1, $role2) = @_;
50 foreach my $attribute_name ($role1->get_attribute_list) {
51 # it if it has one already
52 if ($role2->has_attribute($attribute_name) &&
53 # make sure we haven't seen this one already too
54 $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) {
55 confess "Role '" . $role1->name . "' has encountered an attribute conflict " .
56 "during composition. This is fatal error and cannot be disambiguated.";
59 $role2->add_attribute(
61 $role1->get_attribute($attribute_name)
68 my ($self, $role1, $role2) = @_;
69 foreach my $method_name ($role1->get_method_list) {
71 next if $self->is_method_excluded($method_name);
73 if ($self->is_method_aliased($method_name)) {
74 my $aliased_method_name = $self->get_method_aliases->{$method_name};
75 # it if it has one already
76 if ($role2->has_method($aliased_method_name) &&
77 # and if they are not the same thing ...
78 $role2->get_method($aliased_method_name)->body != $role1->get_method($method_name)->body) {
79 confess "Cannot create a method alias if a local method of the same name exists";
84 $role1->get_method($method_name)
87 if (!$role2->has_method($method_name)) {
88 $role2->add_required_methods($method_name);
94 # it if it has one already
95 if ($role2->has_method($method_name) &&
96 # and if they are not the same thing ...
97 $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) {
98 # method conflicts between roles result
99 # in the method becoming a requirement
100 $role2->add_required_methods($method_name);
103 # add it, although it could be overriden
104 $role2->alias_method(
106 $role1->get_method($method_name)
114 sub apply_override_method_modifiers {
115 my ($self, $role1, $role2) = @_;
116 foreach my $method_name ($role1->get_method_modifier_list('override')) {
117 # it if it has one already then ...
118 if ($role2->has_method($method_name)) {
119 # if it is being composed into another role
120 # we have a conflict here, because you cannot
121 # combine an overriden method with a locally
123 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
124 "during composition (A local method of the same name as been found). This " .
128 # if we are a role, we need to make sure
129 # we dont have a conflict with the role
130 # we are composing into
131 if ($role2->has_override_method_modifier($method_name) &&
132 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
133 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
134 "during composition (Two 'override' methods of the same name encountered). " .
135 "This is fatal error.";
138 # if there is no conflict,
139 # just add it to the role
140 $role2->add_override_method_modifier(
142 $role1->get_override_method_modifier($method_name)
149 sub apply_method_modifiers {
150 my ($self, $modifier_type, $role1, $role2) = @_;
151 my $add = "add_${modifier_type}_method_modifier";
152 my $get = "get_${modifier_type}_method_modifiers";
153 foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
157 ) foreach $role1->$get($method_name);
170 Moose::Meta::Role::Application::ToRole - Compose a role into another role
184 =item B<check_role_exclusions>
186 =item B<check_required_methods>
188 =item B<check_required_attributes>
190 =item B<apply_attributes>
192 =item B<apply_methods>
194 =item B<apply_method_modifiers>
196 =item B<apply_override_method_modifiers>
202 All complex software has bugs lurking in it, and this module is no
203 exception. If you find a bug please either email me, or add the bug
208 Stevan Little E<lt>stevan@iinteractive.comE<gt>
210 =head1 COPYRIGHT AND LICENSE
212 Copyright 2006-2008 by Infinity Interactive, Inc.
214 L<http://www.iinteractive.com>
216 This library is free software; you can redistribute it and/or modify
217 it under the same terms as Perl itself.