1 package Moose::Meta::Role::Application::ToClass;
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.57';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Moose::Meta::Role::Application';
17 my ($self, $role, $class) = @_;
18 $self->SUPER::apply($role, $class);
19 $class->add_role($role);
22 sub check_role_exclusions {
23 my ($self, $role, $class) = @_;
24 if ($class->excludes_role($role->name)) {
25 confess "Conflict detected: " . $class->name . " excludes role '" . $role->name . "'";
27 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
28 if ($class->does_role($excluded_role_name)) {
29 confess "The class " . $class->name . " does the excluded role '$excluded_role_name'";
34 sub check_required_methods {
35 my ($self, $role, $class) = @_;
37 # we might need to move this down below the
38 # the attributes so that we can require any
39 # attribute accessors. However I am thinking
40 # that maybe those are somehow exempt from
41 # the require methods stuff.
42 foreach my $required_method_name ($role->get_required_method_list) {
44 if (!$class->find_method_by_name($required_method_name)) {
46 next if $self->is_aliased_method($required_method_name);
48 confess "'" . $role->name . "' requires the method '$required_method_name' " .
49 "to be implemented by '" . $class->name . "'";
53 # we need to make sure that the method is
54 # not a method modifier, because those do
55 # not satisfy the requirements ...
56 my $method = $class->find_method_by_name($required_method_name);
58 # check if it is a generated accessor ...
59 (!$method->isa('Class::MOP::Method::Accessor'))
60 || confess "'" . $role->name . "' requires the method '$required_method_name' " .
61 "to be implemented by '" . $class->name . "', the method is only an attribute accessor";
64 # All other tests here have been removed, they were tests
65 # for overriden methods and before/after/around modifiers.
66 # But we realized that for classes any overriden or modified
67 # methods would be backed by a real method of that name
68 # (and therefore meet the requirement). And for roles, the
69 # overriden and modified methods are "in statis" and so would
70 # not show up in this test anyway (and as a side-effect they
71 # would not fufill the requirement, which is exactly what we
72 # want them to do anyway).
78 sub check_required_attributes {
82 sub apply_attributes {
83 my ($self, $role, $class) = @_;
84 foreach my $attribute_name ($role->get_attribute_list) {
85 # it if it has one already
86 if ($class->has_attribute($attribute_name) &&
87 # make sure we haven't seen this one already too
88 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
92 $class->add_attribute(
94 $role->get_attribute($attribute_name)
101 my ($self, $role, $class) = @_;
102 foreach my $method_name ($role->get_method_list) {
104 next if $self->is_method_excluded($method_name);
106 # it if it has one already
107 if ($class->has_method($method_name) &&
108 # and if they are not the same thing ...
109 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
113 # add it, although it could be overriden
114 $class->alias_method(
116 $role->get_method($method_name)
120 if ($self->is_method_aliased($method_name)) {
121 my $aliased_method_name = $self->get_method_aliases->{$method_name};
122 # it if it has one already
123 if ($class->has_method($aliased_method_name) &&
124 # and if they are not the same thing ...
125 $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) {
126 confess "Cannot create a method alias if a local method of the same name exists";
128 $class->alias_method(
129 $aliased_method_name,
130 $role->get_method($method_name)
134 # we must reset the cache here since
135 # we are just aliasing methods, otherwise
136 # the modifiers go wonky.
137 $class->reset_package_cache_flag;
140 sub apply_override_method_modifiers {
141 my ($self, $role, $class) = @_;
142 foreach my $method_name ($role->get_method_modifier_list('override')) {
143 # it if it has one already then ...
144 if ($class->has_method($method_name)) {
148 # if this is not a role, then we need to
149 # find the original package of the method
150 # so that we can tell the class were to
151 # find the right super() method
152 my $method = $role->get_override_method_modifier($method_name);
153 my ($package) = Class::MOP::get_code_info($method);
154 # if it is a class, we just add it
155 $class->add_override_method_modifier($method_name, $method, $package);
160 sub apply_method_modifiers {
161 my ($self, $modifier_type, $role, $class) = @_;
162 my $add = "add_${modifier_type}_method_modifier";
163 my $get = "get_${modifier_type}_method_modifiers";
164 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
168 ) foreach $role->$get($method_name);
180 Moose::Meta::Role::Application::ToClass - Compose a role into a class
194 =item B<check_role_exclusions>
196 =item B<check_required_methods>
198 =item B<check_required_attributes>
200 =item B<apply_attributes>
202 =item B<apply_methods>
204 =item B<apply_method_modifiers>
206 =item B<apply_override_method_modifiers>
212 All complex software has bugs lurking in it, and this module is no
213 exception. If you find a bug please either email me, or add the bug
218 Stevan Little E<lt>stevan@iinteractive.comE<gt>
220 =head1 COPYRIGHT AND LICENSE
222 Copyright 2006-2008 by Infinity Interactive, Inc.
224 L<http://www.iinteractive.com>
226 This library is free software; you can redistribute it and/or modify
227 it under the same terms as Perl itself.