bump version to 0.71
[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.71';
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             wrapped_method_metaclass
52             instance_metaclass
53     );
54
55     my $new_metaclass
56         = _make_new_class( ref $for->meta(), $options->{metaclass_roles} );
57
58     my $old_meta = $for->meta();
59
60     # This could get called for a Moose::Meta::Role as well as a Moose::Meta::Class
61     my %classes = map {
62         $_ => _make_new_class( $old_meta->$_(), $options->{ $_ . '_roles' } )
63         }
64         grep { $old_meta->can($_) }
65         qw(
66         attribute_metaclass
67         method_metaclass
68         wrapped_method_metaclass
69         instance_metaclass
70     );
71
72     return $new_metaclass->reinitialize( $for, %classes );
73 }
74
75 sub apply_base_class_roles {
76     my %options = @_;
77
78     my $for = $options{for_class};
79
80     my $meta = $for->meta();
81
82     my $new_base = _make_new_class(
83         $for,
84         $options{roles},
85         [ $meta->superclasses() ],
86     );
87
88     $meta->superclasses($new_base)
89         if $new_base ne $meta->name();
90 }
91
92 sub _make_new_class {
93     my $existing_class = shift;
94     my $roles          = shift;
95     my $superclasses   = shift || [$existing_class];
96
97     return $existing_class unless $roles;
98
99     my $meta = Class::MOP::Class->initialize($existing_class);
100
101     return $existing_class
102         if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles};
103
104     return Moose::Meta::Class->create_anon_class(
105         superclasses => $superclasses,
106         roles        => $roles,
107         cache        => 1,
108     )->name();
109 }
110
111 1;
112
113 __END__
114
115 =head1 NAME
116
117 Moose::Util::MetaRole - Apply roles to any metaclass, as well as the object base class
118
119 =head1 SYNOPSIS
120
121   package MyApp::Moose;
122
123   use strict;
124   use warnings;
125
126   use Moose ();
127   use Moose::Exporter;
128   use Moose::Util::Meta::Role;
129
130   use MyApp::Role::Meta::Class;
131   use MyApp::Role::Meta::Method::Constructor;
132   use MyApp::Role::Object;
133
134   Moose::Exporter->setup_import_methods( also => 'Moose' );
135
136   sub init_meta {
137       shift;
138       my %options = @_;
139
140       Moose->init_meta(%options);
141
142       Moose::Util::MetaRole::apply_metaclass_roles(
143           for_class               => $options{for_class},
144           metaclass_roles         => ['MyApp::Role::Meta::Class'],
145           constructor_class_roles => ['MyApp::Role::Meta::Method::Constructor'],
146       );
147
148       Moose::Util::MetaRole::apply_base_class_roles(
149           for_class => $options{for_class},
150           roles     => ['MyApp::Role::Object'],
151       );
152
153       return $options{for_class}->meta();
154   }
155
156 =head1 DESCRIPTION
157
158 B<The whole concept behind this module is still considered
159 experimental, and it could go away in the future!>
160
161 This utility module is designed to help authors of Moose extensions
162 write extensions that are able to cooperate with other Moose
163 extensions. To do this, you must write your extensions as roles, which
164 can then be dynamically applied to the caller's metaclasses.
165
166 This module makes sure to preserve any existing superclasses and roles
167 already set for the meta objects, which means that any number of
168 extensions can apply roles in any order.
169
170 =head1 USAGE
171
172 B<It is very important that you only call this module's functions when
173 your module is imported by the caller>. The process of applying roles
174 to the metaclass reinitializes the metaclass object, which wipes out
175 any existing attributes already defined. However, as long as you do
176 this when your module is imported, the caller should not have any
177 attributes defined yet.
178
179 The easiest way to ensure that this happens is to use
180 L<Moose::Exporter> and provide an C<init_meta> method that will be
181 called when imported.
182
183 =head1 FUNCTIONS
184
185 This module provides two functions.
186
187 =head2 apply_metaclass_roles( ... )
188
189 This function will apply roles to one or more metaclasses for the
190 specified class. It accepts the following parameters:
191
192 =over 4
193
194 =item * for_class => $name
195
196 This specifies the class for which to alter the meta classes.
197
198 =item * metaclass_roles => \@roles
199
200 =item * attribute_metaclass_roles => \@roles
201
202 =item * method_metaclass_roles => \@roles
203
204 =item * wrapped_method_metaclass_roles => \@roles
205
206 =item * instance_metaclass_roles => \@roles
207
208 =item * constructor_class_roles => \@roles
209
210 =item * destructor_class_roles => \@roles
211
212 These parameter all specify one or more roles to be applied to the
213 specified metaclass. You can pass any or all of these parameters at
214 once.
215
216 =back
217
218 =head2 apply_base_class_roles( for_class => $class, roles => \@roles )
219
220 This function will apply the specified roles to the object's base class.
221
222 =head1 AUTHOR
223
224 Dave Rolsky E<lt>autarch@urth.orgE<gt>
225
226 =head1 COPYRIGHT AND LICENSE
227
228 Copyright 2009 by Infinity Interactive, Inc.
229
230 L<http://www.iinteractive.com>
231
232 This library is free software; you can redistribute it and/or modify
233 it under the same terms as Perl itself.
234
235 =cut