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
60 application_role_summation_class
64 = _make_new_class( ref $for, $options->{metaclass_roles} );
66 # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
68 $_ => _make_new_class( $for->$_(), $options->{ $_ . '_roles' } )
70 grep { $for->can($_) }
74 wrapped_method_metaclass
76 application_to_class_class
77 application_to_role_class
78 application_to_instance_class
79 application_role_summation_class
82 return $new_metaclass->reinitialize( $for, %classes );
85 sub apply_base_class_roles {
88 my $for = $options{for_class};
90 my $meta = Class::MOP::class_of($for);
92 my $new_base = _make_new_class(
95 [ $meta->superclasses() ],
98 $meta->superclasses($new_base)
99 if $new_base ne $meta->name();
102 sub _make_new_class {
103 my $existing_class = shift;
105 my $superclasses = shift || [$existing_class];
107 return $existing_class unless $roles;
109 my $meta = Class::MOP::Class->initialize($existing_class);
111 return $existing_class
112 if $meta->can('does_role') && all { $meta->does_role($_) }
113 grep { !ref $_ } @{$roles};
115 return Moose::Meta::Class->create_anon_class(
116 superclasses => $superclasses,
128 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
132 package MyApp::Moose;
136 use Moose::Util::MetaRole;
138 use MyApp::Role::Meta::Class;
139 use MyApp::Role::Meta::Method::Constructor;
140 use MyApp::Role::Object;
142 Moose::Exporter->setup_import_methods( also => 'Moose' );
148 Moose->init_meta(%options);
150 Moose::Util::MetaRole::apply_metaclass_roles(
151 for_class => $options{for_class},
152 metaclass_roles => ['MyApp::Role::Meta::Class'],
153 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
156 Moose::Util::MetaRole::apply_base_class_roles(
157 for_class => $options{for_class},
158 roles => ['MyApp::Role::Object'],
161 return $options{for_class}->meta();
166 This utility module is designed to help authors of Moose extensions
167 write extensions that are able to cooperate with other Moose
168 extensions. To do this, you must write your extensions as roles, which
169 can then be dynamically applied to the caller's metaclasses.
171 This module makes sure to preserve any existing superclasses and roles
172 already set for the meta objects, which means that any number of
173 extensions can apply roles in any order.
177 B<It is very important that you only call this module's functions when
178 your module is imported by the caller>. The process of applying roles
179 to the metaclass reinitializes the metaclass object, which wipes out
180 any existing attributes already defined. However, as long as you do
181 this when your module is imported, the caller should not have any
182 attributes defined yet.
184 The easiest way to ensure that this happens is to use
185 L<Moose::Exporter>, which can generate the appropriate C<init_meta>
186 method for you, and make sure it is called when imported.
190 This module provides two functions.
192 =head2 apply_metaclass_roles( ... )
194 This function will apply roles to one or more metaclasses for the
195 specified class. It accepts the following parameters:
199 =item * for_class => $name
201 This specifies the class for which to alter the meta classes.
203 =item * metaclass_roles => \@roles
205 =item * attribute_metaclass_roles => \@roles
207 =item * method_metaclass_roles => \@roles
209 =item * wrapped_method_metaclass_roles => \@roles
211 =item * instance_metaclass_roles => \@roles
213 =item * constructor_class_roles => \@roles
215 =item * destructor_class_roles => \@roles
217 =item * application_to_class_class_roles => \@roles
219 =item * application_to_role_class_roles => \@roles
221 =item * application_to_instance_class_roles => \@roles
223 These parameter all specify one or more roles to be applied to the
224 specified metaclass. You can pass any or all of these parameters at
229 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
231 This function will apply the specified roles to the object's base class.
235 Dave Rolsky E<lt>autarch@urth.orgE<gt>
237 =head1 COPYRIGHT AND LICENSE
239 Copyright 2009 by Infinity Interactive, Inc.
241 L<http://www.iinteractive.com>
243 This library is free software; you can redistribute it and/or modify
244 it under the same terms as Perl itself.