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