handles and asserter support
[gitmo/Role-Tiny.git] / lib / Role / Tiny.pm
1 package Role::Tiny;
2
3 use strict;
4 use warnings FATAL => 'all';
5
6 our %INFO;
7 our %APPLIED_TO;
8 our %COMPOSED;
9
10 sub _getglob { no strict 'refs'; \*{$_[0]} }
11
12 sub import {
13   my $target = caller;
14   my $me = $_[0];
15   strictures->import;
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)) {
20     *{_getglob "${target}::${type}"} = sub {
21       require Class::Method::Modifiers;
22       push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
23     };
24   }
25   *{_getglob "${target}::requires"} = sub {
26     push @{$INFO{$target}{requires}||=[]}, @_;
27   };
28   *{_getglob "${target}::with"} = sub {
29     die "Only one role supported at a time by with" if @_ > 1;
30     $me->apply_role_to_package($_[0], $target);
31   };
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
43 sub apply_role_to_package {
44   my ($me, $role, $to) = @_;
45
46   die "This is apply_role_to_package" if ref($to);
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
60   # copy our role list into the target's
61   @{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
62 }
63
64 sub 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
72 sub 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
82   if ($] > 5.010) {
83     require mro;
84   } else {
85     require MRO::Compat;
86   }
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   );
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
109 sub _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}||[];
117   my @mod_base;
118   foreach my $modified (
119     do { my %h; @h{map $_->[1], @$modifiers} = (); keys %h }
120   ) {
121     push @mod_base, "sub ${modified} { shift->next::method(\@_) }";
122   }
123   eval(my $code = join "\n", "package ${base_name};", @mod_base);
124   die "Evaling failed: $@\nTrying to eval:\n${code}" if $@;
125   $me->_install_modifiers($composed_name, $modifiers);
126   $COMPOSED{role}{$composed_name} = 1;
127   return $composed_name;
128 }
129
130 sub _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
142 sub _concrete_methods_of {
143   my ($me, $role) = @_;
144   my $info = $INFO{$role};
145   $info->{methods} ||= do {
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 {
152         my $code = *{$stash->{$_}}{CODE};
153         # rely on the '' key we added in import for "no code here"
154         exists $not_methods->{$code||''} ? () : ($_ => $code)
155       } grep !(ref($stash->{$_}) eq 'SCALAR'), keys %$stash
156     };
157   };
158 }
159
160 sub 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
166 sub _install_methods {
167   my ($me, $to, $role) = @_;
168
169   my $info = $INFO{$role};
170
171   my $methods = $me->_concrete_methods_of($role);
172
173   # grab target symbol table
174   my $stash = do { no strict 'refs'; \%{"${to}::"}};
175
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   } = ();
182
183   foreach my $i (grep !exists $has_methods{$_}, keys %$methods) {
184     no warnings 'once';
185     *{_getglob "${to}::${i}"} = $methods->{$i};
186   }
187 }
188
189 sub _install_modifiers {
190   my ($me, $to, $modifiers) = @_;
191   foreach my $modifier (@{$modifiers||[]}) {
192     Class::Method::Modifiers::install_modifier($to, @{$modifier});
193   }
194 }
195
196 sub does_role {
197   my ($package, $role) = @_;
198   return exists $APPLIED_TO{$package}{$role};
199 }
200
201 1;