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) {
38 next if $self->is_aliased_method($required_method_name);
40 $role2->add_required_methods($required_method_name)
41 unless $role2->find_method_by_name($required_method_name);
45 sub check_required_attributes {
49 sub apply_attributes {
50 my ($self, $role1, $role2) = @_;
51 foreach my $attribute_name ($role1->get_attribute_list) {
52 # it if it has one already
53 if ($role2->has_attribute($attribute_name) &&
54 # make sure we haven't seen this one already too
55 $role2->get_attribute($attribute_name) != $role1->get_attribute($attribute_name)) {
56 confess "Role '" . $role1->name . "' has encountered an attribute conflict " .
57 "during composition. This is fatal error and cannot be disambiguated.";
60 $role2->add_attribute(
62 $role1->get_attribute($attribute_name)
69 my ($self, $role1, $role2) = @_;
70 foreach my $method_name ($role1->get_method_list) {
72 next if $self->is_method_excluded($method_name);
74 my $orig_method_name = $method_name;
76 if ($self->is_method_aliased($method_name)) {
77 $method_name = $self->get_method_aliases->{$method_name};
80 # it if it has one already
81 if ($role2->has_method($method_name) &&
82 # and if they are not the same thing ...
83 $role2->get_method($method_name)->body != $role1->get_method($method_name)->body) {
84 # method conflicts between roles result
85 # in the method becoming a requirement
86 $role2->add_required_methods($method_name);
89 # add it, although it could be overriden
92 $role1->get_method($orig_method_name)
98 sub apply_override_method_modifiers {
99 my ($self, $role1, $role2) = @_;
100 foreach my $method_name ($role1->get_method_modifier_list('override')) {
101 # it if it has one already then ...
102 if ($role2->has_method($method_name)) {
103 # if it is being composed into another role
104 # we have a conflict here, because you cannot
105 # combine an overriden method with a locally
107 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
108 "during composition (A local method of the same name as been found). This " .
112 # if we are a role, we need to make sure
113 # we dont have a conflict with the role
114 # we are composing into
115 if ($role2->has_override_method_modifier($method_name) &&
116 $role2->get_override_method_modifier($method_name) != $role2->get_override_method_modifier($method_name)) {
117 confess "Role '" . $role1->name . "' has encountered an 'override' method conflict " .
118 "during composition (Two 'override' methods of the same name encountered). " .
119 "This is fatal error.";
122 # if there is no conflict,
123 # just add it to the role
124 $role2->add_override_method_modifier(
126 $role1->get_override_method_modifier($method_name)
133 sub apply_method_modifiers {
134 my ($self, $modifier_type, $role1, $role2) = @_;
135 my $add = "add_${modifier_type}_method_modifier";
136 my $get = "get_${modifier_type}_method_modifiers";
137 foreach my $method_name ($role1->get_method_modifier_list($modifier_type)) {
141 ) foreach $role1->$get($method_name);
154 Moose::Meta::Role::Application::ToRole
168 =item B<check_role_exclusions>
170 =item B<check_required_methods>
172 =item B<check_required_attributes>
174 =item B<apply_attributes>
176 =item B<apply_methods>
178 =item B<apply_method_modifiers>
180 =item B<apply_override_method_modifiers>
186 All complex software has bugs lurking in it, and this module is no
187 exception. If you find a bug please either email me, or add the bug
192 Stevan Little E<lt>stevan@iinteractive.comE<gt>
194 =head1 COPYRIGHT AND LICENSE
196 Copyright 2006-2008 by Infinity Interactive, Inc.
198 L<http://www.iinteractive.com>
200 This library is free software; you can redistribute it and/or modify
201 it under the same terms as Perl itself.