Commit | Line | Data |
231be3be |
1 | package Moose::Util::MetaRole; |
2 | |
3 | use strict; |
4 | use warnings; |
114134a5 |
5 | use Scalar::Util 'blessed'; |
231be3be |
6 | |
8de57178 |
7 | our $VERSION = '0.92'; |
ae18d5ec |
8 | $VERSION = eval $VERSION; |
9 | our $AUTHORITY = 'cpan:STEVAN'; |
10 | |
231be3be |
11 | use List::MoreUtils qw( all ); |
12 | |
8f05895e |
13 | my @Classes = qw( constructor_class destructor_class error_class ); |
14 | |
231be3be |
15 | sub apply_metaclass_roles { |
16 | my %options = @_; |
17 | |
114134a5 |
18 | my $for = blessed $options{for_class} |
19 | ? $options{for_class} |
20 | : Class::MOP::class_of($options{for_class}); |
231be3be |
21 | |
114134a5 |
22 | my %old_classes = map { $_ => $for->$_ } |
23 | grep { $for->can($_) } |
9f605368 |
24 | @Classes; |
231be3be |
25 | |
8f05895e |
26 | my $meta = _make_new_metaclass( $for, \%options ); |
231be3be |
27 | |
9eaf623d |
28 | for my $c ( grep { $meta->can($_) } @Classes ) { |
8f05895e |
29 | if ( $options{ $c . '_roles' } ) { |
30 | my $class = _make_new_class( |
31 | $meta->$c(), |
32 | $options{ $c . '_roles' } |
33 | ); |
231be3be |
34 | |
8f05895e |
35 | $meta->$c($class); |
36 | } |
37 | else { |
38 | $meta->$c( $old_classes{$c} ); |
39 | } |
231be3be |
40 | } |
41 | |
42 | return $meta; |
43 | } |
44 | |
45 | sub _make_new_metaclass { |
46 | my $for = shift; |
47 | my $options = shift; |
48 | |
114134a5 |
49 | return $for |
231be3be |
50 | unless grep { exists $options->{ $_ . '_roles' } } |
51 | qw( |
52 | metaclass |
53 | attribute_metaclass |
54 | method_metaclass |
8286fcd6 |
55 | wrapped_method_metaclass |
231be3be |
56 | instance_metaclass |
d401dc20 |
57 | application_to_class_class |
58 | application_to_role_class |
59 | application_to_instance_class |
87f55e33 |
60 | application_role_summation_class |
231be3be |
61 | ); |
62 | |
63 | my $new_metaclass |
114134a5 |
64 | = _make_new_class( ref $for, $options->{metaclass_roles} ); |
231be3be |
65 | |
72d15b83 |
66 | # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class |
231be3be |
67 | my %classes = map { |
114134a5 |
68 | $_ => _make_new_class( $for->$_(), $options->{ $_ . '_roles' } ) |
72d15b83 |
69 | } |
114134a5 |
70 | grep { $for->can($_) } |
72d15b83 |
71 | qw( |
231be3be |
72 | attribute_metaclass |
73 | method_metaclass |
8286fcd6 |
74 | wrapped_method_metaclass |
231be3be |
75 | instance_metaclass |
d401dc20 |
76 | application_to_class_class |
77 | application_to_role_class |
78 | application_to_instance_class |
87f55e33 |
79 | application_role_summation_class |
231be3be |
80 | ); |
81 | |
82 | return $new_metaclass->reinitialize( $for, %classes ); |
83 | } |
84 | |
85 | sub apply_base_class_roles { |
86 | my %options = @_; |
87 | |
88 | my $for = $options{for_class}; |
89 | |
95f64261 |
90 | my $meta = Class::MOP::class_of($for); |
231be3be |
91 | |
92 | my $new_base = _make_new_class( |
93 | $for, |
94 | $options{roles}, |
95 | [ $meta->superclasses() ], |
96 | ); |
97 | |
98 | $meta->superclasses($new_base) |
99 | if $new_base ne $meta->name(); |
100 | } |
101 | |
102 | sub _make_new_class { |
103 | my $existing_class = shift; |
104 | my $roles = shift; |
105 | my $superclasses = shift || [$existing_class]; |
106 | |
107 | return $existing_class unless $roles; |
108 | |
8f05895e |
109 | my $meta = Class::MOP::Class->initialize($existing_class); |
231be3be |
110 | |
111 | return $existing_class |
386c056b |
112 | if $meta->can('does_role') && all { $meta->does_role($_) } |
113 | grep { !ref $_ } @{$roles}; |
231be3be |
114 | |
115 | return Moose::Meta::Class->create_anon_class( |
116 | superclasses => $superclasses, |
117 | roles => $roles, |
118 | cache => 1, |
119 | )->name(); |
120 | } |
121 | |
122 | 1; |
c59bc009 |
123 | |
124 | __END__ |
125 | |
126 | =head1 NAME |
127 | |
128 | Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class |
129 | |
130 | =head1 SYNOPSIS |
131 | |
132 | package MyApp::Moose; |
133 | |
c59bc009 |
134 | use Moose (); |
135 | use Moose::Exporter; |
49a86a99 |
136 | use Moose::Util::MetaRole; |
c59bc009 |
137 | |
138 | use MyApp::Role::Meta::Class; |
139 | use MyApp::Role::Meta::Method::Constructor; |
140 | use MyApp::Role::Object; |
141 | |
142 | Moose::Exporter->setup_import_methods( also => 'Moose' ); |
143 | |
144 | sub init_meta { |
145 | shift; |
146 | my %options = @_; |
147 | |
148 | Moose->init_meta(%options); |
149 | |
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'], |
154 | ); |
155 | |
156 | Moose::Util::MetaRole::apply_base_class_roles( |
157 | for_class => $options{for_class}, |
158 | roles => ['MyApp::Role::Object'], |
159 | ); |
160 | |
161 | return $options{for_class}->meta(); |
162 | } |
163 | |
164 | =head1 DESCRIPTION |
165 | |
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 |
52a919fe |
169 | can then be dynamically applied to the caller's metaclasses. |
c59bc009 |
170 | |
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. |
174 | |
175 | =head1 USAGE |
176 | |
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. |
183 | |
184 | The easiest way to ensure that this happens is to use |
95056a1e |
185 | L<Moose::Exporter>, which can generate the appropriate C<init_meta> |
186 | method for you, and make sure it is called when imported. |
c59bc009 |
187 | |
188 | =head1 FUNCTIONS |
189 | |
190 | This module provides two functions. |
191 | |
192 | =head2 apply_metaclass_roles( ... ) |
193 | |
194 | This function will apply roles to one or more metaclasses for the |
195 | specified class. It accepts the following parameters: |
196 | |
197 | =over 4 |
198 | |
199 | =item * for_class => $name |
200 | |
201 | This specifies the class for which to alter the meta classes. |
202 | |
203 | =item * metaclass_roles => \@roles |
204 | |
205 | =item * attribute_metaclass_roles => \@roles |
206 | |
207 | =item * method_metaclass_roles => \@roles |
208 | |
8286fcd6 |
209 | =item * wrapped_method_metaclass_roles => \@roles |
210 | |
c59bc009 |
211 | =item * instance_metaclass_roles => \@roles |
212 | |
213 | =item * constructor_class_roles => \@roles |
214 | |
215 | =item * destructor_class_roles => \@roles |
216 | |
d401dc20 |
217 | =item * application_to_class_class_roles => \@roles |
218 | |
219 | =item * application_to_role_class_roles => \@roles |
220 | |
221 | =item * application_to_instance_class_roles => \@roles |
222 | |
c59bc009 |
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 |
225 | once. |
226 | |
227 | =back |
228 | |
229 | =head2 apply_base_class_roles( for_class => $class, roles => \@roles ) |
230 | |
231 | This function will apply the specified roles to the object's base class. |
232 | |
233 | =head1 AUTHOR |
234 | |
235 | Dave Rolsky E<lt>autarch@urth.orgE<gt> |
236 | |
237 | =head1 COPYRIGHT AND LICENSE |
238 | |
2840a3b2 |
239 | Copyright 2009 by Infinity Interactive, Inc. |
c59bc009 |
240 | |
241 | L<http://www.iinteractive.com> |
242 | |
243 | This library is free software; you can redistribute it and/or modify |
244 | it under the same terms as Perl itself. |
245 | |
246 | =cut |