1 package Moose::Meta::Role::Application::ToClass;
7 use Moose::Util 'english_list';
8 use Scalar::Util 'weaken', 'blessed';
10 our $VERSION = '0.93';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Moose::Meta::Role::Application';
16 __PACKAGE__->meta->add_attribute('role' => (
20 __PACKAGE__->meta->add_attribute('class' => (
25 my ($self, $role, $class) = @_;
27 # We need weak_ref in CMOP :(
28 weaken($self->{role} = $role);
29 weaken($self->{class} = $class);
31 $self->SUPER::apply($role, $class);
33 $class->add_role($role);
34 $class->add_role_application($self);
37 sub check_role_exclusions {
38 my ($self, $role, $class) = @_;
39 if (ref $class eq 'Class::MOP::Class' ){
42 if ($class->excludes_role($role->name)) {
43 $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
45 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
46 if ($class->does_role($excluded_role_name)) {
47 $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
52 sub check_required_methods {
53 my ($self, $role, $class) = @_;
59 # we might need to move this down below the
60 # the attributes so that we can require any
61 # attribute accessors. However I am thinking
62 # that maybe those are somehow exempt from
63 # the require methods stuff.
64 foreach my $required_method ($role->get_required_method_list) {
65 my $required_method_name = $required_method->name;
67 if (!$class->find_method_by_name($required_method_name)) {
69 next if $self->is_aliased_method($required_method_name);
71 push @missing, $required_method;
75 return unless @missing;
79 @missing = sort { $a->name cmp $b->name } @missing;
80 my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing;
83 my $conflict = $conflicts[0];
84 my $roles = $conflict->roles_as_english_list;
86 my @same_role_conflicts = grep { $_->roles_as_english_list eq $roles } @conflicts;
88 if (@same_role_conflicts == 1) {
90 .= "Due to a method name conflict in roles "
94 . "' must be implemented or excluded by '"
100 = Moose::Util::english_list( map { q{'} . $_->name . q{'} } @same_role_conflicts );
103 .= "Due to method name conflicts in roles "
107 . " must be implemented or excluded by '"
113 my $noun = @missing == 1 ? 'method' : 'methods';
116 = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing );
121 . "' requires the $noun $list "
122 . "to be implemented by '"
123 . $class->name . q{'};
126 $class->throw_error($error);
129 sub check_required_attributes {
133 sub apply_attributes {
134 my ($self, $role, $class) = @_;
135 foreach my $attribute_name ($role->get_attribute_list) {
136 # it if it has one already
137 if ($class->has_attribute($attribute_name) &&
138 # make sure we haven't seen this one already too
139 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
143 $class->add_attribute(
144 $role->get_attribute($attribute_name)->clone
151 my ($self, $role, $class) = @_;
152 foreach my $method_name ($role->get_method_list) {
153 next if $method_name eq 'meta';
155 unless ($self->is_method_excluded($method_name)) {
156 # it if it has one already
157 if ($class->has_method($method_name) &&
158 # and if they are not the same thing ...
159 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
163 # add it, although it could be overridden
166 $role->get_method($method_name)
171 if ($self->is_method_aliased($method_name)) {
172 my $aliased_method_name = $self->get_method_aliases->{$method_name};
173 # it if it has one already
174 if ($class->has_method($aliased_method_name) &&
175 # and if they are not the same thing ...
176 $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) {
177 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
180 $aliased_method_name,
181 $role->get_method($method_name)
185 # we must reset the cache here since
186 # we are just aliasing methods, otherwise
187 # the modifiers go wonky.
188 $class->reset_package_cache_flag;
191 sub apply_override_method_modifiers {
192 my ($self, $role, $class) = @_;
193 foreach my $method_name ($role->get_method_modifier_list('override')) {
194 # it if it has one already then ...
195 if ($class->has_method($method_name)) {
199 # if this is not a role, then we need to
200 # find the original package of the method
201 # so that we can tell the class were to
202 # find the right super() method
203 my $method = $role->get_override_method_modifier($method_name);
204 my ($package) = Class::MOP::get_code_info($method);
205 # if it is a class, we just add it
206 $class->add_override_method_modifier($method_name, $method, $package);
211 sub apply_method_modifiers {
212 my ($self, $modifier_type, $role, $class) = @_;
213 my $add = "add_${modifier_type}_method_modifier";
214 my $get = "get_${modifier_type}_method_modifiers";
215 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
219 ) foreach $role->$get($method_name);
231 Moose::Meta::Role::Application::ToClass - Compose a role into a class
245 =item B<check_role_exclusions>
247 =item B<check_required_methods>
249 =item B<check_required_attributes>
251 =item B<apply_attributes>
253 =item B<apply_methods>
255 =item B<apply_method_modifiers>
257 =item B<apply_override_method_modifiers>
263 All complex software has bugs lurking in it, and this module is no
264 exception. If you find a bug please either email me, or add the bug
269 Stevan Little E<lt>stevan@iinteractive.comE<gt>
271 =head1 COPYRIGHT AND LICENSE
273 Copyright 2006-2009 by Infinity Interactive, Inc.
275 L<http://www.iinteractive.com>
277 This library is free software; you can redistribute it and/or modify
278 it under the same terms as Perl itself.