updating copyright dates
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / ToRole.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::ToRole;
2
3use strict;
4use warnings;
5use metaclass;
6
7use Carp 'confess';
8use Scalar::Util 'blessed';
9
10use Data::Dumper;
11
12our $VERSION = '0.01';
13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Moose::Meta::Role::Application';
16
1c9db35c 17sub apply {
18 my ($self, $role1, $role2) = @_;
19 $self->SUPER::apply($role1, $role2);
20 $role2->add_role($role1);
21}
22
23sub 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);
31 }
32}
33
34sub 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);
39 }
40}
41
42sub 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.";
51 }
52 else {
53 $role2->add_attribute(
54 $attribute_name,
55 $role1->get_attribute($attribute_name)
56 );
57 }
58 }
59}
60
61sub apply_methods {
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);
71 }
72 else {
73 # add it, although it could be overriden
74 $role2->alias_method(
75 $method_name,
76 $role1->get_method($method_name)
77 );
78 }
79 }
80}
81
82sub 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
90 # defined one
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 " .
93 "is fatal error.";
94 }
95 else {
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.";
104 }
105 else {
106 # if there is no conflict,
107 # just add it to the role
108 $role2->add_override_method_modifier(
109 $method_name,
110 $role1->get_override_method_modifier($method_name)
111 );
112 }
113 }
114 }
115}
116
117sub 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)) {
122 $role2->$add(
123 $method_name,
124 $_
125 ) foreach $role1->$get($method_name);
126 }
127}
128
1c9db35c 129
fb1e11d5 1301;
131
132__END__
133
134=pod
135
136=head1 NAME
137
138Moose::Meta::Role::Application::ToRole
139
140=head1 DESCRIPTION
141
142=head2 METHODS
143
144=over 4
145
146=item B<new>
147
148=item B<meta>
149
1c9db35c 150=item B<apply>
151
152=item B<check_required_methods>
153
154=item B<check_role_exclusions>
155
156=item B<apply_attributes>
157
158=item B<apply_methods>
159
160=item B<apply_method_modifiers>
161
1c9db35c 162=item B<apply_override_method_modifiers>
163
fb1e11d5 164=back
165
166=head1 BUGS
167
168All complex software has bugs lurking in it, and this module is no
169exception. If you find a bug please either email me, or add the bug
170to cpan-RT.
171
172=head1 AUTHOR
173
174Stevan Little E<lt>stevan@iinteractive.comE<gt>
175
176=head1 COPYRIGHT AND LICENSE
177
778db3ac 178Copyright 2006-2008 by Infinity Interactive, Inc.
fb1e11d5 179
180L<http://www.iinteractive.com>
181
182This library is free software; you can redistribute it and/or modify
183it under the same terms as Perl itself.
184
185=cut
186