1 package Moose::Util::MetaRole;
5 use Scalar::Util 'blessed';
7 our $VERSION = '0.89_02';
8 $VERSION = eval $VERSION;
9 our $AUTHORITY = 'cpan:STEVAN';
11 use List::MoreUtils qw( all );
13 my @Classes = qw( constructor_class destructor_class error_class );
15 sub apply_metaclass_roles {
18 my $for = blessed $options{for_class}
20 : Class::MOP::class_of($options{for_class});
22 my %old_classes = map { $_ => $for->$_ }
23 grep { $for->can($_) }
26 my $meta = _make_new_metaclass( $for, \%options );
28 for my $c ( grep { $meta->can($_) } @Classes ) {
29 if ( $options{ $c . '_roles' } ) {
30 my $class = _make_new_class(
32 $options{ $c . '_roles' }
38 $meta->$c( $old_classes{$c} );
45 sub _make_new_metaclass {
50 unless grep { exists $options->{ $_ . '_roles' } }
55 wrapped_method_metaclass
57 application_to_class_class
58 application_to_role_class
59 application_to_instance_class
63 = _make_new_class( ref $for, $options->{metaclass_roles} );
65 # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
67 $_ => _make_new_class( $for->$_(), $options->{ $_ . '_roles' } )
69 grep { $for->can($_) }
73 wrapped_method_metaclass
75 application_to_class_class
76 application_to_role_class
77 application_to_instance_class
80 return $new_metaclass->reinitialize( $for, %classes );
83 sub apply_base_class_roles {
86 my $for = $options{for_class};
88 my $meta = Class::MOP::class_of($for);
90 my $new_base = _make_new_class(
93 [ $meta->superclasses() ],
96 $meta->superclasses($new_base)
97 if $new_base ne $meta->name();
100 sub _make_new_class {
101 my $existing_class = shift;
103 my $superclasses = shift || [$existing_class];
105 return $existing_class unless $roles;
107 my $meta = Class::MOP::Class->initialize($existing_class);
109 return $existing_class
110 if $meta->can('does_role') && all { $meta->does_role($_) }
111 grep { !ref $_ } @{$roles};
113 return Moose::Meta::Class->create_anon_class(
114 superclasses => $superclasses,
126 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
130 package MyApp::Moose;
134 use Moose::Util::MetaRole;
136 use MyApp::Role::Meta::Class;
137 use MyApp::Role::Meta::Method::Constructor;
138 use MyApp::Role::Object;
140 Moose::Exporter->setup_import_methods( also => 'Moose' );
146 Moose->init_meta(%options);
148 Moose::Util::MetaRole::apply_metaclass_roles(
149 for_class => $options{for_class},
150 metaclass_roles => ['MyApp::Role::Meta::Class'],
151 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
154 Moose::Util::MetaRole::apply_base_class_roles(
155 for_class => $options{for_class},
156 roles => ['MyApp::Role::Object'],
159 return $options{for_class}->meta();
164 This utility module is designed to help authors of Moose extensions
165 write extensions that are able to cooperate with other Moose
166 extensions. To do this, you must write your extensions as roles, which
167 can then be dynamically applied to the caller's metaclasses.
169 This module makes sure to preserve any existing superclasses and roles
170 already set for the meta objects, which means that any number of
171 extensions can apply roles in any order.
175 B<It is very important that you only call this module's functions when
176 your module is imported by the caller>. The process of applying roles
177 to the metaclass reinitializes the metaclass object, which wipes out
178 any existing attributes already defined. However, as long as you do
179 this when your module is imported, the caller should not have any
180 attributes defined yet.
182 The easiest way to ensure that this happens is to use
183 L<Moose::Exporter>, which can generate the appropriate C<init_meta>
184 method for you, and make sure it is called when imported.
188 This module provides two functions.
190 =head2 apply_metaclass_roles( ... )
192 This function will apply roles to one or more metaclasses for the
193 specified class. It accepts the following parameters:
197 =item * for_class => $name
199 This specifies the class for which to alter the meta classes.
201 =item * metaclass_roles => \@roles
203 =item * attribute_metaclass_roles => \@roles
205 =item * method_metaclass_roles => \@roles
207 =item * wrapped_method_metaclass_roles => \@roles
209 =item * instance_metaclass_roles => \@roles
211 =item * constructor_class_roles => \@roles
213 =item * destructor_class_roles => \@roles
215 =item * application_to_class_class_roles => \@roles
217 =item * application_to_role_class_roles => \@roles
219 =item * application_to_instance_class_roles => \@roles
221 These parameter all specify one or more roles to be applied to the
222 specified metaclass. You can pass any or all of these parameters at
227 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
229 This function will apply the specified roles to the object's base class.
233 Dave Rolsky E<lt>autarch@urth.orgE<gt>
235 =head1 COPYRIGHT AND LICENSE
237 Copyright 2009 by Infinity Interactive, Inc.
239 L<http://www.iinteractive.com>
241 This library is free software; you can redistribute it and/or modify
242 it under the same terms as Perl itself.