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 B<The whole concept behind this module is still considered
144 experimental, and it could go away in the future!>
146 This utility module is designed to help authors of Moose extensions
147 write extensions that are able to cooperate with other Moose
148 extensions. To do this, you must write your extensions as roles, which
149 can then be dynamically applyied to the caller's metaclasses.
151 This module makes sure to preserve any existing superclasses and roles
152 already set for the meta objects, which means that any number of
153 extensions can apply roles in any order.
157 B<It is very important that you only call this module's functions when
158 your module is imported by the caller>. The process of applying roles
159 to the metaclass reinitializes the metaclass object, which wipes out
160 any existing attributes already defined. However, as long as you do
161 this when your module is imported, the caller should not have any
162 attributes defined yet.
164 The easiest way to ensure that this happens is to use
165 L<Moose::Exporter> and provide an C<init_meta> method that will be
166 called when imported.
170 This module provides two functions.
172 =head2 apply_metaclass_roles( ... )
174 This function will apply roles to one or more metaclasses for the
175 specified class. It accepts the following parameters:
179 =item * for_class => $name
181 This specifies the class for which to alter the meta classes.
183 =item * metaclass_roles => \@roles
185 =item * attribute_metaclass_roles => \@roles
187 =item * method_metaclass_roles => \@roles
189 =item * instance_metaclass_roles => \@roles
191 =item * constructor_class_roles => \@roles
193 =item * destructor_class_roles => \@roles
195 These parameter all specify one or more roles to be applied to the
196 specified metaclass. You can pass any or all of these parameters at
201 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
203 This function will apply the specified roles to the object's base class.
207 Dave Rolsky E<lt>autarch@urth.orgE<gt>
209 =head1 COPYRIGHT AND LICENSE
211 Copyright 2008 by Infinity Interactive, Inc.
213 L<http://www.iinteractive.com>
215 This library is free software; you can redistribute it and/or modify
216 it under the same terms as Perl itself.