1 package Moose::Meta::Role::Application::ToClass;
7 use Moose::Util 'english_list';
8 use Scalar::Util 'weaken', 'blessed';
10 our $VERSION = '0.79';
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 ($class->excludes_role($role->name)) {
40 $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
42 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
43 if ($class->does_role($excluded_role_name)) {
44 $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
49 sub check_required_methods {
50 my ($self, $role, $class) = @_;
56 # we might need to move this down below the
57 # the attributes so that we can require any
58 # attribute accessors. However I am thinking
59 # that maybe those are somehow exempt from
60 # the require methods stuff.
61 foreach my $required_method_name ($role->get_required_method_list) {
63 if (!$class->find_method_by_name($required_method_name)) {
65 next if $self->is_aliased_method($required_method_name);
67 push @missing, $required_method_name;
71 return unless @missing;
76 my $noun = @missing == 1 ? 'method' : 'methods';
79 = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing );
84 . "' requires the $noun $list "
85 . "to be implemented by '"
86 . $class->name . q{'};
89 $class->throw_error($error);
92 sub check_required_attributes {
96 sub apply_attributes {
97 my ($self, $role, $class) = @_;
98 foreach my $attribute_name ($role->get_attribute_list) {
99 # it if it has one already
100 if ($class->has_attribute($attribute_name) &&
101 # make sure we haven't seen this one already too
102 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
106 $class->add_attribute(
108 $role->get_attribute($attribute_name)
115 my ($self, $role, $class) = @_;
116 foreach my $method_name ($role->get_method_list) {
118 unless ($self->is_method_excluded($method_name)) {
119 # it if it has one already
120 if ($class->has_method($method_name) &&
121 # and if they are not the same thing ...
122 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
126 # add it, although it could be overridden
129 $role->get_method($method_name)
134 if ($self->is_method_aliased($method_name)) {
135 my $aliased_method_name = $self->get_method_aliases->{$method_name};
136 # it if it has one already
137 if ($class->has_method($aliased_method_name) &&
138 # and if they are not the same thing ...
139 $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) {
140 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
143 $aliased_method_name,
144 $role->get_method($method_name)
148 # we must reset the cache here since
149 # we are just aliasing methods, otherwise
150 # the modifiers go wonky.
151 $class->reset_package_cache_flag;
154 sub apply_override_method_modifiers {
155 my ($self, $role, $class) = @_;
156 foreach my $method_name ($role->get_method_modifier_list('override')) {
157 # it if it has one already then ...
158 if ($class->has_method($method_name)) {
162 # if this is not a role, then we need to
163 # find the original package of the method
164 # so that we can tell the class were to
165 # find the right super() method
166 my $method = $role->get_override_method_modifier($method_name);
167 my ($package) = Class::MOP::get_code_info($method);
168 # if it is a class, we just add it
169 $class->add_override_method_modifier($method_name, $method, $package);
174 sub apply_method_modifiers {
175 my ($self, $modifier_type, $role, $class) = @_;
176 my $add = "add_${modifier_type}_method_modifier";
177 my $get = "get_${modifier_type}_method_modifiers";
178 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
182 ) foreach $role->$get($method_name);
194 Moose::Meta::Role::Application::ToClass - Compose a role into a class
208 =item B<check_role_exclusions>
210 =item B<check_required_methods>
212 =item B<check_required_attributes>
214 =item B<apply_attributes>
216 =item B<apply_methods>
218 =item B<apply_method_modifiers>
220 =item B<apply_override_method_modifiers>
226 All complex software has bugs lurking in it, and this module is no
227 exception. If you find a bug please either email me, or add the bug
232 Stevan Little E<lt>stevan@iinteractive.comE<gt>
234 =head1 COPYRIGHT AND LICENSE
236 Copyright 2006-2009 by Infinity Interactive, Inc.
238 L<http://www.iinteractive.com>
240 This library is free software; you can redistribute it and/or modify
241 it under the same terms as Perl itself.