use cluck for this warning
[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
b6cca0d5 10our $VERSION = '1.14';
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;
e606ae5f 53 my $code = pop @_;
fb1e11d5 54
e606ae5f 55 for (@_) {
b8945921 56 croak "Roles do not currently support "
e606ae5f 57 . ref($_)
2e3c7aa0 58 . " references for $type method modifiers"
e606ae5f 59 if ref $_;
2e3c7aa0 60 my $add_method = "add_${type}_method_modifier";
61 $meta->$add_method( $_, $code );
e606ae5f 62 }
63}
2d562421 64
2e3c7aa0 65sub before { _add_method_modifier('before', @_) }
2d562421 66
2e3c7aa0 67sub after { _add_method_modifier('after', @_) }
fb1e11d5 68
2e3c7aa0 69sub around { _add_method_modifier('around', @_) }
fb1e11d5 70
e606ae5f 71# see Moose.pm for discussion
72sub super {
73 return unless $Moose::SUPER_BODY;
74 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
75}
76
77sub override {
d5447d26 78 my $meta = shift;
e606ae5f 79 my ( $name, $code ) = @_;
80 $meta->add_override_method_modifier( $name, $code );
81}
82
83sub inner {
b8945921 84 croak "Roles cannot support 'inner'";
e606ae5f 85}
86
87sub augment {
b8945921 88 croak "Roles cannot support 'augment'";
e606ae5f 89}
90
c36b393c 91Moose::Exporter->setup_import_methods(
d5447d26 92 with_meta => [
d4783454 93 qw( with requires excludes has before after around override )
e606ae5f 94 ],
95 as_is => [
96 qw( extends super inner augment ),
97 \&Carp::confess,
98 \&Scalar::Util::blessed,
99 ],
100);
101
102sub init_meta {
103 shift;
104 my %args = @_;
105
70ea9161 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 }
e606ae5f 112
113 my $metaclass = $args{metaclass} || "Moose::Meta::Role";
114
ed544690 115 Moose->throw_error("The Metaclass $metaclass must be a subclass of Moose::Meta::Role.")
116 unless $metaclass->isa('Moose::Meta::Role');
117
118 # make a subtype for each Moose role
e606ae5f 119 role_type $role unless find_type_constraint($role);
120
e606ae5f 121 my $meta;
ed544690 122 if ( $meta = Class::MOP::get_metaclass_by_name($role) ) {
123 unless ( $meta->isa("Moose::Meta::Role") ) {
124 my $error_message = "$role already has a metaclass, but it does not inherit $metaclass ($meta).";
125 if ( $meta->isa('Moose::Meta::Class') ) {
126 Moose->throw_error($error_message . ' You cannot make the same thing a role and a class. Remove either Moose or Moose::Role.');
127 } else {
128 Moose->throw_error($error_message);
129 }
70ea9161 130 }
e606ae5f 131 }
132 else {
133 $meta = $metaclass->initialize($role);
ed544690 134 }
e606ae5f 135
d65bfd76 136 unless ($args{no_meta}) {
ed544690 137 # also check for inherited non moose 'meta' method?
d65bfd76 138 my $existing = $meta->get_method('meta');
139 if ($existing && !$existing->isa('Class::MOP::Method::Meta')) {
df100ac2 140 Carp::cluck "Moose::Role is overwriting an existing method named "
141 . "'meta' with its own version, in role $role. If "
142 . "this is actually what you want, you should remove "
143 . "the existing method, otherwise, you should pass "
144 . "the '-no_meta => 1' option to 'use Moose::Role'.";
d65bfd76 145 }
7f1b08f6 146 $meta->_add_meta_method;
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