1 package Moose::Util::MetaRole;
6 use List::MoreUtils qw( all );
8 sub apply_metaclass_roles {
11 my $for = $options{for_class};
13 my $meta = _make_new_metaclass( $for, \%options );
15 for my $tor_class ( grep { $options{ $_ . '_roles' } }
16 qw( constructor_class destructor_class ) ) {
18 my $class = _make_new_class(
20 $options{ $tor_class . '_roles' }
23 $meta->$tor_class($class);
29 sub _make_new_metaclass {
34 unless grep { exists $options->{ $_ . '_roles' } }
43 = _make_new_class( ref $for->meta(), $options->{metaclass_roles} );
45 my $old_meta = $for->meta();
47 # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
49 $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
51 grep { $old_meta->can($_) }
58 return $new_metaclass->reinitialize( $for, %classes );
61 sub apply_base_class_roles {
64 my $for = $options{for_class};
66 my $meta = $for->meta();
68 my $new_base = _make_new_class(
71 [ $meta->superclasses() ],
74 $meta->superclasses($new_base)
75 if $new_base ne $meta->name();
79 my $existing_class = shift;
81 my $superclasses = shift || [$existing_class];
83 return $existing_class unless $roles;
85 my $meta = $existing_class->meta();
87 return $existing_class
88 if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
90 return Moose::Meta::Class->create_anon_class(
91 superclasses => $superclasses,
103 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
107 package MyApp::Moose;
114 use Moose::Util::Meta::Role;
116 use MyApp::Role::Meta::Class;
117 use MyApp::Role::Meta::Method::Constructor;
118 use MyApp::Role::Object;
120 Moose::Exporter->setup_import_methods( also => 'Moose' );
126 Moose->init_meta(%options);
128 Moose::Util::MetaRole::apply_metaclass_roles(
129 for_class => $options{for_class},
130 metaclass_roles => ['MyApp::Role::Meta::Class'],
131 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
134 Moose::Util::MetaRole::apply_base_class_roles(
135 for_class => $options{for_class},
136 roles => ['MyApp::Role::Object'],
139 return $options{for_class}->meta();
144 B<The whole concept behind this module is still considered
145 experimental, and it could go away in the future!>
147 This utility module is designed to help authors of Moose extensions
148 write extensions that are able to cooperate with other Moose
149 extensions. To do this, you must write your extensions as roles, which
150 can then be dynamically applyied to the caller's metaclasses.
152 This module makes sure to preserve any existing superclasses and roles
153 already set for the meta objects, which means that any number of
154 extensions can apply roles in any order.
158 B<It is very important that you only call this module's functions when
159 your module is imported by the caller>. The process of applying roles
160 to the metaclass reinitializes the metaclass object, which wipes out
161 any existing attributes already defined. However, as long as you do
162 this when your module is imported, the caller should not have any
163 attributes defined yet.
165 The easiest way to ensure that this happens is to use
166 L<Moose::Exporter> and provide an C<init_meta> method that will be
167 called when imported.
171 This module provides two functions.
173 =head2 apply_metaclass_roles( ... )
175 This function will apply roles to one or more metaclasses for the
176 specified class. It accepts the following parameters:
180 =item * for_class => $name
182 This specifies the class for which to alter the meta classes.
184 =item * metaclass_roles => \@roles
186 =item * attribute_metaclass_roles => \@roles
188 =item * method_metaclass_roles => \@roles
190 =item * instance_metaclass_roles => \@roles
192 =item * constructor_class_roles => \@roles
194 =item * destructor_class_roles => \@roles
196 These parameter all specify one or more roles to be applied to the
197 specified metaclass. You can pass any or all of these parameters at
202 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
204 This function will apply the specified roles to the object's base class.
206 =head1 PROBLEMS WITH METACLASS ROLES AND SUBCLASS
208 Because of the way this module works, there is an ordering problem
209 which occurs in certain situations. This sequence of events causes an
216 There is a class (ClassA) which uses some extension(s) that apply
217 roles to the metaclass.
221 You have another class (ClassB) which wants to subclass ClassA and
222 apply some more extensions.
226 Normally, the call to C<extends> will happen at run time, I<after> the
227 additional extensions are applied. This causes an error when we try to
228 make the metaclass for ClassB compatible with the metaclass for
231 We hope to be able to fix this in the future.
233 For now the workaround is for ClassB to make sure it extends ClassA
234 I<before> it loads extensions:
240 BEGIN { extends 'ClassA' }
242 use MooseX::SomeExtension;
246 Dave Rolsky E<lt>autarch@urth.orgE<gt>
248 =head1 COPYRIGHT AND LICENSE
250 Copyright 2008 by Infinity Interactive, Inc.
252 L<http://www.iinteractive.com>
254 This library is free software; you can redistribute it and/or modify
255 it under the same terms as Perl itself.