1 package Moose::Util::MetaRole;
5 use Scalar::Util 'blessed';
8 use List::MoreUtils qw( all );
9 use List::Util qw( first );
10 use Moose::Deprecated;
11 use Scalar::Util qw( blessed );
13 sub apply_metaclass_roles {
14 Moose::Deprecated::deprecated(
15 feature => 'pre-0.94 MetaRole API',
17 'The old Moose::Util::MetaRole API (before version 0.94) has been deprecated.'
18 . ' Using this API will throw an error in Moose 2.0200.'
21 goto &apply_metaroles;
27 _fixup_old_style_args(\%args);
29 my $for = _metathing_for( $args{for} );
31 if ( $for->isa('Moose::Meta::Role') ) {
32 return _make_new_metaclass( $for, $args{role_metaroles}, 'role' );
35 return _make_new_metaclass( $for, $args{class_metaroles}, 'class' );
45 : Class::MOP::class_of($passed);
50 && ( $found->isa('Moose::Meta::Role')
51 || $found->isa('Moose::Meta::Class') );
53 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
56 = 'When using Moose::Util::MetaRole, you must pass a Moose class name,'
57 . ' role name, metaclass object, or metarole object.';
59 if ( defined $found && blessed $found ) {
61 . " You passed $passed, and we resolved this to a "
66 if ( defined $passed && !defined $found ) {
68 . " You passed $passed, and this did not resolve to a metaclass or metarole."
69 . ' Maybe you need to call Moose->init_meta to initialize the metaclass first?';
72 if ( !defined $passed ) {
74 . " You passed an undef."
75 . ' Maybe you need to call Moose->init_meta to initialize the metaclass first?';
79 sub _fixup_old_style_args {
82 return if $args->{class_metaroles} || $args->{role_metaroles};
84 Moose::Deprecated::deprecated(
85 feature => 'pre-0.94 MetaRole API',
87 'The old Moose::Util::MetaRole API (before version 0.94) has been deprecated.'
88 . ' Using this API will throw an error in Moose 2.0200.'
91 $args->{for} = delete $args->{for_class}
92 if exists $args->{for_class};
95 attribute_metaclass_roles
96 method_metaclass_roles
97 wrapped_method_metaclass_roles
98 instance_metaclass_roles
99 constructor_class_roles
100 destructor_class_roles
103 application_to_class_class_roles
104 application_to_role_class_roles
105 application_to_instance_class_roles
106 application_role_summation_class_roles
110 = blessed $args->{for}
112 : Class::MOP::class_of( $args->{for} );
115 if ( $for->isa('Moose::Meta::Class') ) {
116 $top_key = 'class_metaroles';
118 $args->{class_metaroles}{class} = delete $args->{metaclass_roles}
119 if exists $args->{metaclass_roles};
122 $top_key = 'role_metaroles';
124 $args->{role_metaroles}{role} = delete $args->{metaclass_roles}
125 if exists $args->{metaclass_roles};
128 for my $old_key (@old_keys) {
129 my ($new_key) = $old_key =~ /^(.+)_(?:class|metaclass)_roles$/;
131 $args->{$top_key}{$new_key} = delete $args->{$old_key}
132 if exists $args->{$old_key};
138 sub _make_new_metaclass {
143 return $for unless keys %{$roles};
146 = exists $roles->{$primary}
147 ? _make_new_class( ref $for, $roles->{$primary} )
152 for my $key ( grep { $_ ne $primary } keys %{$roles} ) {
153 my $attr = first {$_}
154 map { $for->meta->find_attribute_by_name($_) } (
159 my $reader = $attr->get_read_method;
161 $classes{ $attr->init_arg }
162 = _make_new_class( $for->$reader(), $roles->{$key} );
165 my $new_meta = $new_metaclass->reinitialize( $for, %classes );
170 sub apply_base_class_roles {
173 my $meta = _metathing_for( $args{for} || $args{for_class} );
174 croak 'You can only apply base class roles to a Moose class, not a role.'
175 if $meta->isa('Moose::Meta::Role');
177 my $new_base = _make_new_class(
180 [ $meta->superclasses() ],
183 $meta->superclasses($new_base)
184 if $new_base ne $meta->name();
187 sub _make_new_class {
188 my $existing_class = shift;
190 my $superclasses = shift || [$existing_class];
192 return $existing_class unless $roles;
194 my $meta = Class::MOP::Class->initialize($existing_class);
196 return $existing_class
197 if $meta->can('does_role') && all { $meta->does_role($_) }
198 grep { !ref $_ } @{$roles};
200 return Moose::Meta::Class->create_anon_class(
201 superclasses => $superclasses,
209 # ABSTRACT: Apply roles to any metaclass, as well as the object base class
215 package MyApp::Moose;
219 use Moose::Util::MetaRole;
221 use MyApp::Role::Meta::Class;
222 use MyApp::Role::Meta::Method::Constructor;
223 use MyApp::Role::Object;
225 Moose::Exporter->setup_import_methods( also => 'Moose' );
231 Moose->init_meta(%args);
233 Moose::Util::MetaRole::apply_metaroles(
234 for => $args{for_class},
236 class => => ['MyApp::Role::Meta::Class'],
237 constructor => ['MyApp::Role::Meta::Method::Constructor'],
241 Moose::Util::MetaRole::apply_base_class_roles(
242 for => $args{for_class},
243 roles => ['MyApp::Role::Object'],
246 return $args{for_class}->meta();
251 This utility module is designed to help authors of Moose extensions
252 write extensions that are able to cooperate with other Moose
253 extensions. To do this, you must write your extensions as roles, which
254 can then be dynamically applied to the caller's metaclasses.
256 This module makes sure to preserve any existing superclasses and roles
257 already set for the meta objects, which means that any number of
258 extensions can apply roles in any order.
262 The easiest way to use this module is through L<Moose::Exporter>, which can
263 generate the appropriate C<init_meta> method for you, and make sure it is
264 called when imported.
268 This module provides two functions.
270 =head2 apply_metaroles( ... )
272 This function will apply roles to one or more metaclasses for the specified
273 class. It will return a new metaclass object for the class or role passed in
276 It accepts the following parameters:
282 This specifies the class or for which to alter the meta classes. This can be a
283 package name, or an appropriate meta-object (a L<Moose::Meta::Class> or
284 L<Moose::Meta::Role>).
286 =item * class_metaroles => \%roles
288 This is a hash reference specifying which metaroles will be applied to the
289 class metaclass and its contained metaclasses and helper classes.
291 Each key should in turn point to an array reference of role names.
293 It accepts the following keys:
315 =item * role_metaroles => \%roles
317 This is a hash reference specifying which metaroles will be applied to the
318 role metaclass and its contained metaclasses and helper classes.
320 It accepts the following keys:
330 =item required_method
332 =item conflicting_method
334 =item application_to_class
336 =item application_to_role
338 =item application_to_instance
340 =item application_role_summation
346 =head2 apply_base_class_roles( for => $class, roles => \@roles )
348 This function will apply the specified roles to the object's base class.
352 See L<Moose/BUGS> for details on reporting bugs.