1 package Moose::Util::MetaRole;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
10 use List::MoreUtils qw( all );
12 my @Classes = qw( constructor_class destructor_class error_class );
14 sub apply_metaclass_roles {
17 my $for = $options{for_class};
20 = map { $_ => $for->meta->$_ } grep { $for->meta->can($_) } @Classes;
22 my $meta = _make_new_metaclass( $for, \%options );
24 for my $c ( grep { $meta->can($_) } @Classes ) {
25 if ( $options{ $c . '_roles' } ) {
26 my $class = _make_new_class(
28 $options{ $c . '_roles' }
34 $meta->$c( $old_classes{$c} );
41 sub _make_new_metaclass {
46 unless grep { exists $options->{ $_ . '_roles' } }
55 = _make_new_class( ref $for->meta(), $options->{metaclass_roles} );
57 my $old_meta = $for->meta();
59 # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
61 $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
63 grep { $old_meta->can($_) }
70 return $new_metaclass->reinitialize( $for, %classes );
73 sub apply_base_class_roles {
76 my $for = $options{for_class};
78 my $meta = $for->meta();
80 my $new_base = _make_new_class(
83 [ $meta->superclasses() ],
86 $meta->superclasses($new_base)
87 if $new_base ne $meta->name();
91 my $existing_class = shift;
93 my $superclasses = shift || [$existing_class];
95 return $existing_class unless $roles;
97 my $meta = Class::MOP::Class->initialize($existing_class);
99 return $existing_class
100 if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
102 return Moose::Meta::Class->create_anon_class(
103 superclasses => $superclasses,
115 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
119 package MyApp::Moose;
126 use Moose::Util::Meta::Role;
128 use MyApp::Role::Meta::Class;
129 use MyApp::Role::Meta::Method::Constructor;
130 use MyApp::Role::Object;
132 Moose::Exporter->setup_import_methods( also => 'Moose' );
138 Moose->init_meta(%options);
140 Moose::Util::MetaRole::apply_metaclass_roles(
141 for_class => $options{for_class},
142 metaclass_roles => ['MyApp::Role::Meta::Class'],
143 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
146 Moose::Util::MetaRole::apply_base_class_roles(
147 for_class => $options{for_class},
148 roles => ['MyApp::Role::Object'],
151 return $options{for_class}->meta();
156 B<The whole concept behind this module is still considered
157 experimental, and it could go away in the future!>
159 This utility module is designed to help authors of Moose extensions
160 write extensions that are able to cooperate with other Moose
161 extensions. To do this, you must write your extensions as roles, which
162 can then be dynamically applied to the caller's metaclasses.
164 This module makes sure to preserve any existing superclasses and roles
165 already set for the meta objects, which means that any number of
166 extensions can apply roles in any order.
170 B<It is very important that you only call this module's functions when
171 your module is imported by the caller>. The process of applying roles
172 to the metaclass reinitializes the metaclass object, which wipes out
173 any existing attributes already defined. However, as long as you do
174 this when your module is imported, the caller should not have any
175 attributes defined yet.
177 The easiest way to ensure that this happens is to use
178 L<Moose::Exporter> and provide an C<init_meta> method that will be
179 called when imported.
183 This module provides two functions.
185 =head2 apply_metaclass_roles( ... )
187 This function will apply roles to one or more metaclasses for the
188 specified class. It accepts the following parameters:
192 =item * for_class => $name
194 This specifies the class for which to alter the meta classes.
196 =item * metaclass_roles => \@roles
198 =item * attribute_metaclass_roles => \@roles
200 =item * method_metaclass_roles => \@roles
202 =item * instance_metaclass_roles => \@roles
204 =item * constructor_class_roles => \@roles
206 =item * destructor_class_roles => \@roles
208 These parameter all specify one or more roles to be applied to the
209 specified metaclass. You can pass any or all of these parameters at
214 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
216 This function will apply the specified roles to the object's base class.
218 =head1 PROBLEMS WITH METACLASS ROLES AND SUBCLASS
220 Because of the way this module works, there is an ordering problem
221 which occurs in certain situations. This sequence of events causes an
228 There is a class (ClassA) which uses some extension(s) that apply
229 roles to the metaclass.
233 You have another class (ClassB) which wants to subclass ClassA and
234 apply some more extensions.
238 Normally, the call to C<extends> will happen at run time, I<after> the
239 additional extensions are applied. This causes an error when we try to
240 make the metaclass for ClassB compatible with the metaclass for
243 We hope to be able to fix this in the future.
245 For now the workaround is for ClassB to make sure it extends ClassA
246 I<before> it loads extensions:
252 BEGIN { extends 'ClassA' }
254 use MooseX::SomeExtension;
258 Dave Rolsky E<lt>autarch@urth.orgE<gt>
260 =head1 COPYRIGHT AND LICENSE
262 Copyright 2008 by Infinity Interactive, Inc.
264 L<http://www.iinteractive.com>
266 This library is free software; you can redistribute it and/or modify
267 it under the same terms as Perl itself.