1 package Moose::Meta::Role::Application::ToClass;
7 use Moose::Util 'english_list';
8 use Scalar::Util 'blessed';
10 our $VERSION = '0.70';
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 $class->throw_error("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 $class->throw_error("The class " . $class->name . " does the excluded role '$excluded_role_name'");
34 sub check_required_methods {
35 my ($self, $role, $class) = @_;
41 # we might need to move this down below the
42 # the attributes so that we can require any
43 # attribute accessors. However I am thinking
44 # that maybe those are somehow exempt from
45 # the require methods stuff.
46 foreach my $required_method_name ($role->get_required_method_list) {
48 if (!$class->find_method_by_name($required_method_name)) {
50 next if $self->is_aliased_method($required_method_name);
52 push @missing, $required_method_name;
56 return unless @missing;
61 my $noun = @missing == 1 ? 'method' : 'methods';
64 = Moose::Util::english_list( map { q{'} . $_ . q{'} } @missing );
69 . "' requires the $noun $list "
70 . "to be implemented by '"
71 . $class->name . q{'};
74 $class->throw_error($error);
77 sub check_required_attributes {
81 sub apply_attributes {
82 my ($self, $role, $class) = @_;
83 foreach my $attribute_name ($role->get_attribute_list) {
84 # it if it has one already
85 if ($class->has_attribute($attribute_name) &&
86 # make sure we haven't seen this one already too
87 $class->get_attribute($attribute_name) != $role->get_attribute($attribute_name)) {
91 $class->add_attribute(
93 $role->get_attribute($attribute_name)
100 my ($self, $role, $class) = @_;
101 foreach my $method_name ($role->get_method_list) {
103 unless ($self->is_method_excluded($method_name)) {
104 # it if it has one already
105 if ($class->has_method($method_name) &&
106 # and if they are not the same thing ...
107 $class->get_method($method_name)->body != $role->get_method($method_name)->body) {
111 # add it, although it could be overridden
114 $role->get_method($method_name)
119 if ($self->is_method_aliased($method_name)) {
120 my $aliased_method_name = $self->get_method_aliases->{$method_name};
121 # it if it has one already
122 if ($class->has_method($aliased_method_name) &&
123 # and if they are not the same thing ...
124 $class->get_method($aliased_method_name)->body != $role->get_method($method_name)->body) {
125 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
128 $aliased_method_name,
129 $role->get_method($method_name)
133 # we must reset the cache here since
134 # we are just aliasing methods, otherwise
135 # the modifiers go wonky.
136 $class->reset_package_cache_flag;
139 sub apply_override_method_modifiers {
140 my ($self, $role, $class) = @_;
141 foreach my $method_name ($role->get_method_modifier_list('override')) {
142 # it if it has one already then ...
143 if ($class->has_method($method_name)) {
147 # if this is not a role, then we need to
148 # find the original package of the method
149 # so that we can tell the class were to
150 # find the right super() method
151 my $method = $role->get_override_method_modifier($method_name);
152 my ($package) = Class::MOP::get_code_info($method);
153 # if it is a class, we just add it
154 $class->add_override_method_modifier($method_name, $method, $package);
159 sub apply_method_modifiers {
160 my ($self, $modifier_type, $role, $class) = @_;
161 my $add = "add_${modifier_type}_method_modifier";
162 my $get = "get_${modifier_type}_method_modifiers";
163 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
167 ) foreach $role->$get($method_name);
179 Moose::Meta::Role::Application::ToClass - Compose a role into a class
193 =item B<check_role_exclusions>
195 =item B<check_required_methods>
197 =item B<check_required_attributes>
199 =item B<apply_attributes>
201 =item B<apply_methods>
203 =item B<apply_method_modifiers>
205 =item B<apply_override_method_modifiers>
211 All complex software has bugs lurking in it, and this module is no
212 exception. If you find a bug please either email me, or add the bug
217 Stevan Little E<lt>stevan@iinteractive.comE<gt>
219 =head1 COPYRIGHT AND LICENSE
221 Copyright 2006-2009 by Infinity Interactive, Inc.
223 L<http://www.iinteractive.com>
225 This library is free software; you can redistribute it and/or modify
226 it under the same terms as Perl itself.