Skip some distros with known test issues
[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;
833b56a7 42 my %options = ( definition_context => Moose::Util::_caller_info(), @_ );
e606ae5f 43 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
44 $meta->add_attribute( $_, %options ) for @$attrs;
45}
2d562421 46
2e3c7aa0 47sub _add_method_modifier {
48 my $type = shift;
d5447d26 49 my $meta = shift;
d9add270 50
51 if ( ref($_[0]) eq 'Regexp' ) {
52 croak "Roles do not currently support regex "
53 . " references for $type method modifiers";
e606ae5f 54 }
d9add270 55
56 Moose::Util::add_method_modifier($meta, $type, \@_);
e606ae5f 57}
2d562421 58
2e3c7aa0 59sub before { _add_method_modifier('before', @_) }
2d562421 60
2e3c7aa0 61sub after { _add_method_modifier('after', @_) }
fb1e11d5 62
2e3c7aa0 63sub around { _add_method_modifier('around', @_) }
fb1e11d5 64
e606ae5f 65# see Moose.pm for discussion
66sub super {
67 return unless $Moose::SUPER_BODY;
68 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
69}
70
71sub override {
d5447d26 72 my $meta = shift;
e606ae5f 73 my ( $name, $code ) = @_;
74 $meta->add_override_method_modifier( $name, $code );
75}
76
77sub inner {
b8945921 78 croak "Roles cannot support 'inner'";
e606ae5f 79}
80
81sub augment {
b8945921 82 croak "Roles cannot support 'augment'";
e606ae5f 83}
84
c36b393c 85Moose::Exporter->setup_import_methods(
d5447d26 86 with_meta => [
d4783454 87 qw( with requires excludes has before after around override )
e606ae5f 88 ],
89 as_is => [
90 qw( extends super inner augment ),
91 \&Carp::confess,
92 \&Scalar::Util::blessed,
93 ],
94);
95
96sub init_meta {
97 shift;
98 my %args = @_;
99
70ea9161 100 my $role = $args{for_class};
101
102 unless ($role) {
103 require Moose;
104 Moose->throw_error("Cannot call init_meta without specifying a for_class");
105 }
e606ae5f 106
107 my $metaclass = $args{metaclass} || "Moose::Meta::Role";
2937ed18 108 my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta';
e606ae5f 109
3eb29da9 110 Moose->throw_error("The Metaclass $metaclass must be loaded. (Perhaps you forgot to 'use $metaclass'?)")
b5ae7c00 111 unless is_class_loaded($metaclass);
3eb29da9 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
ad46f524 154# ABSTRACT: The Moose Role
155
e185c027 156__END__
157
158=pod
159
76d37e5a 160=head1 SYNOPSIS
161
162 package Eq;
85424612 163 use Moose::Role; # automatically turns on strict and warnings
fb1e11d5 164
e46edf94 165 requires 'equal';
fb1e11d5 166
167 sub no_equal {
76d37e5a 168 my ($self, $other) = @_;
169 !$self->equal($other);
170 }
fb1e11d5 171
76d37e5a 172 # ... then in your classes
fb1e11d5 173
76d37e5a 174 package Currency;
85424612 175 use Moose; # automatically turns on strict and warnings
fb1e11d5 176
76d37e5a 177 with 'Eq';
fb1e11d5 178
76d37e5a 179 sub equal {
180 my ($self, $other) = @_;
bdabd620 181 $self->as_float == $other->as_float;
76d37e5a 182 }
183
36fa7a87 184 # ... and also
185
186 package Comparator;
187 use Moose;
188
189 has compare_to => (
190 is => 'ro',
191 does => 'Eq',
192 handles => 'Eq',
193 );
194
195 # ... which allows
196
197 my $currency1 = Currency->new(...);
198 my $currency2 = Currency->new(...);
199 Comparator->new(compare_to => $currency1)->equal($currency2);
200
e185c027 201=head1 DESCRIPTION
202
46b73973 203The concept of roles is documented in L<Moose::Manual::Roles>. This document
69161799 204serves as API documentation.
76d37e5a 205
2c0cbef7 206=head1 EXPORTED FUNCTIONS
207
85424612 208Moose::Role currently supports all of the functions that L<Moose> exports, but
cec39889 209differs slightly in how some items are handled (see L</CAVEATS> below for
85424612 210details).
76d37e5a 211
85424612 212Moose::Role also offers two role-specific keyword exports:
e185c027 213
214=over 4
215
2c0cbef7 216=item B<requires (@method_names)>
76d37e5a 217
fb1e11d5 218Roles can require that certain methods are implemented by any class which
85424612 219C<does> the role.
9e93dd19 220
92fcea04 221Note that attribute accessors also count as methods for the purposes
222of satisfying the requirements of a role.
223
2c0cbef7 224=item B<excludes (@role_names)>
225
9e93dd19 226Roles can C<exclude> other roles, in effect saying "I can never be combined
fb1e11d5 227with these C<@role_names>". This is a feature which should not be used
85424612 228lightly.
9e93dd19 229
2c0cbef7 230=back
231
d31f9614 232=head2 B<unimport>
233
234Moose::Role offers a way to remove the keywords it exports, through the
235C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
236your code for this to work.
237
c1381000 238=head1 METACLASS
239
0f9d92ef 240When you use Moose::Role, you can specify traits which will be applied to your
241role metaclass:
c1381000 242
243 use Moose::Role -traits => 'My::Trait';
244
245This is very similar to the attribute traits feature. When you do
246this, your class's C<meta> object will have the specified traits
8a8856de 247applied to it. See L<Moose/Metaclass and Trait Name Resolution> for more
248details.
c1381000 249
b9cb323b 250=head1 APPLYING ROLES
251
252In addition to being applied to a class using the 'with' syntax (see
253L<Moose::Manual::Roles>) and using the L<Moose::Util> 'apply_all_roles'
254method, roles may also be applied to an instance of a class using
255L<Moose::Util> 'apply_all_roles' or the role's metaclass:
256
257 MyApp::Test::SomeRole->meta->apply( $instance );
258
259Doing this creates a new, mutable, anonymous subclass, applies the role to that,
260and reblesses. In a debugger, for example, you will see class names of the
0f9d92ef 261form C< Moose::Meta::Class::__ANON__::SERIAL::6 >, which means that doing a
262'ref' on your instance may not return what you expect. See L<Moose::Object> for
263'DOES'.
b9cb323b 264
0f9d92ef 265Additional params may be added to the new instance by providing
266'rebless_params'. See L<Moose::Meta::Role::Application::ToInstance>.
b9cb323b 267
2c0cbef7 268=head1 CAVEATS
269
85424612 270Role support has only a few caveats:
2c0cbef7 271
272=over 4
76d37e5a 273
76d37e5a 274=item *
275
fb1e11d5 276Roles cannot use the C<extends> keyword; it will throw an exception for now.
277The same is true of the C<augment> and C<inner> keywords (not sure those
278really make sense for roles). All other Moose keywords will be I<deferred>
85424612 279so that they can be applied to the consuming class.
76d37e5a 280
fb1e11d5 281=item *
2c0cbef7 282
85424612 283Role composition does its best to B<not> be order-sensitive when it comes to
284conflict resolution and requirements detection. However, it is order-sensitive
285when it comes to method modifiers. All before/around/after modifiers are
286included whenever a role is composed into a class, and then applied in the order
287in which the roles are used. This also means that there is no conflict for
288before/around/after modifiers.
2c0cbef7 289
85424612 290In most cases, this will be a non-issue; however, it is something to keep in
291mind when using method modifiers in a role. You should never assume any
2c0cbef7 292ordering.
293
e185c027 294=back
295
296=head1 BUGS
297
d4048ef3 298See L<Moose/BUGS> for details on reporting bugs.
e185c027 299
68117c45 300=cut