1 package Moose::Util::MetaRole;
6 use List::MoreUtils qw( all );
8 my @Classes = qw( constructor_class destructor_class error_class );
10 sub apply_metaclass_roles {
13 my $for = $options{for_class};
16 = map { $_ => $for->meta->$_ } grep { $for->meta->can($_) } @Classes;
18 my $meta = _make_new_metaclass( $for, \%options );
20 for my $c ( grep { $meta->can($_) } @Classes ) {
21 if ( $options{ $c . '_roles' } ) {
22 my $class = _make_new_class(
24 $options{ $c . '_roles' }
30 $meta->$c( $old_classes{$c} );
37 sub _make_new_metaclass {
42 unless grep { exists $options->{ $_ . '_roles' } }
51 = _make_new_class( ref $for->meta(), $options->{metaclass_roles} );
53 my $old_meta = $for->meta();
55 # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
57 $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
59 grep { $old_meta->can($_) }
66 return $new_metaclass->reinitialize( $for, %classes );
69 sub apply_base_class_roles {
72 my $for = $options{for_class};
74 my $meta = $for->meta();
76 my $new_base = _make_new_class(
79 [ $meta->superclasses() ],
82 $meta->superclasses($new_base)
83 if $new_base ne $meta->name();
87 my $existing_class = shift;
89 my $superclasses = shift || [$existing_class];
91 return $existing_class unless $roles;
93 my $meta = Class::MOP::Class->initialize($existing_class);
95 return $existing_class
96 if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
98 return Moose::Meta::Class->create_anon_class(
99 superclasses => $superclasses,
111 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
115 package MyApp::Moose;
122 use Moose::Util::Meta::Role;
124 use MyApp::Role::Meta::Class;
125 use MyApp::Role::Meta::Method::Constructor;
126 use MyApp::Role::Object;
128 Moose::Exporter->setup_import_methods( also => 'Moose' );
134 Moose->init_meta(%options);
136 Moose::Util::MetaRole::apply_metaclass_roles(
137 for_class => $options{for_class},
138 metaclass_roles => ['MyApp::Role::Meta::Class'],
139 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
142 Moose::Util::MetaRole::apply_base_class_roles(
143 for_class => $options{for_class},
144 roles => ['MyApp::Role::Object'],
147 return $options{for_class}->meta();
152 B<The whole concept behind this module is still considered
153 experimental, and it could go away in the future!>
155 This utility module is designed to help authors of Moose extensions
156 write extensions that are able to cooperate with other Moose
157 extensions. To do this, you must write your extensions as roles, which
158 can then be dynamically applyied to the caller's metaclasses.
160 This module makes sure to preserve any existing superclasses and roles
161 already set for the meta objects, which means that any number of
162 extensions can apply roles in any order.
166 B<It is very important that you only call this module's functions when
167 your module is imported by the caller>. The process of applying roles
168 to the metaclass reinitializes the metaclass object, which wipes out
169 any existing attributes already defined. However, as long as you do
170 this when your module is imported, the caller should not have any
171 attributes defined yet.
173 The easiest way to ensure that this happens is to use
174 L<Moose::Exporter> and provide an C<init_meta> method that will be
175 called when imported.
179 This module provides two functions.
181 =head2 apply_metaclass_roles( ... )
183 This function will apply roles to one or more metaclasses for the
184 specified class. It accepts the following parameters:
188 =item * for_class => $name
190 This specifies the class for which to alter the meta classes.
192 =item * metaclass_roles => \@roles
194 =item * attribute_metaclass_roles => \@roles
196 =item * method_metaclass_roles => \@roles
198 =item * instance_metaclass_roles => \@roles
200 =item * constructor_class_roles => \@roles
202 =item * destructor_class_roles => \@roles
204 These parameter all specify one or more roles to be applied to the
205 specified metaclass. You can pass any or all of these parameters at
210 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
212 This function will apply the specified roles to the object's base class.
214 =head1 PROBLEMS WITH METACLASS ROLES AND SUBCLASS
216 Because of the way this module works, there is an ordering problem
217 which occurs in certain situations. This sequence of events causes an
224 There is a class (ClassA) which uses some extension(s) that apply
225 roles to the metaclass.
229 You have another class (ClassB) which wants to subclass ClassA and
230 apply some more extensions.
234 Normally, the call to C<extends> will happen at run time, I<after> the
235 additional extensions are applied. This causes an error when we try to
236 make the metaclass for ClassB compatible with the metaclass for
239 We hope to be able to fix this in the future.
241 For now the workaround is for ClassB to make sure it extends ClassA
242 I<before> it loads extensions:
248 BEGIN { extends 'ClassA' }
250 use MooseX::SomeExtension;
254 Dave Rolsky E<lt>autarch@urth.orgE<gt>
256 =head1 COPYRIGHT AND LICENSE
258 Copyright 2008 by Infinity Interactive, Inc.
260 L<http://www.iinteractive.com>
262 This library is free software; you can redistribute it and/or modify
263 it under the same terms as Perl itself.