1 package Moose::Meta::Role::Application::ToClass;
8 use Scalar::Util 'blessed';
12 our $VERSION = '0.01';
13 our $AUTHORITY = 'cpan:STEVAN';
15 use base 'Moose::Meta::Role::Application';
18 my ($self, $role, $class) = @_;
19 $self->SUPER::apply($role, $class);
20 $class->add_role($role);
23 sub check_role_exclusions {
24 my ($self, $role, $class) = @_;
25 if ($class->excludes_role($role->name)) {
26 confess "Conflict detected: " . $class->name . " excludes role '" . $role->name . "'";
28 foreach my $excluded_role_name ($role->get_excluded_roles_list) {
29 if ($class->does_role($excluded_role_name)) {
30 confess "The class " . $class->name . " does the excluded role '$excluded_role_name'";
35 sub check_required_methods {
36 my ($self, $role, $class) = @_;
38 # we might need to move this down below the
39 # the attributes so that we can require any
40 # attribute accessors. However I am thinking
41 # that maybe those are somehow exempt from
42 # the require methods stuff.
43 foreach my $required_method_name ($role->get_required_method_list) {
45 unless ($class->find_method_by_name($required_method_name)) {
46 confess "'" . $role->name . "' requires the method '$required_method_name' " .
47 "to be implemented by '" . $class->name . "'";
51 # we need to make sure that the method is
52 # not a method modifier, because those do
53 # not satisfy the requirements ...
54 my $method = $class->find_method_by_name($required_method_name);
56 # check if it is a generated accessor ...
57 (!$method->isa('Class::MOP::Method::Accessor'))
58 || confess "'" . $role->name . "' requires the method '$required_method_name' " .
59 "to be implemented by '" . $class->name . "', the method is only an attribute accessor";
62 # All other tests here have been removed, they were tests
63 # for overriden methods and before/after/around modifiers.
64 # But we realized that for classes any overriden or modified
65 # methods would be backed by a real method of that name
66 # (and therefore meet the requirement). And for roles, the
67 # overriden and modified methods are "in statis" and so would
68 # not show up in this test anyway (and as a side-effect they
69 # would not fufill the requirement, which is exactly what we
70 # want them to do anyway).
76 sub apply_attributes {
77 my ($self, $role, $class) = @_;
78 foreach my $attribute_name ($role->get_attribute_list) {
79 # it if it has one already
80 if ($class->has_attribute($attribute_name) &&
81 # make sure we haven't seen this one already too
82 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
87 # this is kinda ugly ...
88 if ($class->isa('Moose::Meta::Class')) {
89 $class->_process_attribute(
91 %{$role->get_attribute($attribute_name)}
95 $class->add_attribute(
97 $role->get_attribute($attribute_name)
105 my ($self, $role, $class) = @_;
106 foreach my $method_name ($role->get_method_list) {
107 # it if it has one already
108 if ($class->has_method($method_name) &&
109 # and if they are not the same thing ...
110 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
114 # add it, although it could be overriden
115 $class->alias_method(
117 $role->get_method($method_name)
121 # we must reset the cache here since
122 # we are just aliasing methods, otherwise
123 # the modifiers go wonky.
124 $class->reset_package_cache_flag;
127 sub apply_override_method_modifiers {
128 my ($self, $role, $class) = @_;
129 foreach my $method_name ($role->get_method_modifier_list('override')) {
130 # it if it has one already then ...
131 if ($class->has_method($method_name)) {
135 # if this is not a role, then we need to
136 # find the original package of the method
137 # so that we can tell the class were to
138 # find the right super() method
139 my $method = $role->get_override_method_modifier($method_name);
140 my ($package) = Class::MOP::get_code_info($method);
141 # if it is a class, we just add it
142 $class->add_override_method_modifier($method_name, $method, $package);
147 sub apply_method_modifiers {
148 my ($self, $modifier_type, $role, $class) = @_;
149 my $add = "add_${modifier_type}_method_modifier";
150 my $get = "get_${modifier_type}_method_modifiers";
151 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
155 ) foreach $role->$get($method_name);
159 sub apply_before_method_modifiers { (shift)->apply_method_modifiers('before' => @_) }
160 sub apply_around_method_modifiers { (shift)->apply_method_modifiers('around' => @_) }
161 sub apply_after_method_modifiers { (shift)->apply_method_modifiers('after' => @_) }
171 Moose::Meta::Role::Application::ToClass
185 =item B<check_required_methods>
187 =item B<check_role_exclusions>
189 =item B<apply_attributes>
191 =item B<apply_methods>
193 =item B<apply_method_modifiers>
195 =item B<apply_before_method_modifiers>
197 =item B<apply_after_method_modifiers>
199 =item B<apply_around_method_modifiers>
201 =item B<apply_override_method_modifiers>
207 All complex software has bugs lurking in it, and this module is no
208 exception. If you find a bug please either email me, or add the bug
213 Stevan Little E<lt>stevan@iinteractive.comE<gt>
215 =head1 COPYRIGHT AND LICENSE
217 Copyright 2006, 2007 by Infinity Interactive, Inc.
219 L<http://www.iinteractive.com>
221 This library is free software; you can redistribute it and/or modify
222 it under the same terms as Perl itself.