File Coverage

blib/lib/Scope/With/Inject.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package # hide from PAUSE
2             Scope::With::Inject;
3              
4 3     3   2855 use strict;
  3         5  
  3         111  
5 3     3   14 use warnings;
  3         6  
  3         101  
6              
7 3     3   15 use base qw(mysubs);
  3         3  
  3         3128  
8              
9             use Mouse::Meta::Class;
10              
11             sub import {
12             my ($class, $invocant_class) = @_;
13             my $invocant;
14              
15             if ($invocant_class) {
16             my @methods = Mouse::Meta::Class->initialize($invocant_class)->get_all_methods();
17              
18             for my $method (@methods) {
19             my $method_name = $method->name;
20             my $method_body = $method->body;
21              
22             # XXX: this is fast, but it relies on the class's
23             # methods not being modified at runtime (usually the case)
24              
25             $class->SUPER::import(
26             $method_name,
27             sub (@) { $method_body->($invocant, @_) }
28             );
29             }
30             } else {
31             my $autoload = sub (@) {
32             my ($method_name) = our $AUTOLOAD =~ /::(\w+)$/;
33             return $invocant->$method_name(@_);
34             };
35              
36             $class->SUPER::import(
37             AUTOLOAD => $autoload,
38             );
39             }
40              
41             $class->SUPER::import(
42             set_invocant => sub ($) { $invocant = shift }
43             );
44             }
45              
46             sub unimport {
47             my $class = shift;
48             $class->SUPER::unimport('set_invocant');
49             }
50              
51             1;