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.
208 Dave Rolsky E<lt>autarch@urth.orgE<gt>
210 =head1 COPYRIGHT AND LICENSE
212 Copyright 2008 by Infinity Interactive, Inc.
214 L<http://www.iinteractive.com>
216 This library is free software; you can redistribute it and/or modify
217 it under the same terms as Perl itself.