4b12fe68dfccb766cb9576cb3f74373b76c3cb28
[gitmo/Moose.git] / lib / Moose / Util / MetaRole.pm
1 package Moose::Util::MetaRole;
2
3 use strict;
4 use warnings;
5
6 our $VERSION   = '0.68';
7 $VERSION = eval $VERSION;
8 our $AUTHORITY = 'cpan:STEVAN';
9
10 use List::MoreUtils qw( all );
11
12 my @Classes = qw( constructor_class destructor_class error_class );
13
14 sub apply_metaclass_roles {
15     my %options = @_;
16
17     my $for = $options{for_class};
18
19     my %old_classes
20         = map { $_ => $for->meta->$_ } grep { $for->meta->can($_) } @Classes;
21
22     my $meta = _make_new_metaclass( $for, \%options );
23
24     for my $c ( grep { $meta->can($_) } @Classes ) {
25         if ( $options{ $c . '_roles' } ) {
26             my $class = _make_new_class(
27                 $meta->$c(),
28                 $options{ $c . '_roles' }
29             );
30
31             $meta->$c($class);
32         }
33         else {
34             $meta->$c( $old_classes{$c} );
35         }
36     }
37
38     return $meta;
39 }
40
41 sub _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
59     # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
60     my %classes = map {
61         $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
62         }
63         grep { $old_meta->can($_) }
64         qw(
65         attribute_metaclass
66         method_metaclass
67         instance_metaclass
68     );
69
70     return $new_metaclass->reinitialize( $for, %classes );
71 }
72
73 sub 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
90 sub _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
97     my $meta = Class::MOP::Class->initialize($existing_class);
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
109 1;
110
111 __END__
112
113 =head1 NAME
114
115 Moose::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
156 B<The whole concept behind this module is still considered
157 experimental, and it could go away in the future!>
158
159 This utility module is designed to help authors of Moose extensions
160 write extensions that are able to cooperate with other Moose
161 extensions. To do this, you must write your extensions as roles, which
162 can then be dynamically applied to the caller's metaclasses.
163
164 This module makes sure to preserve any existing superclasses and roles
165 already set for the meta objects, which means that any number of
166 extensions can apply roles in any order.
167
168 =head1 USAGE
169
170 B<It is very important that you only call this module's functions when
171 your module is imported by the caller>. The process of applying roles
172 to the metaclass reinitializes the metaclass object, which wipes out
173 any existing attributes already defined. However, as long as you do
174 this when your module is imported, the caller should not have any
175 attributes defined yet.
176
177 The easiest way to ensure that this happens is to use
178 L<Moose::Exporter> and provide an C<init_meta> method that will be
179 called when imported.
180
181 =head1 FUNCTIONS
182
183 This module provides two functions.
184
185 =head2 apply_metaclass_roles( ... )
186
187 This function will apply roles to one or more metaclasses for the
188 specified class. It accepts the following parameters:
189
190 =over 4
191
192 =item * for_class => $name
193
194 This 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
208 These parameter all specify one or more roles to be applied to the
209 specified metaclass. You can pass any or all of these parameters at
210 once.
211
212 =back
213
214 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
215
216 This function will apply the specified roles to the object's base class.
217
218 =head1 PROBLEMS WITH METACLASS ROLES AND SUBCLASS
219
220 Because of the way this module works, there is an ordering problem
221 which occurs in certain situations. This sequence of events causes an
222 error:
223
224 =over 4
225
226 =item 1.
227
228 There is a class (C<ClassA>) which uses some extension(s) that apply
229 roles to the metaclass.
230
231 =item 2.
232
233 You have another class (C<ClassB>) which wants to subclass C<ClassA> and
234 apply some more extensions.
235
236 =back
237
238 Normally, the call to C<extends> will happen at run time, I<after> the
239 additional extensions are applied. This causes an error when we try to
240 make the metaclass for C<ClassB> compatible with the metaclass for
241 C<ClassA>.
242
243 We hope to be able to fix this in the future.
244
245 For now the workaround is for C<ClassB> to make sure it extends C<ClassA>
246 I<before> it loads extensions:
247
248   package ClassB;
249
250   use Moose;
251
252   BEGIN { extends 'ClassA' }
253
254   use MooseX::SomeExtension;
255
256 =head1 AUTHOR
257
258 Dave Rolsky E<lt>autarch@urth.orgE<gt>
259
260 =head1 COPYRIGHT AND LICENSE
261
262 Copyright 2009 by Infinity Interactive, Inc.
263
264 L<http://www.iinteractive.com>
265
266 This library is free software; you can redistribute it and/or modify
267 it under the same terms as Perl itself.
268
269 =cut