blib/lib/Moose/Meta/Method/Delegation.pm

filter: Statementstmt 98.5% 70 / 71 Branchbran 85.7% 24 / 28 Conditioncond 77.7% 14 / 18 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 94.1% 16 / 17 Podpod 100% 4 / 4 totaltotal 89.2% 133 / 149 CC 21 SCAR 17.1
CC
21
Cov
92%
new
12.2
_generate_inline_method
5.0
_eval_environment
3.0
CRAP 5.5
« blib/lib/metaclass.pm ↑ Summary blib/lib/Moose/Meta/Role/Application/ToRole.pm »
1 package Moose::Meta::Method::Delegation;
2 398 our $VERSION = '2.4001';
3  
4 398 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 398 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 398 ▶use Scalar::Util 'blessed', 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 398 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 398 ▶use parent 'Moose::Meta::Method',
Subroutine BEGIN: called CC=1 SCAR=0.0
11 398 'Class::MOP::Method::Generated';
12  
13 398 ▶use Moose::Util 'throw_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 55 ▶sub new {
Subroutine new: called CC=12 SCAR=25.0
Pod documented
16 55 my $class = shift;
17 55 my %options = @_;
18  
19 ( exists $options{attribute} )
20 55 ▶ || throw_exception( MustSupplyAnAttributeToConstructWith => params => \%options,
Branch: 1 branch
BranchTrueFalseTotal
exists $options{"attribute"} or throw_exception("MustSupplyAnAttributeToConstructWith", "params", \%options, "class", $class) 54 1 55
21 class => $class
22 );
23  
24 ( blessed( $options{attribute} )
25 54 ▶ && $options{attribute}->isa('Moose::Meta::Attribute') )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($options{"attribute"}) and $options{"attribute"}->isa("Moose::Meta::Attribute") or throw_exception("MustSupplyAMooseMetaAttributeInstance", "params", \%options, "class", $class) 53 1 54
Condition66%2/3
builtin::blessed($options{"attribute"}) and $options{"attribute"}->isa("Moose::Meta::Attribute")
!ll&&!rl&&r
1053
MC/DC50%1/2
builtin::blessed($options{"attribute"}) and $options{"attribute"}->isa("Moose::Meta::Attribute")
builtin::blessed($options{"attribute"}) $options{"attribute"}->isa("Moose::Meta::Attribute")
Truth table66%2/3
A and B
A= builtin::blessed($options{"attribute"})B= $options{"attribute"}->isa("Moose::Meta::Attribute")
ABresult
0X0
100
111
26 || throw_exception( MustSupplyAMooseMetaAttributeInstance => params => \%options,
27 class => $class
28 );
29  
30 ( $options{package_name} && $options{name} )
31 53 ▶ || throw_exception( MustSupplyPackageNameAndName => params => \%options,
Branch: 1 branch
BranchTrueFalseTotal
$options{"package_name"} and $options{"name"} or throw_exception("MustSupplyPackageNameAndName", "params", \%options, "class", $class) 52 1 53
Condition66%2/3
$options{"package_name"} and $options{"name"}
!ll&&!rl&&r
1052
MC/DC50%1/2
$options{"package_name"} and $options{"name"}
$options{"package_name"} $options{"name"}
Truth table66%2/3
A and B
A= $options{"package_name"}B= $options{"name"}
ABresult
0X0
100
111
32 class => $class
33 );
34  
35 ( $options{delegate_to_method} && ( !ref $options{delegate_to_method} )
36 52 ▶ || ( 'CODE' eq ref $options{delegate_to_method} ) )
Branch: 1 branch
BranchTrueFalseTotal
$options{"delegate_to_method"} and not ref $options{"delegate_to_method"} or "CODE" eq ref $options{"delegate_to_method"} or throw_exception("MustSupplyADelegateToMethod", "params", \%options, "class", $class) 51 1 52
Condition100%6/6
$options{"delegate_to_method"} and not ref $options{"delegate_to_method"} or "CODE" eq ref $options{"delegate_to_method"}
l!l&&r!l&&!r
5011
$options{"delegate_to_method"} and not ref $options{"delegate_to_method"}
!ll&&!rl&&r
1150
MC/DC33%1/3
$options{"delegate_to_method"} and not ref $options{"delegate_to_method"} or "CODE" eq ref $options{"delegate_to_method"}
$options{"delegate_to_method"} not ref $options{"delegate_to_method"} "CODE" eq ref $options{"delegate_to_method"}
Truth table60%3/5
A and not(B) or C
A= $options{"delegate_to_method"}B= not ref $options{"delegate_to_method"}C= "CODE" eq ref $options{"delegate_to_method"}
ABCresult
11X1
0X11
1011
0X00
1000
37 || throw_exception( MustSupplyADelegateToMethod => params => \%options,
38 class => $class
39 );
40  
41 exists $options{curried_arguments}
42 51 ▶ || ( $options{curried_arguments} = [] );
Branch: 1 branch
BranchTrueFalseTotal
exists $options{"curried_arguments"} or $options{"curried_arguments"} = [] 51 0 51
43  
44 ( $options{curried_arguments} &&
45 51 ▶ ( 'ARRAY' eq ref $options{curried_arguments} ) )
Branch: 1 branch
BranchTrueFalseTotal
$options{"curried_arguments"} and "ARRAY" eq ref $options{"curried_arguments"} or throw_exception("MustSupplyArrayRefAsCurriedArguments", "params", \%options, "class_name", $class) 50 1 51
Condition66%2/3
$options{"curried_arguments"} and "ARRAY" eq ref $options{"curried_arguments"}
!ll&&!rl&&r
0150
MC/DC50%1/2
$options{"curried_arguments"} and "ARRAY" eq ref $options{"curried_arguments"}
$options{"curried_arguments"} "ARRAY" eq ref $options{"curried_arguments"}
Truth table66%2/3
A and B
A= $options{"curried_arguments"}B= "ARRAY" eq ref $options{"curried_arguments"}
ABresult
0X0
100
111
46 || throw_exception( MustSupplyArrayRefAsCurriedArguments => params => \%options,
47 class_name => $class
48 );
49  
50 50 my $self = $class->_new( \%options );
51  
52 50 weaken( $self->{'attribute'} );
53  
54 50 $self->_initialize_body;
55  
56 50 return $self;
57 }
58  
59 50 ▶sub _new {
Subroutine _new: called CC=2 SCAR=7.1
60 50 my $class = shift;
61 50 ▶ my $options = @_ == 1 ? $_[0] : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 50 0 50
62  
63 50 return bless $options, $class;
64 }
65  
66 2166 ▶sub curried_arguments { (shift)->{'curried_arguments'} }
Subroutine curried_arguments: called CC=1 SCAR=0.0
Pod documented
67  
68 157 ▶sub associated_attribute { (shift)->{'attribute'} }
Subroutine associated_attribute: called CC=1 SCAR=0.0
Pod documented
69  
70 1951 ▶sub delegate_to_method { (shift)->{'delegate_to_method'} }
Subroutine delegate_to_method: called CC=1 SCAR=0.0
Pod documented
71  
72 50 ▶sub _initialize_body {
Subroutine _initialize_body: called CC=2 SCAR=7.0
73 50 my $self = shift;
74  
75 50 my $method_to_call = $self->delegate_to_method;
76 50 ▶ return $self->{body} = $method_to_call
Branch: 1 branch
BranchTrueFalseTotal
if ref $method_to_call 0 50 50
77 if ref $method_to_call;
78  
79 # We don't inline because it's faster, we do it because when the method is
80 # inlined, any errors thrown because of the delegated method have a _much_
81 # nicer stack trace, as the trace doesn't include any Moose internals.
82 50 $self->{body} = $self->_generate_inline_method;
83  
84 50 return;
85 }
86  
87 50 ▶sub _generate_inline_method {
Subroutine _generate_inline_method: called CC=5 SCAR=16.1
88 50 my $self = shift;
89  
90 50 my $attr = $self->associated_attribute;
91 50 my $delegate = $self->delegate_to_method;
92  
93 50 my $method_name = B::perlstring( $self->name );
94 50 my $attr_name = B::perlstring( $self->associated_attribute->name );
95  
96 50 my $undefined_attr_throw = $self->_inline_throw_exception(
97 'AttributeValueIsNotDefined',
98 sprintf( <<'EOF', $method_name, $attr_name ) );
99 method => $self->meta->find_method_by_name(%s),
100 instance => $self,
101 attribute => $self->meta->find_attribute_by_name(%s),
102 EOF
103  
104 50 my $not_an_object_throw = $self->_inline_throw_exception(
105 'AttributeValueIsNotAnObject',
106 sprintf( <<'EOF', $method_name, $attr_name ) );
107 method => $self->meta->find_method_by_name(%s),
108 instance => $self,
109 attribute => $self->meta->find_attribute_by_name(%s),
110 given_value => $proxy,
111 EOF
112  
113 50 ▶ my $get_proxy
Branch: 1 branch
BranchTrueFalseTotal
$attr->has_read_method ? : 44 6 50
114 = $attr->has_read_method ? $attr->get_read_method : '$reader';
115  
116 50 ▶ my $args = @{ $self->curried_arguments } ? '@curried, @_' : '@_';
Branch: 1 branch
BranchTrueFalseTotal
@{$self->curried_arguments;} ? : 2 48 50
117 50 my $source = sprintf(
118 <<'EOF', $get_proxy, $undefined_attr_throw, $not_an_object_throw, $delegate, $args );
119 sub {
120 my $self = shift;
121  
122 my $proxy = $self->%s;
123 if ( !defined $proxy ) {
124 %s;
125 }
126 elsif ( ref $proxy && !Scalar::Util::blessed($proxy) ) {
127 %s;
128 }
129 return $proxy->%s( %s );
130 }
131 EOF
132  
133 50 my $description
134 = 'inline delegation in '
135 . $self->package_name . ' for '
136 . $attr->name . '->'
137 . $delegate;
138  
139 50 my $definition = $attr->definition_context;
140 # While all attributes created in the usual way (via Moose's has()) will
141 # define this, there's no guarantee that this must be defined. For
142 # example, when Moo inflates a class to Moose it does not define these (as
143 # of Moo 2.003).
144 $description .= " (attribute declared in $definition->{file} at line $definition->{line})"
145 50 ▶ if defined $definition->{file} && defined $definition->{line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $definition->{"file"} and defined $definition->{"line"} 49 1 50
Condition66%2/3
defined $definition->{"file"} and defined $definition->{"line"}
!ll&&!rl&&r
1049
MC/DC50%1/2
defined $definition->{"file"} and defined $definition->{"line"}
defined $definition->{"file"} defined $definition->{"line"}
Truth table66%2/3
A and B
A= defined $definition->{"file"}B= defined $definition->{"line"}
ABresult
0X0
100
111
146  
147 50 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 50 $self->_compile_code(
149 source => $source,
150 description => $description,
151 );
152 }
153 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 $self->_throw_exception(
155 'CouldNotGenerateInlineAttributeMethod',
156 instance => $self,
157 error => $_,
158 option => 'handles for ' . $attr->name . '->' . $delegate,
159 );
160 50 };
161 }
162  
163 50 ▶sub _eval_environment {
Subroutine _eval_environment: called CC=3 SCAR=11.0
164 50 my $self = shift;
165  
166 50 my %env;
167 50 ▶ if ( @{ $self->curried_arguments } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->curried_arguments;}) 2 48 50
168 2 $env{'@curried'} = $self->curried_arguments;
169 }
170  
171 50 ▶ unless ( $self->associated_attribute->has_read_method ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->associated_attribute->has_read_method) 6 44 50
172 6 $env{'$reader'} = \( $self->_get_delegate_accessor );
173 }
174  
175 50 return \%env;
176 }
177  
178 6 ▶sub _get_delegate_accessor {
Subroutine _get_delegate_accessor: called CC=2 SCAR=7.1
179 6 my $self = shift;
180  
181 6 my $accessor = $self->associated_attribute->get_read_method_ref;
182  
183 # If it's blessed it's a Moose::Meta::Method
184 6 ▶ return blessed $accessor
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($accessor) ? : 6 0 6
185 ? ( $accessor->body )
186 : $accessor;
187 }
188  
189 398 1;
190  
191 # ABSTRACT: A Moose Method metaclass for delegation methods
192  
193 __END__
« blib/lib/metaclass.pm ↑ Summary blib/lib/Moose/Meta/Role/Application/ToRole.pm »