Commit | Line | Data |
e185c027 |
1 | package Moose::Role; |
d1e17c7f |
2 | use strict; |
3 | use warnings; |
e185c027 |
4 | |
e65dccbc |
5 | use Scalar::Util 'blessed'; |
f4f808de |
6 | use Carp 'croak'; |
e185c027 |
7 | |
2d562421 |
8 | use Sub::Exporter; |
9 | |
d874ffad |
10 | our $VERSION = '1.20'; |
e606ae5f |
11 | $VERSION = eval $VERSION; |
d44714be |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
e185c027 |
13 | |
d7d8a8c7 |
14 | use Moose (); |
15 | use Moose::Util (); |
e65dccbc |
16 | |
c36b393c |
17 | use Moose::Exporter; |
e185c027 |
18 | use Moose::Meta::Role; |
7eaef7ad |
19 | use Moose::Util::TypeConstraints; |
e185c027 |
20 | |
e606ae5f |
21 | sub extends { |
887e3bf0 |
22 | croak "Roles do not support 'extends' (you can use 'with' to specialize a role)"; |
e606ae5f |
23 | } |
24 | |
25 | sub with { |
d5447d26 |
26 | Moose::Util::apply_all_roles( shift, @_ ); |
e606ae5f |
27 | } |
28 | |
29 | sub requires { |
d5447d26 |
30 | my $meta = shift; |
e606ae5f |
31 | croak "Must specify at least one method" unless @_; |
32 | $meta->add_required_methods(@_); |
33 | } |
34 | |
35 | sub excludes { |
d5447d26 |
36 | my $meta = shift; |
e606ae5f |
37 | croak "Must specify at least one role" unless @_; |
38 | $meta->add_excluded_roles(@_); |
39 | } |
2d562421 |
40 | |
e606ae5f |
41 | sub has { |
d5447d26 |
42 | my $meta = shift; |
e606ae5f |
43 | my $name = shift; |
44 | croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1; |
833b56a7 |
45 | my %options = ( definition_context => Moose::Util::_caller_info(), @_ ); |
e606ae5f |
46 | my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ]; |
47 | $meta->add_attribute( $_, %options ) for @$attrs; |
48 | } |
2d562421 |
49 | |
2e3c7aa0 |
50 | sub _add_method_modifier { |
51 | my $type = shift; |
d5447d26 |
52 | my $meta = shift; |
d9add270 |
53 | |
54 | if ( ref($_[0]) eq 'Regexp' ) { |
55 | croak "Roles do not currently support regex " |
56 | . " references for $type method modifiers"; |
e606ae5f |
57 | } |
d9add270 |
58 | |
59 | Moose::Util::add_method_modifier($meta, $type, \@_); |
e606ae5f |
60 | } |
2d562421 |
61 | |
2e3c7aa0 |
62 | sub before { _add_method_modifier('before', @_) } |
2d562421 |
63 | |
2e3c7aa0 |
64 | sub after { _add_method_modifier('after', @_) } |
fb1e11d5 |
65 | |
2e3c7aa0 |
66 | sub around { _add_method_modifier('around', @_) } |
fb1e11d5 |
67 | |
e606ae5f |
68 | # see Moose.pm for discussion |
69 | sub super { |
70 | return unless $Moose::SUPER_BODY; |
71 | $Moose::SUPER_BODY->(@Moose::SUPER_ARGS); |
72 | } |
73 | |
74 | sub override { |
d5447d26 |
75 | my $meta = shift; |
e606ae5f |
76 | my ( $name, $code ) = @_; |
77 | $meta->add_override_method_modifier( $name, $code ); |
78 | } |
79 | |
80 | sub inner { |
b8945921 |
81 | croak "Roles cannot support 'inner'"; |
e606ae5f |
82 | } |
83 | |
84 | sub augment { |
b8945921 |
85 | croak "Roles cannot support 'augment'"; |
e606ae5f |
86 | } |
87 | |
c36b393c |
88 | Moose::Exporter->setup_import_methods( |
d5447d26 |
89 | with_meta => [ |
d4783454 |
90 | qw( with requires excludes has before after around override ) |
e606ae5f |
91 | ], |
92 | as_is => [ |
93 | qw( extends super inner augment ), |
94 | \&Carp::confess, |
95 | \&Scalar::Util::blessed, |
96 | ], |
97 | ); |
98 | |
99 | sub init_meta { |
100 | shift; |
101 | my %args = @_; |
102 | |
70ea9161 |
103 | my $role = $args{for_class}; |
104 | |
105 | unless ($role) { |
106 | require Moose; |
107 | Moose->throw_error("Cannot call init_meta without specifying a for_class"); |
108 | } |
e606ae5f |
109 | |
110 | my $metaclass = $args{metaclass} || "Moose::Meta::Role"; |
2937ed18 |
111 | my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta'; |
e606ae5f |
112 | |
ed544690 |
113 | Moose->throw_error("The Metaclass $metaclass must be a subclass of Moose::Meta::Role.") |
114 | unless $metaclass->isa('Moose::Meta::Role'); |
115 | |
116 | # make a subtype for each Moose role |
e606ae5f |
117 | role_type $role unless find_type_constraint($role); |
118 | |
e606ae5f |
119 | my $meta; |
ed544690 |
120 | if ( $meta = Class::MOP::get_metaclass_by_name($role) ) { |
121 | unless ( $meta->isa("Moose::Meta::Role") ) { |
122 | my $error_message = "$role already has a metaclass, but it does not inherit $metaclass ($meta)."; |
123 | if ( $meta->isa('Moose::Meta::Class') ) { |
124 | Moose->throw_error($error_message . ' You cannot make the same thing a role and a class. Remove either Moose or Moose::Role.'); |
125 | } else { |
126 | Moose->throw_error($error_message); |
127 | } |
70ea9161 |
128 | } |
e606ae5f |
129 | } |
130 | else { |
131 | $meta = $metaclass->initialize($role); |
ed544690 |
132 | } |
e606ae5f |
133 | |
2937ed18 |
134 | if (defined $meta_name) { |
ed544690 |
135 | # also check for inherited non moose 'meta' method? |
2937ed18 |
136 | my $existing = $meta->get_method($meta_name); |
d65bfd76 |
137 | if ($existing && !$existing->isa('Class::MOP::Method::Meta')) { |
df100ac2 |
138 | Carp::cluck "Moose::Role is overwriting an existing method named " |
2937ed18 |
139 | . "$meta_name in role $role with a method " |
140 | . "which returns the class's metaclass. If this is " |
141 | . "actually what you want, you should remove the " |
142 | . "existing method, otherwise, you should rename or " |
143 | . "disable this generated method using the " |
144 | . "'-meta_name' option to 'use Moose::Role'."; |
d65bfd76 |
145 | } |
2937ed18 |
146 | $meta->_add_meta_method($meta_name); |
d31f9614 |
147 | } |
e606ae5f |
148 | |
149 | return $meta; |
e185c027 |
150 | } |
151 | |
152 | 1; |
153 | |
154 | __END__ |
155 | |
156 | =pod |
157 | |
158 | =head1 NAME |
159 | |
160 | Moose::Role - The Moose Role |
161 | |
76d37e5a |
162 | =head1 SYNOPSIS |
163 | |
164 | package Eq; |
85424612 |
165 | use Moose::Role; # automatically turns on strict and warnings |
fb1e11d5 |
166 | |
e46edf94 |
167 | requires 'equal'; |
fb1e11d5 |
168 | |
169 | sub no_equal { |
76d37e5a |
170 | my ($self, $other) = @_; |
171 | !$self->equal($other); |
172 | } |
fb1e11d5 |
173 | |
76d37e5a |
174 | # ... then in your classes |
fb1e11d5 |
175 | |
76d37e5a |
176 | package Currency; |
85424612 |
177 | use Moose; # automatically turns on strict and warnings |
fb1e11d5 |
178 | |
76d37e5a |
179 | with 'Eq'; |
fb1e11d5 |
180 | |
76d37e5a |
181 | sub equal { |
182 | my ($self, $other) = @_; |
bdabd620 |
183 | $self->as_float == $other->as_float; |
76d37e5a |
184 | } |
185 | |
e185c027 |
186 | =head1 DESCRIPTION |
187 | |
46b73973 |
188 | The concept of roles is documented in L<Moose::Manual::Roles>. This document |
69161799 |
189 | serves as API documentation. |
76d37e5a |
190 | |
2c0cbef7 |
191 | =head1 EXPORTED FUNCTIONS |
192 | |
85424612 |
193 | Moose::Role currently supports all of the functions that L<Moose> exports, but |
cec39889 |
194 | differs slightly in how some items are handled (see L</CAVEATS> below for |
85424612 |
195 | details). |
76d37e5a |
196 | |
85424612 |
197 | Moose::Role also offers two role-specific keyword exports: |
e185c027 |
198 | |
199 | =over 4 |
200 | |
2c0cbef7 |
201 | =item B<requires (@method_names)> |
76d37e5a |
202 | |
fb1e11d5 |
203 | Roles can require that certain methods are implemented by any class which |
85424612 |
204 | C<does> the role. |
9e93dd19 |
205 | |
92fcea04 |
206 | Note that attribute accessors also count as methods for the purposes |
207 | of satisfying the requirements of a role. |
208 | |
2c0cbef7 |
209 | =item B<excludes (@role_names)> |
210 | |
9e93dd19 |
211 | Roles can C<exclude> other roles, in effect saying "I can never be combined |
fb1e11d5 |
212 | with these C<@role_names>". This is a feature which should not be used |
85424612 |
213 | lightly. |
9e93dd19 |
214 | |
2c0cbef7 |
215 | =back |
216 | |
d31f9614 |
217 | =head2 B<unimport> |
218 | |
219 | Moose::Role offers a way to remove the keywords it exports, through the |
220 | C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of |
221 | your code for this to work. |
222 | |
e606ae5f |
223 | =head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >> |
224 | |
225 | The C<init_meta> method sets up the metaclass object for the role |
226 | specified by C<for_class>. It also injects a a C<meta> accessor into |
227 | the role so you can get at this object. |
228 | |
229 | The default metaclass is L<Moose::Meta::Role>. You can specify an |
230 | alternate metaclass with the C<metaclass> parameter. |
231 | |
c1381000 |
232 | =head1 METACLASS |
233 | |
234 | When you use Moose::Role, you can specify which metaclass to use: |
235 | |
236 | use Moose::Role -metaclass => 'My::Meta::Role'; |
237 | |
238 | You can also specify traits which will be applied to your role metaclass: |
239 | |
240 | use Moose::Role -traits => 'My::Trait'; |
241 | |
242 | This is very similar to the attribute traits feature. When you do |
243 | this, your class's C<meta> object will have the specified traits |
8a8856de |
244 | applied to it. See L<Moose/Metaclass and Trait Name Resolution> for more |
245 | details. |
c1381000 |
246 | |
b9cb323b |
247 | =head1 APPLYING ROLES |
248 | |
249 | In addition to being applied to a class using the 'with' syntax (see |
250 | L<Moose::Manual::Roles>) and using the L<Moose::Util> 'apply_all_roles' |
251 | method, roles may also be applied to an instance of a class using |
252 | L<Moose::Util> 'apply_all_roles' or the role's metaclass: |
253 | |
254 | MyApp::Test::SomeRole->meta->apply( $instance ); |
255 | |
256 | Doing this creates a new, mutable, anonymous subclass, applies the role to that, |
257 | and reblesses. In a debugger, for example, you will see class names of the |
258 | form C< Class::MOP::Class::__ANON__::SERIAL::6 >, which means that doing a 'ref' |
259 | on your instance may not return what you expect. See L<Moose::Object> for 'DOES'. |
260 | |
261 | Additional params may be added to the new instance by providing 'rebless_params'. |
262 | See L<Moose::Meta::Role::Application::ToInstance>. |
263 | |
2c0cbef7 |
264 | =head1 CAVEATS |
265 | |
85424612 |
266 | Role support has only a few caveats: |
2c0cbef7 |
267 | |
268 | =over 4 |
76d37e5a |
269 | |
76d37e5a |
270 | =item * |
271 | |
fb1e11d5 |
272 | Roles cannot use the C<extends> keyword; it will throw an exception for now. |
273 | The same is true of the C<augment> and C<inner> keywords (not sure those |
274 | really make sense for roles). All other Moose keywords will be I<deferred> |
85424612 |
275 | so that they can be applied to the consuming class. |
76d37e5a |
276 | |
fb1e11d5 |
277 | =item * |
2c0cbef7 |
278 | |
85424612 |
279 | Role composition does its best to B<not> be order-sensitive when it comes to |
280 | conflict resolution and requirements detection. However, it is order-sensitive |
281 | when it comes to method modifiers. All before/around/after modifiers are |
282 | included whenever a role is composed into a class, and then applied in the order |
283 | in which the roles are used. This also means that there is no conflict for |
284 | before/around/after modifiers. |
2c0cbef7 |
285 | |
85424612 |
286 | In most cases, this will be a non-issue; however, it is something to keep in |
287 | mind when using method modifiers in a role. You should never assume any |
2c0cbef7 |
288 | ordering. |
289 | |
e185c027 |
290 | =back |
291 | |
292 | =head1 BUGS |
293 | |
d4048ef3 |
294 | See L<Moose/BUGS> for details on reporting bugs. |
e185c027 |
295 | |
296 | =head1 AUTHOR |
297 | |
298 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
299 | |
db1ab48d |
300 | Christian Hansen E<lt>chansen@cpan.orgE<gt> |
98aae381 |
301 | |
e185c027 |
302 | =head1 COPYRIGHT AND LICENSE |
303 | |
7e0492d3 |
304 | Copyright 2006-2010 by Infinity Interactive, Inc. |
e185c027 |
305 | |
306 | L<http://www.iinteractive.com> |
307 | |
308 | This library is free software; you can redistribute it and/or modify |
fb1e11d5 |
309 | it under the same terms as Perl itself. |
e185c027 |
310 | |
68117c45 |
311 | =cut |