Commit | Line | Data |
ab3370e7 |
1 | package Role::Tiny; |
2 | |
119014a7 |
3 | sub _getglob { \*{$_[0]} } |
5e03b55c |
4 | sub _getstash { \%{"$_[0]::"} } |
119014a7 |
5 | |
b1eebd55 |
6 | use strict; |
7 | use warnings FATAL => 'all'; |
ab3370e7 |
8 | |
c334a5c1 |
9 | our $VERSION = '1.000000'; # 1.0.0 |
10 | $VERSION = eval $VERSION; |
11 | |
ab3370e7 |
12 | our %INFO; |
13 | our %APPLIED_TO; |
1947330a |
14 | our %COMPOSED; |
ab3370e7 |
15 | |
cf62c989 |
16 | # Module state workaround totally stolen from Zefram's Module::Runtime. |
17 | |
18 | BEGIN { |
19 | *_WORK_AROUND_BROKEN_MODULE_STATE = "$]" < 5.009 ? sub(){1} : sub(){0}; |
20 | } |
21 | |
22 | sub Role::Tiny::__GUARD__::DESTROY { |
23 | delete $INC{$_[0]->[0]} if @{$_[0]}; |
24 | } |
5e03b55c |
25 | |
fb5074f6 |
26 | sub _load_module { |
fb5074f6 |
27 | (my $proto = $_[0]) =~ s/::/\//g; |
cf62c989 |
28 | $proto .= '.pm'; |
29 | return 1 if $INC{$proto}; |
5e03b55c |
30 | # can't just ->can('can') because a sub-package Foo::Bar::Baz |
31 | # creates a 'Baz::' key in Foo::Bar's symbol table |
32 | return 1 if grep !/::$/, keys %{_getstash($_[0])||{}}; |
cf62c989 |
33 | my $guard = _WORK_AROUND_BROKEN_MODULE_STATE |
34 | && bless([ $proto ], 'Role::Tiny::__GUARD__'); |
35 | require $proto; |
36 | pop @$guard if _WORK_AROUND_BROKEN_MODULE_STATE; |
fb5074f6 |
37 | return 1; |
38 | } |
39 | |
ab3370e7 |
40 | sub import { |
41 | my $target = caller; |
a1164a0b |
42 | my $me = shift; |
de3d4906 |
43 | strictures->import; |
1ba11455 |
44 | return if $INFO{$target}; # already exported into this package |
ab3370e7 |
45 | # get symbol table reference |
46 | my $stash = do { no strict 'refs'; \%{"${target}::"} }; |
47 | # install before/after/around subs |
48 | foreach my $type (qw(before after around)) { |
5a247406 |
49 | *{_getglob "${target}::${type}"} = sub { |
7568ba55 |
50 | require Class::Method::Modifiers; |
ab3370e7 |
51 | push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ]; |
52 | }; |
53 | } |
5a247406 |
54 | *{_getglob "${target}::requires"} = sub { |
ab3370e7 |
55 | push @{$INFO{$target}{requires}||=[]}, @_; |
56 | }; |
5a247406 |
57 | *{_getglob "${target}::with"} = sub { |
58 | die "Only one role supported at a time by with" if @_ > 1; |
369a4c50 |
59 | $me->apply_role_to_package($target, $_[0]); |
96d3f07a |
60 | }; |
7b8177f8 |
61 | # grab all *non-constant* (stash slot is not a scalarref) subs present |
ab3370e7 |
62 | # in the symbol table and store their refaddrs (no need to forcibly |
63 | # inflate constant subs into real subs) - also add '' to here (this |
64 | # is used later) |
65 | @{$INFO{$target}{not_methods}={}}{ |
faa9ce11 |
66 | '', map { *$_{CODE}||() } grep !ref($_), values %$stash |
ab3370e7 |
67 | } = (); |
68 | # a role does itself |
69 | $APPLIED_TO{$target} = { $target => undef }; |
70 | } |
71 | |
72 | sub apply_role_to_package { |
369a4c50 |
73 | my ($me, $to, $role) = @_; |
1947330a |
74 | |
fb5074f6 |
75 | _load_module($role); |
76 | |
ab3370e7 |
77 | die "This is apply_role_to_package" if ref($to); |
1947330a |
78 | die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; |
79 | |
80 | $me->_check_requires($to, $role, @{$info->{requires}||[]}); |
81 | |
82 | $me->_install_methods($to, $role); |
83 | |
84 | $me->_install_modifiers($to, $info->{modifiers}); |
85 | |
86 | # only add does() method to classes and only if they don't have one |
87 | if (not $INFO{$to} and not $to->can('does')) { |
88 | *{_getglob "${to}::does"} = \&does_role; |
89 | } |
90 | |
1947330a |
91 | # copy our role list into the target's |
92 | @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = (); |
93 | } |
94 | |
95 | sub apply_roles_to_object { |
96 | my ($me, $object, @roles) = @_; |
97 | die "No roles supplied!" unless @roles; |
98 | my $class = ref($object); |
99 | bless($object, $me->create_class_with_roles($class, @roles)); |
100 | $object; |
101 | } |
102 | |
103 | sub create_class_with_roles { |
104 | my ($me, $superclass, @roles) = @_; |
105 | |
fb5074f6 |
106 | die "No roles supplied!" unless @roles; |
107 | |
c69190f1 |
108 | my $new_name = join( |
109 | '__WITH__', $superclass, my $compose_name = join '__AND__', @roles |
110 | ); |
111 | |
1947330a |
112 | return $new_name if $COMPOSED{class}{$new_name}; |
113 | |
114 | foreach my $role (@roles) { |
fb5074f6 |
115 | _load_module($role); |
1947330a |
116 | die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; |
117 | } |
118 | |
786e5ba0 |
119 | if ($] >= 5.010) { |
7568ba55 |
120 | require mro; |
b1eebd55 |
121 | } else { |
7568ba55 |
122 | require MRO::Compat; |
b1eebd55 |
123 | } |
1947330a |
124 | |
125 | my @composable = map $me->_composable_package_for($_), reverse @roles; |
126 | |
127 | *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ]; |
128 | |
129 | my @info = map +($INFO{$_} ? $INFO{$_} : ()), @roles; |
130 | |
131 | $me->_check_requires( |
132 | $new_name, $compose_name, |
133 | do { my %h; @h{map @{$_->{requires}||[]}, @info} = (); keys %h } |
134 | ); |
1947330a |
135 | |
136 | *{_getglob "${new_name}::does"} = \&does_role unless $new_name->can('does'); |
137 | |
138 | @{$APPLIED_TO{$new_name}||={}}{ |
139 | map keys %{$APPLIED_TO{$_}}, @roles |
140 | } = (); |
141 | |
142 | $COMPOSED{class}{$new_name} = 1; |
143 | return $new_name; |
144 | } |
145 | |
146 | sub _composable_package_for { |
147 | my ($me, $role) = @_; |
148 | my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role; |
149 | return $composed_name if $COMPOSED{role}{$composed_name}; |
150 | $me->_install_methods($composed_name, $role); |
151 | my $base_name = $composed_name.'::_BASE'; |
152 | *{_getglob("${composed_name}::ISA")} = [ $base_name ]; |
153 | my $modifiers = $INFO{$role}{modifiers}||[]; |
b1eebd55 |
154 | my @mod_base; |
1947330a |
155 | foreach my $modified ( |
156 | do { my %h; @h{map $_->[1], @$modifiers} = (); keys %h } |
157 | ) { |
b1eebd55 |
158 | push @mod_base, "sub ${modified} { shift->next::method(\@_) }"; |
1947330a |
159 | } |
59812c87 |
160 | { |
161 | local $@; |
162 | eval(my $code = join "\n", "package ${base_name};", @mod_base); |
163 | die "Evaling failed: $@\nTrying to eval:\n${code}" if $@; |
164 | } |
1947330a |
165 | $me->_install_modifiers($composed_name, $modifiers); |
166 | $COMPOSED{role}{$composed_name} = 1; |
167 | return $composed_name; |
168 | } |
169 | |
170 | sub _check_requires { |
171 | my ($me, $to, $name, @requires) = @_; |
172 | if (my @requires_fail = grep !$to->can($_), @requires) { |
173 | # role -> role, add to requires, role -> class, error out |
174 | if (my $to_info = $INFO{$to}) { |
175 | push @{$to_info->{requires}||=[]}, @requires_fail; |
176 | } else { |
177 | die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail); |
178 | } |
179 | } |
180 | } |
181 | |
4db3a740 |
182 | sub _concrete_methods_of { |
183 | my ($me, $role) = @_; |
1947330a |
184 | my $info = $INFO{$role}; |
4db3a740 |
185 | $info->{methods} ||= do { |
ab3370e7 |
186 | # grab role symbol table |
187 | my $stash = do { no strict 'refs'; \%{"${role}::"}}; |
188 | my $not_methods = $info->{not_methods}; |
189 | +{ |
190 | # grab all code entries that aren't in the not_methods list |
191 | map { |
934ea2c1 |
192 | my $code = *{$stash->{$_}}{CODE}; |
193 | # rely on the '' key we added in import for "no code here" |
194 | exists $not_methods->{$code||''} ? () : ($_ => $code) |
faa9ce11 |
195 | } grep !ref($stash->{$_}), keys %$stash |
ab3370e7 |
196 | }; |
197 | }; |
4db3a740 |
198 | } |
199 | |
200 | sub methods_provided_by { |
201 | my ($me, $role) = @_; |
202 | die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; |
203 | (keys %{$me->_concrete_methods_of($role)}, @{$info->{requires}||[]}); |
204 | } |
205 | |
206 | sub _install_methods { |
207 | my ($me, $to, $role) = @_; |
208 | |
209 | my $info = $INFO{$role}; |
210 | |
211 | my $methods = $me->_concrete_methods_of($role); |
1947330a |
212 | |
ab3370e7 |
213 | # grab target symbol table |
214 | my $stash = do { no strict 'refs'; \%{"${to}::"}}; |
1947330a |
215 | |
ab3370e7 |
216 | # determine already extant methods of target |
217 | my %has_methods; |
218 | @has_methods{grep |
faa9ce11 |
219 | +(ref($stash->{$_}) || *{$stash->{$_}}{CODE}), |
ab3370e7 |
220 | keys %$stash |
221 | } = (); |
ab3370e7 |
222 | |
1947330a |
223 | foreach my $i (grep !exists $has_methods{$_}, keys %$methods) { |
ab3370e7 |
224 | no warnings 'once'; |
5a247406 |
225 | *{_getglob "${to}::${i}"} = $methods->{$i}; |
ab3370e7 |
226 | } |
1947330a |
227 | } |
ab3370e7 |
228 | |
1947330a |
229 | sub _install_modifiers { |
230 | my ($me, $to, $modifiers) = @_; |
dccea57d |
231 | if (my $info = $INFO{$to}) { |
232 | push @{$info->{modifiers}}, @{$modifiers||[]}; |
233 | } else { |
234 | foreach my $modifier (@{$modifiers||[]}) { |
235 | $me->_install_single_modifier($to, @$modifier); |
236 | } |
96d3f07a |
237 | } |
ab3370e7 |
238 | } |
239 | |
dccea57d |
240 | sub _install_single_modifier { |
241 | my ($me, @args) = @_; |
242 | Class::Method::Modifiers::install_modifier(@args); |
243 | } |
244 | |
ab3370e7 |
245 | sub does_role { |
390ac406 |
246 | my ($proto, $role) = @_; |
247 | return exists $APPLIED_TO{ref($proto)||$proto}{$role}; |
ab3370e7 |
248 | } |
249 | |
250 | 1; |
5febcf4d |
251 | |
0b6e5fff |
252 | =head1 NAME |
253 | |
254 | Role::Tiny - Roles. Like a nouvelle cusine portion size slice of Moose. |
5febcf4d |
255 | |
256 | =head1 SYNOPSIS |
257 | |
258 | package Some::Role; |
259 | |
260 | use Role::Tiny; |
261 | |
262 | sub foo { ... } |
263 | |
264 | sub bar { ... } |
265 | |
266 | 1; |
267 | |
268 | else where |
269 | |
270 | package Some::Class; |
271 | |
a1164a0b |
272 | use Role::Tiny::With; |
5febcf4d |
273 | |
274 | # bar gets imported, but not foo |
a1164a0b |
275 | with 'Some::Role'; |
5febcf4d |
276 | |
277 | sub foo { ... } |
278 | |
279 | 1; |
280 | |
281 | =head1 DESCRIPTION |
282 | |
283 | C<Role::Tiny> is a minimalist role composition tool. |
284 | |
285 | =head1 ROLE COMPOSITION |
286 | |
287 | Role composition can be thought of as much more clever and meaningful multiple |
288 | inheritance. The basics of this implementation of roles is: |
289 | |
290 | =over 2 |
291 | |
292 | =item * |
293 | |
294 | If a method is already defined on a class, that method will not be composed in |
295 | from the role. |
296 | |
297 | =item * |
298 | |
299 | If a method that the role L</requires> to be implemented is not implemented, |
300 | role application will fail loudly. |
301 | |
0d39f9d3 |
302 | =back |
303 | |
5febcf4d |
304 | Unlike L<Class::C3>, where the B<last> class inherited from "wins," role |
305 | composition is the other way around, where first wins. In a more complete |
306 | system (see L<Moose>) roles are checked to see if they clash. The goal of this |
307 | is to be much simpler, hence disallowing composition of multiple roles at once. |
308 | |
309 | =head1 METHODS |
310 | |
311 | =head2 apply_role_to_package |
312 | |
369a4c50 |
313 | Role::Tiny->apply_role_to_package('Some::Package', 'Some::Role'); |
5febcf4d |
314 | |
a1164a0b |
315 | Composes role with package. See also L<Role::Tiny::With>. |
5febcf4d |
316 | |
317 | =head2 apply_roles_to_object |
318 | |
319 | Role::Tiny->apply_roles_to_object($foo, qw(Some::Role1 Some::Role2)); |
320 | |
321 | Composes roles in order into object directly. Object is reblessed into the |
322 | resulting class. |
323 | |
324 | =head2 create_class_with_roles |
325 | |
326 | Role::Tiny->create_class_with_roles('Some::Base', qw(Some::Role1 Some::Role2)); |
327 | |
328 | Creates a new class based on base, with the roles composed into it in order. |
329 | New class is returned. |
330 | |
54e4000d |
331 | =head1 SUBROUTINES |
5febcf4d |
332 | |
333 | =head2 does_role |
334 | |
54e4000d |
335 | if (Role::Tiny::does_role($foo, 'Some::Role')) { |
5febcf4d |
336 | ... |
337 | } |
338 | |
339 | Returns true if class has been composed with role. |
340 | |
54e4000d |
341 | This subroutine is also installed as ->does on any class a Role::Tiny is |
342 | composed into unless that class already has an ->does method, so |
343 | |
344 | if ($foo->does_role('Some::Role')) { |
345 | ... |
346 | } |
347 | |
348 | will work for classes but to test a role, one must use ::does_role directly |
349 | |
5febcf4d |
350 | =head1 IMPORTED SUBROUTINES |
351 | |
352 | =head2 requires |
353 | |
354 | requires qw(foo bar); |
355 | |
356 | Declares a list of methods that must be defined to compose role. |
357 | |
358 | =head2 with |
359 | |
360 | with 'Some::Role1'; |
361 | with 'Some::Role2'; |
362 | |
363 | Composes another role into the current role. Only one role may be composed in |
364 | at a time to allow the code to remain as simple as possible. |
365 | |
366 | =head2 before |
367 | |
368 | before foo => sub { ... }; |
369 | |
370 | See L<< Class::Method::Modifiers/before method(s) => sub { ... } >> for full |
371 | documentation. |
372 | |
373 | =head2 around |
374 | |
375 | around foo => sub { ... }; |
376 | |
377 | See L<< Class::Method::Modifiers/around method(s) => sub { ... } >> for full |
378 | documentation. |
379 | |
380 | =head2 after |
381 | |
382 | after foo => sub { ... }; |
383 | |
384 | See L<< Class::Method::Modifiers/after method(s) => sub { ... } >> for full |
385 | documentation. |
386 | |
c334a5c1 |
387 | =head1 AUTHOR |
388 | |
389 | mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk> |
390 | |
391 | =head1 CONTRIBUTORS |
392 | |
393 | dg - David Leadbeater (cpan:DGL) <dgl@dgl.cx> |
394 | |
395 | frew - Arthur Axel "fREW" Schmidt (cpan:FREW) <frioux@gmail.com> |
396 | |
397 | hobbs - Andrew Rodland (cpan:ARODLAND) <arodland@cpan.org> |
398 | |
399 | jnap - John Napiorkowski (cpan:JJNAPIORK) <jjn1056@yahoo.com> |
400 | |
401 | ribasushi - Peter Rabbitson (cpan:RIBASUSHI) <ribasushi@cpan.org> |
402 | |
403 | chip - Chip Salzenberg (cpan:CHIPS) <chip@pobox.com> |
404 | |
405 | ajgb - Alex J. G. BurzyĆski (cpan:AJGB) <ajgb@cpan.org> |
406 | |
407 | doy - Jesse Luehrs (cpan:DOY) <doy at tozt dot net> |
408 | |
409 | perigrin - Chris Prather (cpan:PERIGRIN) <chris@prather.org> |
410 | |
411 | =head1 COPYRIGHT |
40f3e3aa |
412 | |
c334a5c1 |
413 | Copyright (c) 2010-2012 the Role::Tiny L</AUTHOR> and L</CONTRIBUTORS> |
414 | as listed above. |
40f3e3aa |
415 | |
c334a5c1 |
416 | =head1 LICENSE |
40f3e3aa |
417 | |
c334a5c1 |
418 | This library is free software and may be distributed under the same terms |
419 | as perl itself. |
40f3e3aa |
420 | |
421 | =cut |