1 package Moose::Meta::Role::Application::ToClass;
7 use Moose::Util 'english_list';
8 use Scalar::Util 'weaken', 'blessed';
10 our $AUTHORITY = 'cpan:STEVAN';
12 use base 'Moose::Meta::Role::Application';
14 __PACKAGE__->meta->add_attribute('role' => (
18 __PACKAGE__->meta->add_attribute('class' => (
23 my ($self, $role, $class) = @_;
25 # We need weak_ref in CMOP :(
26 weaken($self->{role} = $role);
27 weaken($self->{class} = $class);
29 $self->SUPER::apply($role, $class);
31 $class->add_role($role);
32 $class->add_role_application($self);
35 sub check_role_exclusions {
36 my ($self, $role, $class) = @_;
37 if ($class->excludes_role($role->name)) {
38 $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
40 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
41 if ($class->does_role($excluded_role_name)) {
42 $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
47 sub check_required_methods {
48 my ($self, $role, $class) = @_;
54 # we might need to move this down below the
55 # the attributes so that we can require any
56 # attribute accessors. However I am thinking
57 # that maybe those are somehow exempt from
58 # the require methods stuff.
59 foreach my $required_method ($role->get_required_method_list) {
60 my $required_method_name = $required_method->name;
62 if (!$class->find_method_by_name($required_method_name)) {
64 next if $self->is_aliased_method($required_method_name);
66 push @missing, $required_method;
70 return unless @missing;
74 @missing = sort { $a->name cmp $b->name } @missing;
75 my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing;
78 my $conflict = $conflicts[0];
79 my $roles = $conflict->roles_as_english_list;
81 my @same_role_conflicts = grep { $_->roles_as_english_list eq $roles } @conflicts;
83 if (@same_role_conflicts == 1) {
85 .= "Due to a method name conflict in roles "
89 . "' must be implemented or excluded by '"
95 = Moose::Util::english_list( map { q{'} . $_->name . q{'} } @same_role_conflicts );
98 .= "Due to method name conflicts in roles "
102 . " must be implemented or excluded by '"
108 my $noun = @missing == 1 ? 'method' : 'methods';
111 = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing );
116 . "' requires the $noun $list "
117 . "to be implemented by '"
118 . $class->name . q{'};
121 $class->throw_error($error);
124 sub check_required_attributes {
128 sub apply_attributes {
129 my ($self, $role, $class) = @_;
130 my $attr_metaclass = $role->applied_attribute_metaclass;
132 foreach my $attribute_name ($role->get_attribute_list) {
133 # it if it has one already
134 if ($class->has_attribute($attribute_name) &&
135 # make sure we haven't seen this one already too
136 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
140 $class->add_attribute(
141 $role->get_attribute($attribute_name)->attribute_for_class($attr_metaclass)
148 my ( $self, $role, $class ) = @_;
150 foreach my $method ( $role->_get_local_methods ) {
151 my $method_name = $method->name;
153 next if $method->isa('Class::MOP::Method::Meta');
155 unless ( $self->is_method_excluded($method_name) ) {
157 my $class_method = $class->get_method($method_name);
159 next if $class_method && $class_method->body != $method->body;
167 next unless $self->is_method_aliased($method_name);
169 my $aliased_method_name = $self->get_method_aliases->{$method_name};
171 my $class_method = $class->get_method($aliased_method_name);
173 if ( $class_method && $class_method->body != $method->body ) {
175 "Cannot create a method alias if a local method of the same name exists"
180 $aliased_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);
225 # ABSTRACT: Compose a role into a class
243 =item B<check_role_exclusions>
245 =item B<check_required_methods>
247 =item B<check_required_attributes>
249 =item B<apply_attributes>
251 =item B<apply_methods>
253 =item B<apply_method_modifiers>
255 =item B<apply_override_method_modifiers>
261 See L<Moose/BUGS> for details on reporting bugs.