1 package Moose::Util::MetaRole;
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
10 use List::MoreUtils qw( all );
12 my @Classes = qw( constructor_class destructor_class error_class );
14 sub apply_metaclass_roles {
17 my $for = $options{for_class};
19 my %old_classes = map { $_ => Class::MOP::class_of($for)->$_ }
20 grep { Class::MOP::class_of($for)->can($_) }
23 my $meta = _make_new_metaclass( $for, \%options );
25 for my $c ( grep { $meta->can($_) } @Classes ) {
26 if ( $options{ $c . '_roles' } ) {
27 my $class = _make_new_class(
29 $options{ $c . '_roles' }
35 $meta->$c( $old_classes{$c} );
42 sub _make_new_metaclass {
46 return Class::MOP::class_of($for)
47 unless grep { exists $options->{ $_ . '_roles' } }
52 wrapped_method_metaclass
56 my $old_meta = Class::MOP::class_of($for);
58 = _make_new_class( ref $old_meta, $options->{metaclass_roles} );
60 # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
62 $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
64 grep { $old_meta->can($_) }
68 wrapped_method_metaclass
72 return $new_metaclass->reinitialize( $for, %classes );
75 sub apply_base_class_roles {
78 my $for = $options{for_class};
80 my $meta = Class::MOP::class_of($for);
82 my $new_base = _make_new_class(
85 [ $meta->superclasses() ],
88 $meta->superclasses($new_base)
89 if $new_base ne $meta->name();
93 my $existing_class = shift;
95 my $superclasses = shift || [$existing_class];
97 return $existing_class unless $roles;
99 my $meta = Class::MOP::Class->initialize($existing_class);
101 return $existing_class
102 if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
104 return Moose::Meta::Class->create_anon_class(
105 superclasses => $superclasses,
117 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
121 package MyApp::Moose;
125 use Moose::Util::MetaRole;
127 use MyApp::Role::Meta::Class;
128 use MyApp::Role::Meta::Method::Constructor;
129 use MyApp::Role::Object;
131 Moose::Exporter->setup_import_methods( also => 'Moose' );
137 Moose->init_meta(%options);
139 Moose::Util::MetaRole::apply_metaclass_roles(
140 for_class => $options{for_class},
141 metaclass_roles => ['MyApp::Role::Meta::Class'],
142 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
145 Moose::Util::MetaRole::apply_base_class_roles(
146 for_class => $options{for_class},
147 roles => ['MyApp::Role::Object'],
150 return $options{for_class}->meta();
155 This utility module is designed to help authors of Moose extensions
156 write extensions that are able to cooperate with other Moose
157 extensions. To do this, you must write your extensions as roles, which
158 can then be dynamically applied to the caller's metaclasses.
160 This module makes sure to preserve any existing superclasses and roles
161 already set for the meta objects, which means that any number of
162 extensions can apply roles in any order.
166 B<It is very important that you only call this module's functions when
167 your module is imported by the caller>. The process of applying roles
168 to the metaclass reinitializes the metaclass object, which wipes out
169 any existing attributes already defined. However, as long as you do
170 this when your module is imported, the caller should not have any
171 attributes defined yet.
173 The easiest way to ensure that this happens is to use
174 L<Moose::Exporter> and provide an C<init_meta> method that will be
175 called when imported.
179 This module provides two functions.
181 =head2 apply_metaclass_roles( ... )
183 This function will apply roles to one or more metaclasses for the
184 specified class. It accepts the following parameters:
188 =item * for_class => $name
190 This specifies the class for which to alter the meta classes.
192 =item * metaclass_roles => \@roles
194 =item * attribute_metaclass_roles => \@roles
196 =item * method_metaclass_roles => \@roles
198 =item * wrapped_method_metaclass_roles => \@roles
200 =item * instance_metaclass_roles => \@roles
202 =item * constructor_class_roles => \@roles
204 =item * destructor_class_roles => \@roles
206 These parameter all specify one or more roles to be applied to the
207 specified metaclass. You can pass any or all of these parameters at
212 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
214 This function will apply the specified roles to the object's base class.
218 Dave Rolsky E<lt>autarch@urth.orgE<gt>
220 =head1 COPYRIGHT AND LICENSE
222 Copyright 2009 by Infinity Interactive, Inc.
224 L<http://www.iinteractive.com>
226 This library is free software; you can redistribute it and/or modify
227 it under the same terms as Perl itself.