improve the warning for unknown attribute parameters (mjd)
[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';
b5ae7c00 7use Class::Load 'is_class_loaded';
e185c027 8
2d562421 9use Sub::Exporter;
10
d7d8a8c7 11use Moose ();
12use Moose::Util ();
e65dccbc 13
c36b393c 14use Moose::Exporter;
e185c027 15use Moose::Meta::Role;
7eaef7ad 16use Moose::Util::TypeConstraints;
e185c027 17
e606ae5f 18sub extends {
887e3bf0 19 croak "Roles do not support 'extends' (you can use 'with' to specialize a role)";
e606ae5f 20}
21
22sub with {
d5447d26 23 Moose::Util::apply_all_roles( shift, @_ );
e606ae5f 24}
25
26sub requires {
d5447d26 27 my $meta = shift;
e606ae5f 28 croak "Must specify at least one method" unless @_;
29 $meta->add_required_methods(@_);
30}
31
32sub excludes {
d5447d26 33 my $meta = shift;
e606ae5f 34 croak "Must specify at least one role" unless @_;
35 $meta->add_excluded_roles(@_);
36}
2d562421 37
e606ae5f 38sub has {
d5447d26 39 my $meta = shift;
e606ae5f 40 my $name = shift;
41 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
a917d5ae 42 my %context = Moose::Util::_caller_info;
43 $context{context} = 'has declaration';
44 $context{type} = 'role';
45 my %options = ( definition_context => \%context, @_ );
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
3eb29da9 113 Moose->throw_error("The Metaclass $metaclass must be loaded. (Perhaps you forgot to 'use $metaclass'?)")
b5ae7c00 114 unless is_class_loaded($metaclass);
3eb29da9 115
ed544690 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
e606ae5f 120 role_type $role unless find_type_constraint($role);
121
e606ae5f 122 my $meta;
ed544690 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 }
70ea9161 131 }
e606ae5f 132 }
133 else {
134 $meta = $metaclass->initialize($role);
ed544690 135 }
e606ae5f 136
2937ed18 137 if (defined $meta_name) {
ed544690 138 # also check for inherited non moose 'meta' method?
2937ed18 139 my $existing = $meta->get_method($meta_name);
d65bfd76 140 if ($existing && !$existing->isa('Class::MOP::Method::Meta')) {
df100ac2 141 Carp::cluck "Moose::Role is overwriting an existing method named "
2937ed18 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'.";
d65bfd76 148 }
2937ed18 149 $meta->_add_meta_method($meta_name);
d31f9614 150 }
e606ae5f 151
152 return $meta;
e185c027 153}
154
1551;
156
ad46f524 157# ABSTRACT: The Moose Role
158
e185c027 159__END__
160
161=pod
162
76d37e5a 163=head1 SYNOPSIS
164
165 package Eq;
85424612 166 use Moose::Role; # automatically turns on strict and warnings
fb1e11d5 167
e46edf94 168 requires 'equal';
fb1e11d5 169
170 sub no_equal {
76d37e5a 171 my ($self, $other) = @_;
172 !$self->equal($other);
173 }
fb1e11d5 174
76d37e5a 175 # ... then in your classes
fb1e11d5 176
76d37e5a 177 package Currency;
85424612 178 use Moose; # automatically turns on strict and warnings
fb1e11d5 179
76d37e5a 180 with 'Eq';
fb1e11d5 181
76d37e5a 182 sub equal {
183 my ($self, $other) = @_;
bdabd620 184 $self->as_float == $other->as_float;
76d37e5a 185 }
186
36fa7a87 187 # ... and also
188
189 package Comparator;
190 use Moose;
191
192 has compare_to => (
193 is => 'ro',
194 does => 'Eq',
195 handles => 'Eq',
196 );
197
198 # ... which allows
199
200 my $currency1 = Currency->new(...);
201 my $currency2 = Currency->new(...);
202 Comparator->new(compare_to => $currency1)->equal($currency2);
203
e185c027 204=head1 DESCRIPTION
205
46b73973 206The concept of roles is documented in L<Moose::Manual::Roles>. This document
69161799 207serves as API documentation.
76d37e5a 208
2c0cbef7 209=head1 EXPORTED FUNCTIONS
210
85424612 211Moose::Role currently supports all of the functions that L<Moose> exports, but
cec39889 212differs slightly in how some items are handled (see L</CAVEATS> below for
85424612 213details).
76d37e5a 214
85424612 215Moose::Role also offers two role-specific keyword exports:
e185c027 216
217=over 4
218
2c0cbef7 219=item B<requires (@method_names)>
76d37e5a 220
fb1e11d5 221Roles can require that certain methods are implemented by any class which
85424612 222C<does> the role.
9e93dd19 223
92fcea04 224Note that attribute accessors also count as methods for the purposes
225of satisfying the requirements of a role.
226
2c0cbef7 227=item B<excludes (@role_names)>
228
9e93dd19 229Roles can C<exclude> other roles, in effect saying "I can never be combined
fb1e11d5 230with these C<@role_names>". This is a feature which should not be used
85424612 231lightly.
9e93dd19 232
2c0cbef7 233=back
234
d31f9614 235=head2 B<unimport>
236
237Moose::Role offers a way to remove the keywords it exports, through the
238C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
239your code for this to work.
240
c1381000 241=head1 METACLASS
242
0f9d92ef 243When you use Moose::Role, you can specify traits which will be applied to your
244role metaclass:
c1381000 245
246 use Moose::Role -traits => 'My::Trait';
247
248This is very similar to the attribute traits feature. When you do
249this, your class's C<meta> object will have the specified traits
8a8856de 250applied to it. See L<Moose/Metaclass and Trait Name Resolution> for more
251details.
c1381000 252
b9cb323b 253=head1 APPLYING ROLES
254
255In addition to being applied to a class using the 'with' syntax (see
256L<Moose::Manual::Roles>) and using the L<Moose::Util> 'apply_all_roles'
257method, roles may also be applied to an instance of a class using
258L<Moose::Util> 'apply_all_roles' or the role's metaclass:
259
260 MyApp::Test::SomeRole->meta->apply( $instance );
261
262Doing this creates a new, mutable, anonymous subclass, applies the role to that,
263and reblesses. In a debugger, for example, you will see class names of the
0f9d92ef 264form C< Moose::Meta::Class::__ANON__::SERIAL::6 >, which means that doing a
265'ref' on your instance may not return what you expect. See L<Moose::Object> for
266'DOES'.
b9cb323b 267
0f9d92ef 268Additional params may be added to the new instance by providing
269'rebless_params'. See L<Moose::Meta::Role::Application::ToInstance>.
b9cb323b 270
2c0cbef7 271=head1 CAVEATS
272
85424612 273Role support has only a few caveats:
2c0cbef7 274
275=over 4
76d37e5a 276
76d37e5a 277=item *
278
fb1e11d5 279Roles cannot use the C<extends> keyword; it will throw an exception for now.
280The same is true of the C<augment> and C<inner> keywords (not sure those
281really make sense for roles). All other Moose keywords will be I<deferred>
85424612 282so that they can be applied to the consuming class.
76d37e5a 283
fb1e11d5 284=item *
2c0cbef7 285
85424612 286Role composition does its best to B<not> be order-sensitive when it comes to
287conflict resolution and requirements detection. However, it is order-sensitive
288when it comes to method modifiers. All before/around/after modifiers are
289included whenever a role is composed into a class, and then applied in the order
290in which the roles are used. This also means that there is no conflict for
291before/around/after modifiers.
2c0cbef7 292
85424612 293In most cases, this will be a non-issue; however, it is something to keep in
294mind when using method modifiers in a role. You should never assume any
2c0cbef7 295ordering.
296
e185c027 297=back
298
299=head1 BUGS
300
d4048ef3 301See L<Moose/BUGS> for details on reporting bugs.
e185c027 302
68117c45 303=cut