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