mooooooooooose
[gitmo/Moose.git] / lib / Moose / Role.pm
CommitLineData
e185c027 1
2package Moose::Role;
3
4use strict;
5use warnings;
6
7use Scalar::Util ();
8use Carp 'confess';
9use Sub::Name 'subname';
10
2d562421 11use Sub::Exporter;
12
4276ccb4 13our $VERSION = '0.05';
e185c027 14
15use Moose::Meta::Role;
7eaef7ad 16use Moose::Util::TypeConstraints;
e185c027 17
2d562421 18{
19 my ( $CALLER, %METAS );
20
21 sub _find_meta {
7eaef7ad 22 my $role = $CALLER;
2d562421 23
7eaef7ad 24 return $METAS{$role} if exists $METAS{$role};
25
26 # make a subtype for each Moose class
27 subtype $role
28 => as 'Role'
29 => where { $_->does($role) }
30 unless find_type_constraint($role);
2d562421 31
32 my $meta;
7eaef7ad 33 if ($role->can('meta')) {
34 $meta = $role->meta();
2d562421 35 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
36 || confess "Whoops, not møøsey enough";
37 }
38 else {
7eaef7ad 39 $meta = Moose::Meta::Role->new(role_name => $role);
2d562421 40 $meta->_role_meta->add_method('meta' => sub { $meta })
41 }
42
7eaef7ad 43 return $METAS{$role} = $meta;
2d562421 44 }
45
e185c027 46
9d3188da 47 my %exports = (
2d562421 48 extends => sub {
49 my $meta = _find_meta();
50 return subname 'Moose::Role::extends' => sub {
51 confess "Moose::Role does not currently support 'extends'"
52 };
53 },
54 with => sub {
55 my $meta = _find_meta();
68117c45 56 return subname 'Moose::Role::with' => sub (@) {
d05cd563 57 my (@roles) = @_;
68117c45 58 confess "Must specify at least one role" unless @roles;
d05cd563 59 Moose::_load_all_classes(@roles);
60 ($_->can('meta') && $_->meta->isa('Moose::Meta::Role'))
61 || confess "You can only consume roles, $_ is not a Moose role"
62 foreach @roles;
63 if (scalar @roles == 1) {
64 $roles[0]->meta->apply($meta);
65 }
66 else {
67 Moose::Meta::Role->combine(
68 map { $_->meta } @roles
69 )->apply($meta);
70 }
2d562421 71 };
72 },
73 requires => sub {
74 my $meta = _find_meta();
68117c45 75 return subname 'Moose::Role::requires' => sub (@) {
76 confess "Must specify at least one method" unless @_;
2d562421 77 $meta->add_required_methods(@_);
78 };
79 },
d79e62fd 80 excludes => sub {
81 my $meta = _find_meta();
68117c45 82 return subname 'Moose::Role::excludes' => sub (@) {
83 confess "Must specify at least one role" unless @_;
d79e62fd 84 $meta->add_excluded_roles(@_);
85 };
86 },
2d562421 87 has => sub {
88 my $meta = _find_meta();
2c0cbef7 89 return subname 'Moose::Role::has' => sub ($;%) {
2d562421 90 my ($name, %options) = @_;
91 $meta->add_attribute($name, %options)
92 };
93 },
94 before => sub {
95 my $meta = _find_meta();
2c0cbef7 96 return subname 'Moose::Role::before' => sub (@&) {
2d562421 97 my $code = pop @_;
98 $meta->add_before_method_modifier($_, $code) for @_;
99 };
100 },
101 after => sub {
102 my $meta = _find_meta();
2c0cbef7 103 return subname 'Moose::Role::after' => sub (@&) {
2d562421 104 my $code = pop @_;
105 $meta->add_after_method_modifier($_, $code) for @_;
106 };
107 },
108 around => sub {
109 my $meta = _find_meta();
2c0cbef7 110 return subname 'Moose::Role::around' => sub (@&) {
2d562421 111 my $code = pop @_;
112 $meta->add_around_method_modifier($_, $code) for @_;
113 };
114 },
115 super => sub {
116 my $meta = _find_meta();
117 return subname 'Moose::Role::super' => sub {};
118 },
119 override => sub {
120 my $meta = _find_meta();
2c0cbef7 121 return subname 'Moose::Role::override' => sub ($&) {
2d562421 122 my ($name, $code) = @_;
123 $meta->add_override_method_modifier($name, $code);
124 };
125 },
126 inner => sub {
127 my $meta = _find_meta();
128 return subname 'Moose::Role::inner' => sub {
129 confess "Moose::Role does not currently support 'inner'";
130 };
131 },
132 augment => sub {
133 my $meta = _find_meta();
134 return subname 'Moose::Role::augment' => sub {
135 confess "Moose::Role does not currently support 'augment'";
136 };
137 },
138 confess => sub {
139 return \&Carp::confess;
140 },
141 blessed => sub {
142 return \&Scalar::Util::blessed;
143 }
144 );
145
146 my $exporter = Sub::Exporter::build_exporter({
147 exports => \%exports,
148 groups => {
149 default => [':all']
150 }
151 });
152
153 sub import {
154 $CALLER = caller();
c235cd98 155
156 strict->import;
157 warnings->import;
2d562421 158
159 # we should never export to main
160 return if $CALLER eq 'main';
161
162 goto $exporter;
163 };
164
e185c027 165}
166
1671;
168
169__END__
170
171=pod
172
173=head1 NAME
174
175Moose::Role - The Moose Role
176
76d37e5a 177=head1 SYNOPSIS
178
179 package Eq;
180 use strict;
181 use warnings;
182 use Moose::Role;
183
e46edf94 184 requires 'equal';
76d37e5a 185
186 sub no_equal {
187 my ($self, $other) = @_;
188 !$self->equal($other);
189 }
190
191 # ... then in your classes
192
193 package Currency;
194 use strict;
195 use warnings;
196 use Moose;
197
198 with 'Eq';
199
200 sub equal {
201 my ($self, $other) = @_;
bdabd620 202 $self->as_float == $other->as_float;
76d37e5a 203 }
204
e185c027 205=head1 DESCRIPTION
206
2c0cbef7 207Role support in Moose is coming along quite well. It's best documentation
208is still the the test suite, but it is fairly safe to assume Perl 6 style
209behavior, and then either refer to the test suite, or ask questions on
210#moose if something doesn't quite do what you expect. More complete
211documentation is planned and will be included with the next official
212(non-developer) release.
76d37e5a 213
2c0cbef7 214=head1 EXPORTED FUNCTIONS
215
216Currently Moose::Role supports all of the functions that L<Moose> exports,
217but differs slightly in how some items are handled (see L<CAVEATS> below
218for details).
76d37e5a 219
2c0cbef7 220Moose::Role also offers two role specific keyword exports:
e185c027 221
222=over 4
223
2c0cbef7 224=item B<requires (@method_names)>
76d37e5a 225
9e93dd19 226Roles can require that certain methods are implemented by any class which
227C<does> the role.
228
2c0cbef7 229=item B<excludes (@role_names)>
230
9e93dd19 231Roles can C<exclude> other roles, in effect saying "I can never be combined
232with these C<@role_names>". This is a feature which should not be used
233lightly.
234
2c0cbef7 235=back
236
237=head1 CAVEATS
238
239The role support now has only a few caveats. They are as follows:
240
241=over 4
76d37e5a 242
76d37e5a 243=item *
244
245Roles cannot use the C<extends> keyword, it will throw an exception for now.
246The same is true of the C<augment> and C<inner> keywords (not sure those
247really make sense for roles). All other Moose keywords will be I<deferred>
248so that they can be applied to the consuming class.
249
2c0cbef7 250=item *
251
252Role composition does it's best to B<not> be order sensitive when it comes
253to conflict resolution and requirements detection. However, it is order
254sensitive when it comes to method modifiers. All before/around/after modifiers
255are included whenever a role is composed into a class, and then are applied
256in the order the roles are used. This too means that there is no conflict for
257before/around/after modifiers as well.
258
259In most cases, this will be a non issue, however it is something to keep in
260mind when using method modifiers in a role. You should never assume any
261ordering.
262
263=item *
264
265The C<requires> keyword currently only works with actual methods. A method
266modifier (before/around/after and override) will not count as a fufillment
267of the requirement, and neither will an autogenerated accessor for an attribute.
268
269It is likely that the attribute accessors will eventually be allowed to fufill
270those requirements, either that or we will introduce a C<requires_attr> keyword
271of some kind instead. This descision has not yet been finalized.
272
e185c027 273=back
274
275=head1 BUGS
276
277All complex software has bugs lurking in it, and this module is no
278exception. If you find a bug please either email me, or add the bug
279to cpan-RT.
280
281=head1 AUTHOR
282
283Stevan Little E<lt>stevan@iinteractive.comE<gt>
284
db1ab48d 285Christian Hansen E<lt>chansen@cpan.orgE<gt>
98aae381 286
e185c027 287=head1 COPYRIGHT AND LICENSE
288
289Copyright 2006 by Infinity Interactive, Inc.
290
291L<http://www.iinteractive.com>
292
293This library is free software; you can redistribute it and/or modify
294it under the same terms as Perl itself.
295
68117c45 296=cut