deferred constructor construction
[gitmo/Role-Tiny.git] / lib / Role / Tiny.pm
CommitLineData
ab3370e7 1package Role::Tiny;
2
3use strictures 1;
6c74d087 4use Class::Tiny::_Utils;
ab3370e7 5
6our %INFO;
7our %APPLIED_TO;
8
9sub import {
10 my $target = caller;
de3d4906 11 strictures->import;
ab3370e7 12 # get symbol table reference
13 my $stash = do { no strict 'refs'; \%{"${target}::"} };
14 # install before/after/around subs
15 foreach my $type (qw(before after around)) {
5a247406 16 *{_getglob "${target}::${type}"} = sub {
ab3370e7 17 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
18 };
19 }
5a247406 20 *{_getglob "${target}::requires"} = sub {
ab3370e7 21 push @{$INFO{$target}{requires}||=[]}, @_;
22 };
5a247406 23 *{_getglob "${target}::with"} = sub {
24 die "Only one role supported at a time by with" if @_ > 1;
25 Role::Tiny->apply_role_to_package($_[0], $target);
26 };
ab3370e7 27 # grab all *non-constant* (ref eq 'SCALAR') subs present
28 # in the symbol table and store their refaddrs (no need to forcibly
29 # inflate constant subs into real subs) - also add '' to here (this
30 # is used later)
31 @{$INFO{$target}{not_methods}={}}{
32 '', map { *$_{CODE}||() } grep !(ref eq 'SCALAR'), values %$stash
33 } = ();
34 # a role does itself
35 $APPLIED_TO{$target} = { $target => undef };
36}
37
38sub apply_role_to_package {
39 my ($class, $role, $to) = @_;
40 die "This is apply_role_to_package" if ref($to);
41 die "Not a Role::Tiny" unless my $info = $INFO{$role};
42 my $methods = $info->{methods} ||= do {
43 # grab role symbol table
44 my $stash = do { no strict 'refs'; \%{"${role}::"}};
45 my $not_methods = $info->{not_methods};
46 +{
47 # grab all code entries that aren't in the not_methods list
48 map {
934ea2c1 49 my $code = *{$stash->{$_}}{CODE};
50 # rely on the '' key we added in import for "no code here"
51 exists $not_methods->{$code||''} ? () : ($_ => $code)
ab3370e7 52 } grep !(ref($stash->{$_}) eq 'SCALAR'), keys %$stash
53 };
54 };
55 # grab target symbol table
56 my $stash = do { no strict 'refs'; \%{"${to}::"}};
57 # determine already extant methods of target
58 my %has_methods;
59 @has_methods{grep
60 +((ref($stash->{$_}) eq 'SCALAR') || (*{$stash->{$_}}{CODE})),
61 keys %$stash
62 } = ();
63 if (my @requires_fail
934ea2c1 64 = grep !exists $has_methods{$_}, @{$info->{requires}||[]}) {
ab3370e7 65 # role -> role, add to requires, role -> class, error out
66 if (my $to_info = $INFO{$to}) {
67 push @{$to_info->{requires}||=[]}, @requires_fail;
68 } else {
69 die "Can't apply ${role} to ${to} - missing ".join(', ', @requires_fail);
70 }
71 }
72
73 my @to_install = grep !exists $has_methods{$_}, keys %$methods;
74 foreach my $i (@to_install) {
75 no warnings 'once';
5a247406 76 *{_getglob "${to}::${i}"} = $methods->{$i};
ab3370e7 77 }
78
79 foreach my $modifier (@{$info->{modifiers}||[]}) {
6c74d087 80 _install_modifier($to, @{$modifier});
ab3370e7 81 }
82
5a247406 83 # only add does() method to classes and only if they don't have one
84 if (not $INFO{$to} and not $to->can('does')) {
85 ${_getglob "${to}::does"} = \&does_role;
86 }
87
88
ab3370e7 89 # copy our role list into the target's
90 @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
91}
92
93sub does_role {
94 my ($package, $role) = @_;
95 return exists $APPLIED_TO{$package}{$role};
96}
97
981;