bump version to 0.91
[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
113d3174 10our $VERSION = '0.91';
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
115 # make a subtype for each Moose class
116 role_type $role unless find_type_constraint($role);
117
118 # FIXME copy from Moose.pm
119 my $meta;
120 if ($role->can('meta')) {
121 $meta = $role->meta();
70ea9161 122
123 unless ( blessed($meta) && $meta->isa('Moose::Meta::Role') ) {
124 require Moose;
125 Moose->throw_error("You already have a &meta function, but it does not return a Moose::Meta::Role");
126 }
e606ae5f 127 }
128 else {
129 $meta = $metaclass->initialize($role);
130
131 $meta->add_method(
132 'meta' => sub {
133 # re-initialize so it inherits properly
134 $metaclass->initialize( ref($_[0]) || $_[0] );
d31f9614 135 }
e606ae5f 136 );
d31f9614 137 }
e606ae5f 138
139 return $meta;
e185c027 140}
141
1421;
143
144__END__
145
146=pod
147
148=head1 NAME
149
150Moose::Role - The Moose Role
151
76d37e5a 152=head1 SYNOPSIS
153
154 package Eq;
85424612 155 use Moose::Role; # automatically turns on strict and warnings
fb1e11d5 156
e46edf94 157 requires 'equal';
fb1e11d5 158
159 sub no_equal {
76d37e5a 160 my ($self, $other) = @_;
161 !$self->equal($other);
162 }
fb1e11d5 163
76d37e5a 164 # ... then in your classes
fb1e11d5 165
76d37e5a 166 package Currency;
85424612 167 use Moose; # automatically turns on strict and warnings
fb1e11d5 168
76d37e5a 169 with 'Eq';
fb1e11d5 170
76d37e5a 171 sub equal {
172 my ($self, $other) = @_;
bdabd620 173 $self->as_float == $other->as_float;
76d37e5a 174 }
175
e185c027 176=head1 DESCRIPTION
177
46b73973 178The concept of roles is documented in L<Moose::Manual::Roles>. This document
69161799 179serves as API documentation.
76d37e5a 180
2c0cbef7 181=head1 EXPORTED FUNCTIONS
182
85424612 183Moose::Role currently supports all of the functions that L<Moose> exports, but
184differs slightly in how some items are handled (see L<CAVEATS> below for
185details).
76d37e5a 186
85424612 187Moose::Role also offers two role-specific keyword exports:
e185c027 188
189=over 4
190
2c0cbef7 191=item B<requires (@method_names)>
76d37e5a 192
fb1e11d5 193Roles can require that certain methods are implemented by any class which
85424612 194C<does> the role.
9e93dd19 195
92fcea04 196Note that attribute accessors also count as methods for the purposes
197of satisfying the requirements of a role.
198
2c0cbef7 199=item B<excludes (@role_names)>
200
9e93dd19 201Roles can C<exclude> other roles, in effect saying "I can never be combined
fb1e11d5 202with these C<@role_names>". This is a feature which should not be used
85424612 203lightly.
9e93dd19 204
2c0cbef7 205=back
206
d31f9614 207=head2 B<unimport>
208
209Moose::Role offers a way to remove the keywords it exports, through the
210C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
211your code for this to work.
212
e606ae5f 213=head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >>
214
215The C<init_meta> method sets up the metaclass object for the role
216specified by C<for_class>. It also injects a a C<meta> accessor into
217the role so you can get at this object.
218
219The default metaclass is L<Moose::Meta::Role>. You can specify an
220alternate metaclass with the C<metaclass> parameter.
221
c1381000 222=head1 METACLASS
223
224When you use Moose::Role, you can specify which metaclass to use:
225
226 use Moose::Role -metaclass => 'My::Meta::Role';
227
228You can also specify traits which will be applied to your role metaclass:
229
230 use Moose::Role -traits => 'My::Trait';
231
232This is very similar to the attribute traits feature. When you do
233this, your class's C<meta> object will have the specified traits
8a8856de 234applied to it. See L<Moose/Metaclass and Trait Name Resolution> for more
235details.
c1381000 236
b9cb323b 237=head1 APPLYING ROLES
238
239In addition to being applied to a class using the 'with' syntax (see
240L<Moose::Manual::Roles>) and using the L<Moose::Util> 'apply_all_roles'
241method, roles may also be applied to an instance of a class using
242L<Moose::Util> 'apply_all_roles' or the role's metaclass:
243
244 MyApp::Test::SomeRole->meta->apply( $instance );
245
246Doing this creates a new, mutable, anonymous subclass, applies the role to that,
247and reblesses. In a debugger, for example, you will see class names of the
248form C< Class::MOP::Class::__ANON__::SERIAL::6 >, which means that doing a 'ref'
249on your instance may not return what you expect. See L<Moose::Object> for 'DOES'.
250
251Additional params may be added to the new instance by providing 'rebless_params'.
252See L<Moose::Meta::Role::Application::ToInstance>.
253
2c0cbef7 254=head1 CAVEATS
255
85424612 256Role support has only a few caveats:
2c0cbef7 257
258=over 4
76d37e5a 259
76d37e5a 260=item *
261
fb1e11d5 262Roles cannot use the C<extends> keyword; it will throw an exception for now.
263The same is true of the C<augment> and C<inner> keywords (not sure those
264really make sense for roles). All other Moose keywords will be I<deferred>
85424612 265so that they can be applied to the consuming class.
76d37e5a 266
fb1e11d5 267=item *
2c0cbef7 268
85424612 269Role composition does its best to B<not> be order-sensitive when it comes to
270conflict resolution and requirements detection. However, it is order-sensitive
271when it comes to method modifiers. All before/around/after modifiers are
272included whenever a role is composed into a class, and then applied in the order
273in which the roles are used. This also means that there is no conflict for
274before/around/after modifiers.
2c0cbef7 275
85424612 276In most cases, this will be a non-issue; however, it is something to keep in
277mind when using method modifiers in a role. You should never assume any
2c0cbef7 278ordering.
279
e185c027 280=back
281
282=head1 BUGS
283
fb1e11d5 284All complex software has bugs lurking in it, and this module is no
e185c027 285exception. If you find a bug please either email me, or add the bug
286to cpan-RT.
287
288=head1 AUTHOR
289
290Stevan Little E<lt>stevan@iinteractive.comE<gt>
291
db1ab48d 292Christian Hansen E<lt>chansen@cpan.orgE<gt>
98aae381 293
e185c027 294=head1 COPYRIGHT AND LICENSE
295
2840a3b2 296Copyright 2006-2009 by Infinity Interactive, Inc.
e185c027 297
298L<http://www.iinteractive.com>
299
300This library is free software; you can redistribute it and/or modify
fb1e11d5 301it under the same terms as Perl itself.
e185c027 302
68117c45 303=cut