bump version to 1.25
[gitmo/Moose.git] / lib / Moose / Role.pm
CommitLineData
e185c027 1package Moose::Role;
d1e17c7f 2use strict;
3use warnings;
e185c027 4
e65dccbc 5use Scalar::Util 'blessed';
f4f808de 6use Carp 'croak';
e185c027 7
2d562421 8use Sub::Exporter;
9
0c3879e8 10our $VERSION = '1.25';
e606ae5f 11$VERSION = eval $VERSION;
d44714be 12our $AUTHORITY = 'cpan:STEVAN';
e185c027 13
d7d8a8c7 14use Moose ();
15use Moose::Util ();
e65dccbc 16
c36b393c 17use Moose::Exporter;
e185c027 18use Moose::Meta::Role;
7eaef7ad 19use Moose::Util::TypeConstraints;
e185c027 20
e606ae5f 21sub extends {
887e3bf0 22 croak "Roles do not support 'extends' (you can use 'with' to specialize a role)";
e606ae5f 23}
24
25sub with {
d5447d26 26 Moose::Util::apply_all_roles( shift, @_ );
e606ae5f 27}
28
29sub requires {
d5447d26 30 my $meta = shift;
e606ae5f 31 croak "Must specify at least one method" unless @_;
32 $meta->add_required_methods(@_);
33}
34
35sub 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 41sub 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 50sub _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 62sub before { _add_method_modifier('before', @_) }
2d562421 63
2e3c7aa0 64sub after { _add_method_modifier('after', @_) }
fb1e11d5 65
2e3c7aa0 66sub around { _add_method_modifier('around', @_) }
fb1e11d5 67
e606ae5f 68# see Moose.pm for discussion
69sub super {
70 return unless $Moose::SUPER_BODY;
71 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
72}
73
74sub override {
d5447d26 75 my $meta = shift;
e606ae5f 76 my ( $name, $code ) = @_;
77 $meta->add_override_method_modifier( $name, $code );
78}
79
80sub inner {
b8945921 81 croak "Roles cannot support 'inner'";
e606ae5f 82}
83
84sub augment {
b8945921 85 croak "Roles cannot support 'augment'";
e606ae5f 86}
87
c36b393c 88Moose::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
99sub 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
1521;
153
154__END__
155
156=pod
157
158=head1 NAME
159
160Moose::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 188The concept of roles is documented in L<Moose::Manual::Roles>. This document
69161799 189serves as API documentation.
76d37e5a 190
2c0cbef7 191=head1 EXPORTED FUNCTIONS
192
85424612 193Moose::Role currently supports all of the functions that L<Moose> exports, but
cec39889 194differs slightly in how some items are handled (see L</CAVEATS> below for
85424612 195details).
76d37e5a 196
85424612 197Moose::Role also offers two role-specific keyword exports:
e185c027 198
199=over 4
200
2c0cbef7 201=item B<requires (@method_names)>
76d37e5a 202
fb1e11d5 203Roles can require that certain methods are implemented by any class which
85424612 204C<does> the role.
9e93dd19 205
92fcea04 206Note that attribute accessors also count as methods for the purposes
207of satisfying the requirements of a role.
208
2c0cbef7 209=item B<excludes (@role_names)>
210
9e93dd19 211Roles can C<exclude> other roles, in effect saying "I can never be combined
fb1e11d5 212with these C<@role_names>". This is a feature which should not be used
85424612 213lightly.
9e93dd19 214
2c0cbef7 215=back
216
d31f9614 217=head2 B<unimport>
218
219Moose::Role offers a way to remove the keywords it exports, through the
220C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
221your code for this to work.
222
e606ae5f 223=head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >>
224
225The C<init_meta> method sets up the metaclass object for the role
226specified by C<for_class>. It also injects a a C<meta> accessor into
227the role so you can get at this object.
228
229The default metaclass is L<Moose::Meta::Role>. You can specify an
230alternate metaclass with the C<metaclass> parameter.
231
c1381000 232=head1 METACLASS
233
234When you use Moose::Role, you can specify which metaclass to use:
235
236 use Moose::Role -metaclass => 'My::Meta::Role';
237
238You can also specify traits which will be applied to your role metaclass:
239
240 use Moose::Role -traits => 'My::Trait';
241
242This is very similar to the attribute traits feature. When you do
243this, your class's C<meta> object will have the specified traits
8a8856de 244applied to it. See L<Moose/Metaclass and Trait Name Resolution> for more
245details.
c1381000 246
b9cb323b 247=head1 APPLYING ROLES
248
249In addition to being applied to a class using the 'with' syntax (see
250L<Moose::Manual::Roles>) and using the L<Moose::Util> 'apply_all_roles'
251method, roles may also be applied to an instance of a class using
252L<Moose::Util> 'apply_all_roles' or the role's metaclass:
253
254 MyApp::Test::SomeRole->meta->apply( $instance );
255
256Doing this creates a new, mutable, anonymous subclass, applies the role to that,
257and reblesses. In a debugger, for example, you will see class names of the
258form C< Class::MOP::Class::__ANON__::SERIAL::6 >, which means that doing a 'ref'
259on your instance may not return what you expect. See L<Moose::Object> for 'DOES'.
260
261Additional params may be added to the new instance by providing 'rebless_params'.
262See L<Moose::Meta::Role::Application::ToInstance>.
263
2c0cbef7 264=head1 CAVEATS
265
85424612 266Role support has only a few caveats:
2c0cbef7 267
268=over 4
76d37e5a 269
76d37e5a 270=item *
271
fb1e11d5 272Roles cannot use the C<extends> keyword; it will throw an exception for now.
273The same is true of the C<augment> and C<inner> keywords (not sure those
274really make sense for roles). All other Moose keywords will be I<deferred>
85424612 275so that they can be applied to the consuming class.
76d37e5a 276
fb1e11d5 277=item *
2c0cbef7 278
85424612 279Role composition does its best to B<not> be order-sensitive when it comes to
280conflict resolution and requirements detection. However, it is order-sensitive
281when it comes to method modifiers. All before/around/after modifiers are
282included whenever a role is composed into a class, and then applied in the order
283in which the roles are used. This also means that there is no conflict for
284before/around/after modifiers.
2c0cbef7 285
85424612 286In most cases, this will be a non-issue; however, it is something to keep in
287mind when using method modifiers in a role. You should never assume any
2c0cbef7 288ordering.
289
e185c027 290=back
291
292=head1 BUGS
293
d4048ef3 294See L<Moose/BUGS> for details on reporting bugs.
e185c027 295
296=head1 AUTHOR
297
298Stevan Little E<lt>stevan@iinteractive.comE<gt>
299
db1ab48d 300Christian Hansen E<lt>chansen@cpan.orgE<gt>
98aae381 301
e185c027 302=head1 COPYRIGHT AND LICENSE
303
7e0492d3 304Copyright 2006-2010 by Infinity Interactive, Inc.
e185c027 305
306L<http://www.iinteractive.com>
307
308This library is free software; you can redistribute it and/or modify
fb1e11d5 309it under the same terms as Perl itself.
e185c027 310
68117c45 311=cut