Commit | Line | Data |
e185c027 |
1 | package Moose::Role; |
d1e17c7f |
2 | use strict; |
3 | use warnings; |
e185c027 |
4 | |
e65dccbc |
5 | use Scalar::Util 'blessed'; |
f4f808de |
6 | use Carp 'croak'; |
e185c027 |
7 | |
2d562421 |
8 | use Sub::Exporter; |
9 | |
5ef36add |
10 | our $VERSION = '0.85'; |
e606ae5f |
11 | $VERSION = eval $VERSION; |
d44714be |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
e185c027 |
13 | |
d7d8a8c7 |
14 | use Moose (); |
15 | use Moose::Util (); |
e65dccbc |
16 | |
c36b393c |
17 | use Moose::Exporter; |
e185c027 |
18 | use Moose::Meta::Role; |
7eaef7ad |
19 | use Moose::Util::TypeConstraints; |
e185c027 |
20 | |
e606ae5f |
21 | sub extends { |
887e3bf0 |
22 | croak "Roles do not support 'extends' (you can use 'with' to specialize a role)"; |
e606ae5f |
23 | } |
24 | |
25 | sub with { |
26 | Moose::Util::apply_all_roles( Moose::Meta::Role->initialize(shift), @_ ); |
27 | } |
28 | |
29 | sub requires { |
30 | my $meta = Moose::Meta::Role->initialize(shift); |
31 | croak "Must specify at least one method" unless @_; |
32 | $meta->add_required_methods(@_); |
33 | } |
34 | |
35 | sub excludes { |
36 | my $meta = Moose::Meta::Role->initialize(shift); |
37 | croak "Must specify at least one role" unless @_; |
38 | $meta->add_excluded_roles(@_); |
39 | } |
2d562421 |
40 | |
e606ae5f |
41 | sub has { |
42 | my $meta = Moose::Meta::Role->initialize(shift); |
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 |
50 | sub _add_method_modifier { |
51 | my $type = shift; |
e606ae5f |
52 | my $meta = Moose::Meta::Role->initialize(shift); |
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 |
65 | sub before { _add_method_modifier('before', @_) } |
2d562421 |
66 | |
2e3c7aa0 |
67 | sub after { _add_method_modifier('after', @_) } |
fb1e11d5 |
68 | |
2e3c7aa0 |
69 | sub around { _add_method_modifier('around', @_) } |
fb1e11d5 |
70 | |
e606ae5f |
71 | # see Moose.pm for discussion |
72 | sub super { |
73 | return unless $Moose::SUPER_BODY; |
74 | $Moose::SUPER_BODY->(@Moose::SUPER_ARGS); |
75 | } |
76 | |
77 | sub override { |
78 | my $meta = Moose::Meta::Role->initialize(shift); |
79 | my ( $name, $code ) = @_; |
80 | $meta->add_override_method_modifier( $name, $code ); |
81 | } |
82 | |
83 | sub inner { |
b8945921 |
84 | croak "Roles cannot support 'inner'"; |
e606ae5f |
85 | } |
86 | |
87 | sub augment { |
b8945921 |
88 | croak "Roles cannot support 'augment'"; |
e606ae5f |
89 | } |
90 | |
c36b393c |
91 | Moose::Exporter->setup_import_methods( |
e606ae5f |
92 | with_caller => [ |
93 | qw( with requires excludes has before after around override make_immutable ) |
94 | ], |
95 | as_is => [ |
96 | qw( extends super inner augment ), |
97 | \&Carp::confess, |
98 | \&Scalar::Util::blessed, |
99 | ], |
100 | ); |
101 | |
102 | sub 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 | |
142 | 1; |
143 | |
144 | __END__ |
145 | |
146 | =pod |
147 | |
148 | =head1 NAME |
149 | |
150 | Moose::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 | |
69161799 |
178 | The concept of roles is documented in L<Moose::Manual::Role>. This document |
179 | serves as API documentation. |
76d37e5a |
180 | |
2c0cbef7 |
181 | =head1 EXPORTED FUNCTIONS |
182 | |
85424612 |
183 | Moose::Role currently supports all of the functions that L<Moose> exports, but |
184 | differs slightly in how some items are handled (see L<CAVEATS> below for |
185 | details). |
76d37e5a |
186 | |
85424612 |
187 | Moose::Role also offers two role-specific keyword exports: |
e185c027 |
188 | |
189 | =over 4 |
190 | |
2c0cbef7 |
191 | =item B<requires (@method_names)> |
76d37e5a |
192 | |
fb1e11d5 |
193 | Roles can require that certain methods are implemented by any class which |
85424612 |
194 | C<does> the role. |
9e93dd19 |
195 | |
2c0cbef7 |
196 | =item B<excludes (@role_names)> |
197 | |
9e93dd19 |
198 | Roles can C<exclude> other roles, in effect saying "I can never be combined |
fb1e11d5 |
199 | with these C<@role_names>". This is a feature which should not be used |
85424612 |
200 | lightly. |
9e93dd19 |
201 | |
2c0cbef7 |
202 | =back |
203 | |
d31f9614 |
204 | =head2 B<unimport> |
205 | |
206 | Moose::Role offers a way to remove the keywords it exports, through the |
207 | C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of |
208 | your code for this to work. |
209 | |
e606ae5f |
210 | =head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >> |
211 | |
212 | The C<init_meta> method sets up the metaclass object for the role |
213 | specified by C<for_class>. It also injects a a C<meta> accessor into |
214 | the role so you can get at this object. |
215 | |
216 | The default metaclass is L<Moose::Meta::Role>. You can specify an |
217 | alternate metaclass with the C<metaclass> parameter. |
218 | |
c1381000 |
219 | =head1 METACLASS |
220 | |
221 | When you use Moose::Role, you can specify which metaclass to use: |
222 | |
223 | use Moose::Role -metaclass => 'My::Meta::Role'; |
224 | |
225 | You can also specify traits which will be applied to your role metaclass: |
226 | |
227 | use Moose::Role -traits => 'My::Trait'; |
228 | |
229 | This is very similar to the attribute traits feature. When you do |
230 | this, your class's C<meta> object will have the specified traits |
231 | applied to it. See L<Moose/TRAIT NAME RESOLUTION> for more details. |
232 | |
2c0cbef7 |
233 | =head1 CAVEATS |
234 | |
85424612 |
235 | Role support has only a few caveats: |
2c0cbef7 |
236 | |
237 | =over 4 |
76d37e5a |
238 | |
76d37e5a |
239 | =item * |
240 | |
fb1e11d5 |
241 | Roles cannot use the C<extends> keyword; it will throw an exception for now. |
242 | The same is true of the C<augment> and C<inner> keywords (not sure those |
243 | really make sense for roles). All other Moose keywords will be I<deferred> |
85424612 |
244 | so that they can be applied to the consuming class. |
76d37e5a |
245 | |
fb1e11d5 |
246 | =item * |
2c0cbef7 |
247 | |
85424612 |
248 | Role composition does its best to B<not> be order-sensitive when it comes to |
249 | conflict resolution and requirements detection. However, it is order-sensitive |
250 | when it comes to method modifiers. All before/around/after modifiers are |
251 | included whenever a role is composed into a class, and then applied in the order |
252 | in which the roles are used. This also means that there is no conflict for |
253 | before/around/after modifiers. |
2c0cbef7 |
254 | |
85424612 |
255 | In most cases, this will be a non-issue; however, it is something to keep in |
256 | mind when using method modifiers in a role. You should never assume any |
2c0cbef7 |
257 | ordering. |
258 | |
e185c027 |
259 | =back |
260 | |
261 | =head1 BUGS |
262 | |
fb1e11d5 |
263 | All complex software has bugs lurking in it, and this module is no |
e185c027 |
264 | exception. If you find a bug please either email me, or add the bug |
265 | to cpan-RT. |
266 | |
267 | =head1 AUTHOR |
268 | |
269 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
270 | |
db1ab48d |
271 | Christian Hansen E<lt>chansen@cpan.orgE<gt> |
98aae381 |
272 | |
e185c027 |
273 | =head1 COPYRIGHT AND LICENSE |
274 | |
2840a3b2 |
275 | Copyright 2006-2009 by Infinity Interactive, Inc. |
e185c027 |
276 | |
277 | L<http://www.iinteractive.com> |
278 | |
279 | This library is free software; you can redistribute it and/or modify |
fb1e11d5 |
280 | it under the same terms as Perl itself. |
e185c027 |
281 | |
68117c45 |
282 | =cut |