bump version to 1.19
[gitmo/Moose.git] / lib / Moose / Role.pm
1 package Moose::Role;
2 use strict;
3 use warnings;
4
5 use Scalar::Util 'blessed';
6 use Carp         'croak';
7
8 use Sub::Exporter;
9
10 our $VERSION   = '1.19';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
13
14 use Moose       ();
15 use Moose::Util ();
16
17 use Moose::Exporter;
18 use Moose::Meta::Role;
19 use Moose::Util::TypeConstraints;
20
21 sub extends {
22     croak "Roles do not support 'extends' (you can use 'with' to specialize a role)";
23 }
24
25 sub with {
26     Moose::Util::apply_all_roles( shift, @_ );
27 }
28
29 sub requires {
30     my $meta = shift;
31     croak "Must specify at least one method" unless @_;
32     $meta->add_required_methods(@_);
33 }
34
35 sub excludes {
36     my $meta = shift;
37     croak "Must specify at least one role" unless @_;
38     $meta->add_excluded_roles(@_);
39 }
40
41 sub has {
42     my $meta = shift;
43     my $name = shift;
44     croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
45     my %options = ( definition_context => Moose::Util::_caller_info(), @_ );
46     my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
47     $meta->add_attribute( $_, %options ) for @$attrs;
48 }
49
50 sub _add_method_modifier {
51     my $type = shift;
52     my $meta = shift;
53
54     if ( ref($_[0]) eq 'Regexp' ) {
55         croak "Roles do not currently support regex "
56             . " references for $type method modifiers";
57     }
58
59     Moose::Util::add_method_modifier($meta, $type, \@_);
60 }
61
62 sub before { _add_method_modifier('before', @_) }
63
64 sub after  { _add_method_modifier('after',  @_) }
65
66 sub around { _add_method_modifier('around', @_) }
67
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 {
75     my $meta = shift;
76     my ( $name, $code ) = @_;
77     $meta->add_override_method_modifier( $name, $code );
78 }
79
80 sub inner {
81     croak "Roles cannot support 'inner'";
82 }
83
84 sub augment {
85     croak "Roles cannot support 'augment'";
86 }
87
88 Moose::Exporter->setup_import_methods(
89     with_meta => [
90         qw( with requires excludes has before after around override )
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
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     }
109
110     my $metaclass = $args{metaclass} || "Moose::Meta::Role";
111     my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta';
112
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
117     role_type $role unless find_type_constraint($role);
118
119     my $meta;
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             }
128         }
129     }
130     else {
131         $meta = $metaclass->initialize($role);
132     }
133
134     if (defined $meta_name) {
135         # also check for inherited non moose 'meta' method?
136         my $existing = $meta->get_method($meta_name);
137         if ($existing && !$existing->isa('Class::MOP::Method::Meta')) {
138             Carp::cluck "Moose::Role is overwriting an existing method named "
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'.";
145         }
146         $meta->_add_meta_method($meta_name);
147     }
148
149     return $meta;
150 }
151
152 1;
153
154 __END__
155
156 =pod
157
158 =head1 NAME
159
160 Moose::Role - The Moose Role
161
162 =head1 SYNOPSIS
163
164   package Eq;
165   use Moose::Role; # automatically turns on strict and warnings
166
167   requires 'equal';
168
169   sub no_equal {
170       my ($self, $other) = @_;
171       !$self->equal($other);
172   }
173
174   # ... then in your classes
175
176   package Currency;
177   use Moose; # automatically turns on strict and warnings
178
179   with 'Eq';
180
181   sub equal {
182       my ($self, $other) = @_;
183       $self->as_float == $other->as_float;
184   }
185
186 =head1 DESCRIPTION
187
188 The concept of roles is documented in L<Moose::Manual::Roles>. This document
189 serves as API documentation.
190
191 =head1 EXPORTED FUNCTIONS
192
193 Moose::Role currently supports all of the functions that L<Moose> exports, but
194 differs slightly in how some items are handled (see L</CAVEATS> below for
195 details).
196
197 Moose::Role also offers two role-specific keyword exports:
198
199 =over 4
200
201 =item B<requires (@method_names)>
202
203 Roles can require that certain methods are implemented by any class which
204 C<does> the role.
205
206 Note that attribute accessors also count as methods for the purposes
207 of satisfying the requirements of a role.
208
209 =item B<excludes (@role_names)>
210
211 Roles can C<exclude> other roles, in effect saying "I can never be combined
212 with these C<@role_names>". This is a feature which should not be used
213 lightly.
214
215 =back
216
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
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
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
244 applied to it. See L<Moose/Metaclass and Trait Name Resolution> for more
245 details.
246
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
264 =head1 CAVEATS
265
266 Role support has only a few caveats:
267
268 =over 4
269
270 =item *
271
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>
275 so that they can be applied to the consuming class.
276
277 =item *
278
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.
285
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
288 ordering.
289
290 =back
291
292 =head1 BUGS
293
294 See L<Moose/BUGS> for details on reporting bugs.
295
296 =head1 AUTHOR
297
298 Stevan Little E<lt>stevan@iinteractive.comE<gt>
299
300 Christian Hansen E<lt>chansen@cpan.orgE<gt>
301
302 =head1 COPYRIGHT AND LICENSE
303
304 Copyright 2006-2010 by Infinity Interactive, Inc.
305
306 L<http://www.iinteractive.com>
307
308 This library is free software; you can redistribute it and/or modify
309 it under the same terms as Perl itself.
310
311 =cut