bump version to 0.66
[gitmo/Moose.git] / lib / Moose / Util / MetaRole.pm
CommitLineData
231be3be 1package Moose::Util::MetaRole;
2
3use strict;
4use warnings;
5
aa7bbf26 6our $VERSION = '0.66';
ae18d5ec 7$VERSION = eval $VERSION;
8our $AUTHORITY = 'cpan:STEVAN';
9
231be3be 10use List::MoreUtils qw( all );
11
8f05895e 12my @Classes = qw( constructor_class destructor_class error_class );
13
231be3be 14sub apply_metaclass_roles {
15 my %options = @_;
16
17 my $for = $options{for_class};
18
9eaf623d 19 my %old_classes
20 = map { $_ => $for->meta->$_ } grep { $for->meta->can($_) } @Classes;
231be3be 21
8f05895e 22 my $meta = _make_new_metaclass( $for, \%options );
231be3be 23
9eaf623d 24 for my $c ( grep { $meta->can($_) } @Classes ) {
8f05895e 25 if ( $options{ $c . '_roles' } ) {
26 my $class = _make_new_class(
27 $meta->$c(),
28 $options{ $c . '_roles' }
29 );
231be3be 30
8f05895e 31 $meta->$c($class);
32 }
33 else {
34 $meta->$c( $old_classes{$c} );
35 }
231be3be 36 }
37
38 return $meta;
39}
40
41sub _make_new_metaclass {
42 my $for = shift;
43 my $options = shift;
44
45 return $for->meta()
46 unless grep { exists $options->{ $_ . '_roles' } }
47 qw(
48 metaclass
49 attribute_metaclass
50 method_metaclass
51 instance_metaclass
52 );
53
54 my $new_metaclass
55 = _make_new_class( ref $for->meta(), $options->{metaclass_roles} );
56
57 my $old_meta = $for->meta();
58
72d15b83 59 # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
231be3be 60 my %classes = map {
61 $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
72d15b83 62 }
63 grep { $old_meta->can($_) }
64 qw(
231be3be 65 attribute_metaclass
66 method_metaclass
67 instance_metaclass
68 );
69
70 return $new_metaclass->reinitialize( $for, %classes );
71}
72
73sub apply_base_class_roles {
74 my %options = @_;
75
76 my $for = $options{for_class};
77
78 my $meta = $for->meta();
79
80 my $new_base = _make_new_class(
81 $for,
82 $options{roles},
83 [ $meta->superclasses() ],
84 );
85
86 $meta->superclasses($new_base)
87 if $new_base ne $meta->name();
88}
89
90sub _make_new_class {
91 my $existing_class = shift;
92 my $roles = shift;
93 my $superclasses = shift || [$existing_class];
94
95 return $existing_class unless $roles;
96
8f05895e 97 my $meta = Class::MOP::Class->initialize($existing_class);
231be3be 98
99 return $existing_class
100 if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
101
102 return Moose::Meta::Class->create_anon_class(
103 superclasses => $superclasses,
104 roles => $roles,
105 cache => 1,
106 )->name();
107}
108
1091;
c59bc009 110
111__END__
112
113=head1 NAME
114
115Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
116
117=head1 SYNOPSIS
118
119 package MyApp::Moose;
120
121 use strict;
122 use warnings;
123
124 use Moose ();
125 use Moose::Exporter;
126 use Moose::Util::Meta::Role;
127
128 use MyApp::Role::Meta::Class;
129 use MyApp::Role::Meta::Method::Constructor;
130 use MyApp::Role::Object;
131
132 Moose::Exporter->setup_import_methods( also => 'Moose' );
133
134 sub init_meta {
135 shift;
136 my %options = @_;
137
138 Moose->init_meta(%options);
139
140 Moose::Util::MetaRole::apply_metaclass_roles(
141 for_class => $options{for_class},
142 metaclass_roles => ['MyApp::Role::Meta::Class'],
143 constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
144 );
145
146 Moose::Util::MetaRole::apply_base_class_roles(
147 for_class => $options{for_class},
148 roles => ['MyApp::Role::Object'],
149 );
150
151 return $options{for_class}->meta();
152 }
153
154=head1 DESCRIPTION
155
dd9a6d87 156B<The whole concept behind this module is still considered
157experimental, and it could go away in the future!>
158
c59bc009 159This utility module is designed to help authors of Moose extensions
160write extensions that are able to cooperate with other Moose
161extensions. To do this, you must write your extensions as roles, which
52a919fe 162can then be dynamically applied to the caller's metaclasses.
c59bc009 163
164This module makes sure to preserve any existing superclasses and roles
165already set for the meta objects, which means that any number of
166extensions can apply roles in any order.
167
168=head1 USAGE
169
170B<It is very important that you only call this module's functions when
171your module is imported by the caller>. The process of applying roles
172to the metaclass reinitializes the metaclass object, which wipes out
173any existing attributes already defined. However, as long as you do
174this when your module is imported, the caller should not have any
175attributes defined yet.
176
177The easiest way to ensure that this happens is to use
178L<Moose::Exporter> and provide an C<init_meta> method that will be
179called when imported.
180
181=head1 FUNCTIONS
182
183This module provides two functions.
184
185=head2 apply_metaclass_roles( ... )
186
187This function will apply roles to one or more metaclasses for the
188specified class. It accepts the following parameters:
189
190=over 4
191
192=item * for_class => $name
193
194This specifies the class for which to alter the meta classes.
195
196=item * metaclass_roles => \@roles
197
198=item * attribute_metaclass_roles => \@roles
199
200=item * method_metaclass_roles => \@roles
201
202=item * instance_metaclass_roles => \@roles
203
204=item * constructor_class_roles => \@roles
205
206=item * destructor_class_roles => \@roles
207
208These parameter all specify one or more roles to be applied to the
209specified metaclass. You can pass any or all of these parameters at
210once.
211
212=back
213
214=head2 apply_base_class_roles( for_class => $class, roles => \@roles )
215
216This function will apply the specified roles to the object's base class.
217
eaeacdd6 218=head1 PROBLEMS WITH METACLASS ROLES AND SUBCLASS
219
220Because of the way this module works, there is an ordering problem
221which occurs in certain situations. This sequence of events causes an
222error:
223
224=over 4
225
226=item 1.
227
228There is a class (ClassA) which uses some extension(s) that apply
229roles to the metaclass.
230
231=item 2.
232
233You have another class (ClassB) which wants to subclass ClassA and
234apply some more extensions.
235
236=back
237
238Normally, the call to C<extends> will happen at run time, I<after> the
239additional extensions are applied. This causes an error when we try to
240make the metaclass for ClassB compatible with the metaclass for
241ClassA.
242
243We hope to be able to fix this in the future.
244
245For now the workaround is for ClassB to make sure it extends ClassA
246I<before> it loads extensions:
247
248 package ClassB;
249
250 use Moose;
251
252 BEGIN { extends 'ClassA' }
253
254 use MooseX::SomeExtension;
255
c59bc009 256=head1 AUTHOR
257
258Dave Rolsky E<lt>autarch@urth.orgE<gt>
259
260=head1 COPYRIGHT AND LICENSE
261
2840a3b2 262Copyright 2009 by Infinity Interactive, Inc.
c59bc009 263
264L<http://www.iinteractive.com>
265
266This library is free software; you can redistribute it and/or modify
267it under the same terms as Perl itself.
268
269=cut