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