1 package Moose::Meta::Role::Application::ToClass;
7 use List::MoreUtils 'firstval';
8 use Moose::Util 'english_list';
9 use Scalar::Util 'weaken', 'blessed';
11 use base 'Moose::Meta::Role::Application';
13 __PACKAGE__->meta->add_attribute('role' => (
15 Class::MOP::_definition_context(),
18 __PACKAGE__->meta->add_attribute('class' => (
20 Class::MOP::_definition_context(),
24 my ($self, $role, $class) = @_;
26 # We need weak_ref in CMOP :(
27 weaken($self->{role} = $role);
28 weaken($self->{class} = $class);
30 $self->SUPER::apply($role, $class);
32 $class->add_role($role);
33 $class->add_role_application($self);
36 sub check_role_exclusions {
37 my ($self, $role, $class) = @_;
38 if ($class->excludes_role($role->name)) {
39 $class->throw_error("Conflict detected: " . $class->name . " excludes role '" . $role->name . "'");
41 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
42 if ($class->does_role($excluded_role_name)) {
43 $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
48 sub check_required_methods {
49 my ($self, $role, $class) = @_;
55 # we might need to move this down below the
56 # the attributes so that we can require any
57 # attribute accessors. However I am thinking
58 # that maybe those are somehow exempt from
59 # the require methods stuff.
60 foreach my $required_method ($role->get_required_method_list) {
61 my $required_method_name = $required_method->name;
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;
71 return unless @missing;
75 @missing = sort { $a->name cmp $b->name } @missing;
76 my @conflicts = grep { $_->isa('Moose::Meta::Role::Method::Conflicting') } @missing;
79 my $conflict = $conflicts[0];
80 my $roles = $conflict->roles_as_english_list;
82 my @same_role_conflicts = grep { $_->roles_as_english_list eq $roles } @conflicts;
84 if (@same_role_conflicts == 1) {
86 .= "Due to a method name conflict in roles "
90 . "' must be implemented or excluded by '"
96 = Moose::Util::english_list( map { q{'} . $_->name . q{'} } @same_role_conflicts );
99 .= "Due to method name conflicts in roles "
103 . " must be implemented or excluded by '"
109 my $noun = @missing == 1 ? 'method' : 'methods';
112 = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing );
117 . "' requires the $noun $list "
118 . "to be implemented by '"
119 . $class->name . q{'};
121 if (my $meth = firstval { $class->name->can($_) } @missing) {
122 $error .= ". If you imported functions intending to use them as "
123 . "methods, you need to explicitly mark them as such, via "
124 . $class->name . "->meta->add_method($meth => \\\&$meth)";
128 $class->throw_error($error);
131 sub check_required_attributes {
135 sub apply_attributes {
136 my ($self, $role, $class) = @_;
138 foreach my $attribute_name ($role->get_attribute_list) {
139 # it if it has one already
140 if ($class->has_attribute($attribute_name) &&
141 # make sure we haven't seen this one already too
142 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
146 $class->add_attribute(
147 $role->get_attribute($attribute_name)->attribute_for_class
154 my ( $self, $role, $class ) = @_;
156 foreach my $method ( $role->_get_local_methods ) {
157 my $method_name = $method->name;
159 next if $method->isa('Class::MOP::Method::Meta');
161 unless ( $self->is_method_excluded($method_name) ) {
163 my $class_method = $class->get_method($method_name);
165 next if $class_method && $class_method->body != $method->body;
173 next unless $self->is_method_aliased($method_name);
175 my $aliased_method_name = $self->get_method_aliases->{$method_name};
177 my $class_method = $class->get_method($aliased_method_name);
179 if ( $class_method && $class_method->body != $method->body ) {
181 "Cannot create a method alias if a local method of the same name exists"
186 $aliased_method_name,
191 # we must reset the cache here since
192 # we are just aliasing methods, otherwise
193 # the modifiers go wonky.
194 $class->reset_package_cache_flag;
197 sub apply_override_method_modifiers {
198 my ($self, $role, $class) = @_;
199 foreach my $method_name ($role->get_method_modifier_list('override')) {
200 # it if it has one already then ...
201 if ($class->has_method($method_name)) {
205 # if this is not a role, then we need to
206 # find the original package of the method
207 # so that we can tell the class were to
208 # find the right super() method
209 my $method = $role->get_override_method_modifier($method_name);
210 my ($package) = Class::MOP::get_code_info($method);
211 # if it is a class, we just add it
212 $class->add_override_method_modifier($method_name, $method, $package);
217 sub apply_method_modifiers {
218 my ($self, $modifier_type, $role, $class) = @_;
219 my $add = "add_${modifier_type}_method_modifier";
220 my $get = "get_${modifier_type}_method_modifiers";
221 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
225 ) foreach $role->$get($method_name);
231 # ABSTRACT: Compose a role into a class
249 =item B<check_role_exclusions>
251 =item B<check_required_methods>
253 =item B<check_required_attributes>
255 =item B<apply_attributes>
257 =item B<apply_methods>
259 =item B<apply_method_modifiers>
261 =item B<apply_override_method_modifiers>
267 See L<Moose/BUGS> for details on reporting bugs.