1 package Moose::Meta::Role::Application::ToRole;
8 use Scalar::Util 'blessed';
12 our $VERSION = '0.01';
13 our $AUTHORITY = 'cpan:STEVAN';
15 use base 'Moose::Meta::Role::Application';
18 my ($self, $role1, $role2) = @_;
19 $self->SUPER::apply($role1, $role2);
20 $role2->add_role($role1);
23 sub 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);
34 sub check_required_methods {
35 my ($self, $role1, $role2) = @_;
36 foreach my $required_method_name ($role1->get_required_method_list) {
37 $role2->add_required_methods($required_method_name)
38 unless $role2->find_method_by_name($required_method_name);
42 sub apply_attributes {
43 my ($self, $role1, $role2) = @_;
44 foreach my $attribute_name ($role1->get_attribute_list) {
45 # it if it has one already
46 if ($role2->has_attribute($attribute_name) &&
47 # make sure we haven't seen this one already too
48 $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) {
49 confess "Role '" . $role1->name . "' has encountered an attribute conflict " .
50 "during composition. This is fatal error and cannot be disambiguated.";
53 $role2->add_attribute(
55 $role1->get_attribute($attribute_name)
62 my ($self, $role1, $role2) = @_;
63 foreach my $method_name ($role1->get_method_list) {
64 # it if it has one already
65 if ($role2->has_method($method_name) &&
66 # and if they are not the same thing ...
67 $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) {
68 # method conflicts between roles result
69 # in the method becoming a requirement
70 $role2->add_required_methods($method_name);
73 # add it, although it could be overriden
76 $role1->get_method($method_name)
82 sub apply_override_method_modifiers {
83 my ($self, $role1, $role2) = @_;
84 foreach my $method_name ($role1->get_method_modifier_list('override')) {
85 # it if it has one already then ...
86 if ($role2->has_method($method_name)) {
87 # if it is being composed into another role
88 # we have a conflict here, because you cannot
89 # combine an overriden method with a locally
91 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
92 "during composition (A local method of the same name as been found). This " .
96 # if we are a role, we need to make sure
97 # we dont have a conflict with the role
98 # we are composing into
99 if ($role2->has_override_method_modifier($method_name) &&
100 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
101 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
102 "during composition (Two 'override' methods of the same name encountered). " .
103 "This is fatal error.";
106 # if there is no conflict,
107 # just add it to the role
108 $role2->add_override_method_modifier(
110 $role1->get_override_method_modifier($method_name)
117 sub apply_method_modifiers {
118 my ($self, $modifier_type, $role1, $role2) = @_;
119 my $add = "add_${modifier_type}_method_modifier";
120 my $get = "get_${modifier_type}_method_modifiers";
121 foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
125 ) foreach $role1->$get($method_name);
138 Moose::Meta::Role::Application::ToRole
152 =item B<check_required_methods>
154 =item B<check_role_exclusions>
156 =item B<apply_attributes>
158 =item B<apply_methods>
160 =item B<apply_method_modifiers>
162 =item B<apply_override_method_modifiers>
168 All complex software has bugs lurking in it, and this module is no
169 exception. If you find a bug please either email me, or add the bug
174 Stevan Little E<lt>stevan@iinteractive.comE<gt>
176 =head1 COPYRIGHT AND LICENSE
178 Copyright 2006, 2007 by Infinity Interactive, Inc.
180 L<http://www.iinteractive.com>
182 This library is free software; you can redistribute it and/or modify
183 it under the same terms as Perl itself.