our %INFO;
our %APPLIED_TO;
+sub _getglob { no strict 'refs'; \*{$_[0]} }
+
sub import {
my $target = caller;
# get symbol table reference
my $stash = do { no strict 'refs'; \%{"${target}::"} };
# install before/after/around subs
foreach my $type (qw(before after around)) {
- *{(do { no strict 'refs'; \*{"${target}::${type}"}})} = sub {
+ *{_getglob "${target}::${type}"} = sub {
push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
};
}
- *{(do { no strict 'refs'; \*{"${target}::requires"}})} = sub {
+ *{_getglob "${target}::requires"} = sub {
push @{$INFO{$target}{requires}||=[]}, @_;
};
+ *{_getglob "${target}::with"} = sub {
+ die "Only one role supported at a time by with" if @_ > 1;
+ Role::Tiny->apply_role_to_package($_[0], $target);
+ };
# grab all *non-constant* (ref eq 'SCALAR') subs present
# in the symbol table and store their refaddrs (no need to forcibly
# inflate constant subs into real subs) - also add '' to here (this
my @to_install = grep !exists $has_methods{$_}, keys %$methods;
foreach my $i (@to_install) {
no warnings 'once';
- *{(do { no strict 'refs'; \*{"${to}::$i"}})}
- = $methods->{$i};
+ *{_getglob "${to}::${i}"} = $methods->{$i};
}
foreach my $modifier (@{$info->{modifiers}||[]}) {
Class::Method::Modifiers::install_modifier($to, @{$modifier});
}
+ # only add does() method to classes and only if they don't have one
+ if (not $INFO{$to} and not $to->can('does')) {
+ ${_getglob "${to}::does"} = \&does_role;
+ }
+
+
# copy our role list into the target's
@{$APPLIED_TO{$to}||={}}{keys %{$APPLIED_TO{$role}}} = ();
}
is(MyClass->foo, 'role foo class foo', 'method modifier');
is(MyClass->bar, 'role bar', 'method from role');
is(MyClass->baz, 'class baz', 'method from class');
+ok(MyClass->does('MyRole'), 'class does role');
+ok(!MyClass->does('Random'), 'class does not do non-role');
like(try_apply_to('NoMethods'), qr/req1, req2/, 'error for both methods');
like(try_apply_to('OneMethod'), qr/req2/, 'error for one method');