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 Class::MOP::remove_metaclass_by_name($for);
49 # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
51 $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
53 grep { $old_meta->can($_) }
60 return $new_metaclass->reinitialize( $for, %classes );
63 sub apply_base_class_roles {
66 my $for = $options{for_class};
68 my $meta = $for->meta();
70 my $new_base = _make_new_class(
73 [ $meta->superclasses() ],
76 $meta->superclasses($new_base)
77 if $new_base ne $meta->name();
81 my $existing_class = shift;
83 my $superclasses = shift || [$existing_class];
85 return $existing_class unless $roles;
87 my $meta = $existing_class->meta();
89 return $existing_class
90 if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
92 return Moose::Meta::Class->create_anon_class(
93 superclasses => $superclasses,
105 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
109 package MyApp::Moose;
116 use Moose::Util::Meta::Role;
118 use MyApp::Role::Meta::Class;
119 use MyApp::Role::Meta::Method::Constructor;
120 use MyApp::Role::Object;
122 Moose::Exporter->setup_import_methods( also => 'Moose' );
128 Moose->init_meta(%options);
130 Moose::Util::MetaRole::apply_metaclass_roles(
131 for_class => $options{for_class},
132 metaclass_roles => ['MyApp::Role::Meta::Class'],
133 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
136 Moose::Util::MetaRole::apply_base_class_roles(
137 for_class => $options{for_class},
138 roles => ['MyApp::Role::Object'],
141 return $options{for_class}->meta();
146 B<The whole concept behind this module is still considered
147 experimental, and it could go away in the future!>
149 This utility module is designed to help authors of Moose extensions
150 write extensions that are able to cooperate with other Moose
151 extensions. To do this, you must write your extensions as roles, which
152 can then be dynamically applyied to the caller's metaclasses.
154 This module makes sure to preserve any existing superclasses and roles
155 already set for the meta objects, which means that any number of
156 extensions can apply roles in any order.
160 B<It is very important that you only call this module's functions when
161 your module is imported by the caller>. The process of applying roles
162 to the metaclass reinitializes the metaclass object, which wipes out
163 any existing attributes already defined. However, as long as you do
164 this when your module is imported, the caller should not have any
165 attributes defined yet.
167 The easiest way to ensure that this happens is to use
168 L<Moose::Exporter> and provide an C<init_meta> method that will be
169 called when imported.
173 This module provides two functions.
175 =head2 apply_metaclass_roles( ... )
177 This function will apply roles to one or more metaclasses for the
178 specified class. It accepts the following parameters:
182 =item * for_class => $name
184 This specifies the class for which to alter the meta classes.
186 =item * metaclass_roles => \@roles
188 =item * attribute_metaclass_roles => \@roles
190 =item * method_metaclass_roles => \@roles
192 =item * instance_metaclass_roles => \@roles
194 =item * constructor_class_roles => \@roles
196 =item * destructor_class_roles => \@roles
198 These parameter all specify one or more roles to be applied to the
199 specified metaclass. You can pass any or all of these parameters at
204 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
206 This function will apply the specified roles to the object's base class.
210 Dave Rolsky E<lt>autarch@urth.orgE<gt>
212 =head1 COPYRIGHT AND LICENSE
214 Copyright 2008 by Infinity Interactive, Inc.
216 L<http://www.iinteractive.com>
218 This library is free software; you can redistribute it and/or modify
219 it under the same terms as Perl itself.