handles and asserter support
[gitmo/Role-Tiny.git] / lib / Role / Tiny.pm
CommitLineData
ab3370e7 1package Role::Tiny;
2
b1eebd55 3use strict;
4use warnings FATAL => 'all';
ab3370e7 5
6our %INFO;
7our %APPLIED_TO;
1947330a 8our %COMPOSED;
ab3370e7 9
b1eebd55 10sub _getglob { no strict 'refs'; \*{$_[0]} }
11
ab3370e7 12sub import {
13 my $target = caller;
b1eebd55 14 my $me = $_[0];
de3d4906 15 strictures->import;
ab3370e7 16 # get symbol table reference
17 my $stash = do { no strict 'refs'; \%{"${target}::"} };
18 # install before/after/around subs
19 foreach my $type (qw(before after around)) {
5a247406 20 *{_getglob "${target}::${type}"} = sub {
b1eebd55 21 require Class::Method::Modifiers;
ab3370e7 22 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
23 };
24 }
5a247406 25 *{_getglob "${target}::requires"} = sub {
ab3370e7 26 push @{$INFO{$target}{requires}||=[]}, @_;
27 };
5a247406 28 *{_getglob "${target}::with"} = sub {
29 die "Only one role supported at a time by with" if @_ > 1;
b1eebd55 30 $me->apply_role_to_package($_[0], $target);
96d3f07a 31 };
ab3370e7 32 # grab all *non-constant* (ref eq 'SCALAR') subs present
33 # in the symbol table and store their refaddrs (no need to forcibly
34 # inflate constant subs into real subs) - also add '' to here (this
35 # is used later)
36 @{$INFO{$target}{not_methods}={}}{
37 '', map { *$_{CODE}||() } grep !(ref eq 'SCALAR'), values %$stash
38 } = ();
39 # a role does itself
40 $APPLIED_TO{$target} = { $target => undef };
41}
42
43sub apply_role_to_package {
1947330a 44 my ($me, $role, $to) = @_;
45
ab3370e7 46 die "This is apply_role_to_package" if ref($to);
1947330a 47 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
48
49 $me->_check_requires($to, $role, @{$info->{requires}||[]});
50
51 $me->_install_methods($to, $role);
52
53 $me->_install_modifiers($to, $info->{modifiers});
54
55 # only add does() method to classes and only if they don't have one
56 if (not $INFO{$to} and not $to->can('does')) {
57 *{_getglob "${to}::does"} = \&does_role;
58 }
59
1947330a 60 # copy our role list into the target's
61 @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
62}
63
64sub apply_roles_to_object {
65 my ($me, $object, @roles) = @_;
66 die "No roles supplied!" unless @roles;
67 my $class = ref($object);
68 bless($object, $me->create_class_with_roles($class, @roles));
69 $object;
70}
71
72sub create_class_with_roles {
73 my ($me, $superclass, @roles) = @_;
74
75 my $new_name = join('+', $superclass, my $compose_name = join '+', @roles);
76 return $new_name if $COMPOSED{class}{$new_name};
77
78 foreach my $role (@roles) {
79 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
80 }
81
b1eebd55 82 if ($] > 5.010) {
83 require mro;
84 } else {
85 require MRO::Compat;
86 }
1947330a 87
88 my @composable = map $me->_composable_package_for($_), reverse @roles;
89
90 *{_getglob("${new_name}::ISA")} = [ @composable, $superclass ];
91
92 my @info = map +($INFO{$_} ? $INFO{$_} : ()), @roles;
93
94 $me->_check_requires(
95 $new_name, $compose_name,
96 do { my %h; @h{map @{$_->{requires}||[]}, @info} = (); keys %h }
97 );
1947330a 98
99 *{_getglob "${new_name}::does"} = \&does_role unless $new_name->can('does');
100
101 @{$APPLIED_TO{$new_name}||={}}{
102 map keys %{$APPLIED_TO{$_}}, @roles
103 } = ();
104
105 $COMPOSED{class}{$new_name} = 1;
106 return $new_name;
107}
108
109sub _composable_package_for {
110 my ($me, $role) = @_;
111 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
112 return $composed_name if $COMPOSED{role}{$composed_name};
113 $me->_install_methods($composed_name, $role);
114 my $base_name = $composed_name.'::_BASE';
115 *{_getglob("${composed_name}::ISA")} = [ $base_name ];
116 my $modifiers = $INFO{$role}{modifiers}||[];
b1eebd55 117 my @mod_base;
1947330a 118 foreach my $modified (
119 do { my %h; @h{map $_->[1], @$modifiers} = (); keys %h }
120 ) {
b1eebd55 121 push @mod_base, "sub ${modified} { shift->next::method(\@_) }";
1947330a 122 }
b1eebd55 123 eval(my $code = join "\n", "package ${base_name};", @mod_base);
124 die "Evaling failed: $@\nTrying to eval:\n${code}" if $@;
1947330a 125 $me->_install_modifiers($composed_name, $modifiers);
126 $COMPOSED{role}{$composed_name} = 1;
127 return $composed_name;
128}
129
130sub _check_requires {
131 my ($me, $to, $name, @requires) = @_;
132 if (my @requires_fail = grep !$to->can($_), @requires) {
133 # role -> role, add to requires, role -> class, error out
134 if (my $to_info = $INFO{$to}) {
135 push @{$to_info->{requires}||=[]}, @requires_fail;
136 } else {
137 die "Can't apply ${name} to ${to} - missing ".join(', ', @requires_fail);
138 }
139 }
140}
141
4db3a740 142sub _concrete_methods_of {
143 my ($me, $role) = @_;
1947330a 144 my $info = $INFO{$role};
4db3a740 145 $info->{methods} ||= do {
ab3370e7 146 # grab role symbol table
147 my $stash = do { no strict 'refs'; \%{"${role}::"}};
148 my $not_methods = $info->{not_methods};
149 +{
150 # grab all code entries that aren't in the not_methods list
151 map {
934ea2c1 152 my $code = *{$stash->{$_}}{CODE};
153 # rely on the '' key we added in import for "no code here"
154 exists $not_methods->{$code||''} ? () : ($_ => $code)
ab3370e7 155 } grep !(ref($stash->{$_}) eq 'SCALAR'), keys %$stash
156 };
157 };
4db3a740 158}
159
160sub methods_provided_by {
161 my ($me, $role) = @_;
162 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
163 (keys %{$me->_concrete_methods_of($role)}, @{$info->{requires}||[]});
164}
165
166sub _install_methods {
167 my ($me, $to, $role) = @_;
168
169 my $info = $INFO{$role};
170
171 my $methods = $me->_concrete_methods_of($role);
1947330a 172
ab3370e7 173 # grab target symbol table
174 my $stash = do { no strict 'refs'; \%{"${to}::"}};
1947330a 175
ab3370e7 176 # determine already extant methods of target
177 my %has_methods;
178 @has_methods{grep
179 +((ref($stash->{$_}) eq 'SCALAR') || (*{$stash->{$_}}{CODE})),
180 keys %$stash
181 } = ();
ab3370e7 182
1947330a 183 foreach my $i (grep !exists $has_methods{$_}, keys %$methods) {
ab3370e7 184 no warnings 'once';
5a247406 185 *{_getglob "${to}::${i}"} = $methods->{$i};
ab3370e7 186 }
1947330a 187}
ab3370e7 188
1947330a 189sub _install_modifiers {
190 my ($me, $to, $modifiers) = @_;
191 foreach my $modifier (@{$modifiers||[]}) {
b1eebd55 192 Class::Method::Modifiers::install_modifier($to, @{$modifier});
96d3f07a 193 }
ab3370e7 194}
195
196sub does_role {
197 my ($package, $role) = @_;
198 return exists $APPLIED_TO{$package}{$role};
199}
200
2011;