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);
50 $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
57 return $new_metaclass->reinitialize( $for, %classes );
60 sub apply_base_class_roles {
63 my $for = $options{for_class};
65 my $meta = $for->meta();
67 my $new_base = _make_new_class(
70 [ $meta->superclasses() ],
73 $meta->superclasses($new_base)
74 if $new_base ne $meta->name();
78 my $existing_class = shift;
80 my $superclasses = shift || [$existing_class];
82 return $existing_class unless $roles;
84 my $meta = $existing_class->meta();
86 return $existing_class
87 if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
89 return Moose::Meta::Class->create_anon_class(
90 superclasses => $superclasses,
102 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
106 package MyApp::Moose;
113 use Moose::Util::Meta::Role;
115 use MyApp::Role::Meta::Class;
116 use MyApp::Role::Meta::Method::Constructor;
117 use MyApp::Role::Object;
119 Moose::Exporter->setup_import_methods( also => 'Moose' );
125 Moose->init_meta(%options);
127 Moose::Util::MetaRole::apply_metaclass_roles(
128 for_class => $options{for_class},
129 metaclass_roles => ['MyApp::Role::Meta::Class'],
130 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
133 Moose::Util::MetaRole::apply_base_class_roles(
134 for_class => $options{for_class},
135 roles => ['MyApp::Role::Object'],
138 return $options{for_class}->meta();
143 This utility module is designed to help authors of Moose extensions
144 write extensions that are able to cooperate with other Moose
145 extensions. To do this, you must write your extensions as roles, which
146 can then be dynamically applyied to the caller's metaclasses.
148 This module makes sure to preserve any existing superclasses and roles
149 already set for the meta objects, which means that any number of
150 extensions can apply roles in any order.
154 B<It is very important that you only call this module's functions when
155 your module is imported by the caller>. The process of applying roles
156 to the metaclass reinitializes the metaclass object, which wipes out
157 any existing attributes already defined. However, as long as you do
158 this when your module is imported, the caller should not have any
159 attributes defined yet.
161 The easiest way to ensure that this happens is to use
162 L<Moose::Exporter> and provide an C<init_meta> method that will be
163 called when imported.
167 This module provides two functions.
169 =head2 apply_metaclass_roles( ... )
171 This function will apply roles to one or more metaclasses for the
172 specified class. It accepts the following parameters:
176 =item * for_class => $name
178 This specifies the class for which to alter the meta classes.
180 =item * metaclass_roles => \@roles
182 =item * attribute_metaclass_roles => \@roles
184 =item * method_metaclass_roles => \@roles
186 =item * instance_metaclass_roles => \@roles
188 =item * constructor_class_roles => \@roles
190 =item * destructor_class_roles => \@roles
192 These parameter all specify one or more roles to be applied to the
193 specified metaclass. You can pass any or all of these parameters at
198 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
200 This function will apply the specified roles to the object's base class.
204 Dave Rolsky E<lt>autarch@urth.orgE<gt>
206 =head1 COPYRIGHT AND LICENSE
208 Copyright 2008 by Infinity Interactive, Inc.
210 L<http://www.iinteractive.com>
212 This library is free software; you can redistribute it and/or modify
213 it under the same terms as Perl itself.