File Coverage

blib/lib/MooX/Role/Parameterized/Proxy.pm
Criterion Covered Total %
statement 40 42 95.2
branch 4 6 66.6
condition n/a
subroutine 12 12 100.0
pod 0 8 0.0
total 56 68 82.3


line stmt bran cond sub pod time code
1             package MooX::Role::Parameterized::Proxy;
2             {
3             $MooX::Role::Parameterized::Proxy::VERSION = '0.082';
4             }
5 9     9   50 use strict;
  9         16  
  9         250  
6 9     9   47 use warnings;
  9         18  
  9         254  
7 9     9   48 use Carp qw(croak);
  9         14  
  9         4588  
8              
9             # ABSTRACT: small proxy to offer mop methods like has, with, requires, etc.
10              
11             =head1 DESCRIPTION
12              
13             L is a proxy to the target class.
14              
15             This proxy offer has, with, before, around, after, requires and method - to avoid inject magic around the L
16              
17             =cut
18              
19             sub new {
20 12     12 0 57 my ( $klass, %args ) = @_;
21              
22 12         81 return bless { target => $args{target}, role => $args{role} }, $klass;
23             }
24              
25             sub has {
26 14     14 0 104 my $self = shift;
27 14         23 goto &{ $self->{target} . '::has' };
  14         135  
28             }
29              
30             sub with {
31 3     3 0 21 my $self = shift;
32 3         6 goto &{ $self->{target} . '::with' };
  3         19  
33             }
34              
35             sub before {
36 1     1 0 14 my $self = shift;
37 1         3 goto &{ $self->{target} . '::before' };
  1         9  
38             }
39              
40             sub around {
41 1     1 0 9 my $self = shift;
42 1         2 goto &{ $self->{target} . '::around' };
  1         6  
43              
44             }
45              
46             sub after {
47 1     1 0 9 my $self = shift;
48 1         1 goto &{ $self->{target} . '::after' };
  1         6  
49             }
50              
51             sub requires {
52 7     7 0 2049 my $self = shift;
53 7         17 my $target = $self->{target};
54 7         14 my $role = $self->{role};
55              
56 7 50       64 if ( $target->can('requires') ) {
57 0         0 goto &{"${target}::requires"};
  0         0  
58             }
59             else {
60 7         16 my $required_method = shift;
61 7 100       107 croak "Can't apply $role to $target - missing $required_method"
62             if !$target->can($required_method);
63             }
64             }
65              
66             sub method {
67 14     14 0 54526 my ( $self, $name, $code ) = @_;
68 14         36 my $target = $self->{target};
69              
70 14 50       140 carp("method ${target}\:\:${name} already exists, overriding...")
71             if $target->can($name);
72              
73 9     9   1323 no strict 'refs';
  9         18  
  9         829  
74 14         28 *{"${target}\:\:${name}"} = $code;
  14         84  
75             }
76              
77             1;