blib/lib/Simple/Factory.pm

filter: Statementstmt 99.3% 163 / 164 Branchbran 97.2% 35 / 36 Conditioncond 86.6% 13 / 15 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 97.2% 35 / 36 Podpod 60.0% 3 / 5 totaltotal 96.6% 256 / 265 CC 25 SCAR 9.4
CC
25
Cov
98%
BUILDARGS
6.0
__ANON__
4.0
_build_object_from_args
4.0
CRAP 2.6
↑ Summary
1 package Simple::Factory;
2 {
3 6 $Simple::Factory::VERSION = '0.09';
4 }
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 #ABSTRACT: simple factory
9  
10 6 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use MooX::HandlesVia;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use MooX::Types::MooseLike::Base qw(HasMethods HashRef Any Bool CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 has build_class => (
22 is => 'ro',
23 required => 1,
24 30 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 30 my ($class) = @_;
26 30 use_module($class);
27 }
28 6 );
29  
30 6 has build_conf => (
31 is => 'ro',
32 isa => HashRef [Any],
33 required => 1,
34 handles_via => 'Hash',
35 handles => {
36 has_build_conf_for => 'exists',
37 get_build_conf_for => 'get',
38 get_supported_keys => 'keys',
39 _add_build_conf_for => 'set',
40 }
41 );
42  
43 6 has fallback => ( is => 'ro', predicate => 1 );
44 27 ▶has build_method => ( is => 'ro', default => sub { "new" } );
Subroutine __ANON__: called CC=1 SCAR=0.0
45 27 ▶has autoderef => ( is => 'ro', isa => Bool, default => sub { 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
46 29 ▶has silence => ( is => 'ro', isa => Bool, default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
47 6 has cache =>
48 ( is => 'ro', isa => HasMethods [qw(get set remove)], predicate => 1 );
49  
50 29 ▶has inline => ( is => 'ro', isa => Bool, default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
51 28 ▶has eager => ( is => 'ro', isa => Bool, default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
52 has on_error => (
53 is => 'ro',
54 isa => CodeRef,
55 22 ▶ default => sub { "croak" },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 30 ▶ coerce => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
57 30 my ($on_error) = @_;
58  
59 30 ▶ return $on_error if ref($on_error) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $on_error eq "CODE" 1 29 30
60  
61 29 given ($on_error) {
62 29 when ("croak") {
63 3 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
64 3 my $key = $_[0]->{key};
65 croak "cant resolve instance for key '$key': "
66 3 . $_[0]->{exception};
67 }
68 23 }
69 6 when ("confess") {
70 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
71 1 my $key = $_[0]->{key};
72 confess "cant resolve instance for key '$key': "
73 1 . $_[0]->{exception};
74 }
75 1 }
76 5 when ("carp") {
77 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 my $key = $_[0]->{key};
79 carp "cant resolve instance for key '$key': "
80 1 . $_[0]->{exception};
81 1 return;
82 }
83 1 }
84 4 when ("fallback") {
85 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 return $_[0]->{factory}
87 0 ->get_fallback_for_key( $_[0]->{key} );
88 }
89 2 }
90 2 when ("undef") {
91 1 ▶ return sub { undef }
Subroutine __ANON__: called CC=1 SCAR=0.0
92 1 }
93 1 default {
94 1 croak
95 "can't coerce on_error '$on_error', please use: carp, confess, croak, fallback or undef";
96 }
97 }
98 }
99 6 );
100  
101 32 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=6 SCAR=17.9
Pod undocumented
102 32 my ( $self, @args ) = @_;
103  
104 32 ▶ if ( scalar(@args) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args == 1) 1 31 32
105 1 unshift @args, "build_class";
106 }
107 32 my (%hash_args) = @args;
108  
109 32 ▶ if ( scalar(@args) >= 2
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args >= 2 and not exists $hash_args{"build_class"} and not exists $hash_args{"build_conf"}) 25 7 32
Condition83%5/6
scalar @args >= 2 and not exists $hash_args{"build_class"} and not exists $hash_args{"build_conf"}
!ll&&!rl&&r
7025
scalar @args >= 2 and not exists $hash_args{"build_class"}
!ll&&!rl&&r
1625
MC/DC66%2/3
scalar @args >= 2 and not exists $hash_args{"build_class"} and not exists $hash_args{"build_conf"}
scalar @args >= 2 not exists $hash_args{"build_class"} not exists $hash_args{"build_conf"}
Truth table75%3/4
A and not(B) and not(C)
A= scalar @args >= 2B= not exists $hash_args{"build_class"}C= not exists $hash_args{"build_conf"}
ABCresult
0XX0
10X0
1100
1111
110 && !exists $hash_args{build_class}
111 && !exists $hash_args{build_conf} )
112 {
113 25 my $build_class = $args[0];
114 25 my $build_conf = $args[1];
115  
116 25 $hash_args{build_class} = $build_class;
117 25 $hash_args{build_conf} = $build_conf;
118  
119 25 ▶ if ( $hash_args{inline} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_args{"inline"}) 1 24 25
120 $hash_args{build_conf} =
121 1 { map { $_ => { $_ => $build_conf->{$_} } } keys %{$build_conf},
122 };
123 }
124 }
125  
126 32 return \%hash_args;
127 }
128  
129 29 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
130 29 my ($self) = @_;
131  
132 29 $self->_coerce_build_method;
133  
134 28 ▶ if ( $self->eager ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->eager) 2 26 28
135 2 $self->resolve($_) for $self->get_supported_keys;
136 }
137  
138 27 return;
139 }
140  
141 76 ▶sub _coerce_build_method {
Subroutine _coerce_build_method: called CC=2 SCAR=6.9
142 76 my ($self) = @_;
143  
144 76 my $class = $self->build_class;
145 76 my $build_method = $self->build_method;
146  
147 76 ▶ my $method = $class->can( $self->build_method )
Branch: 1 branch
BranchTrueFalseTotal
my $method = $class->can($self->build_method) or croak("Error: class '${class}' does not support build method: $build_method") 75 1 76
148 or croak
149 "Error: class '$class' does not support build method: $build_method";
150  
151 75 return $method;
152 }
153  
154 47 ▶sub _build_object_from_args {
Subroutine _build_object_from_args: called CC=4 SCAR=13.9
155 47 my ( $self, $args, $key ) = @_;
156  
157 47 my $class = $self->build_class;
158 47 my $method = $self->_coerce_build_method;
159  
160 47 ▶ if ( $self->autoderef && ref($args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autoderef and ref $args) 38 9 47
Condition100%3/3
$self->autoderef and ref $args
!ll&&!rl&&r
4538
MC/DC100%2/2
$self->autoderef and ref $args
$self->autoderef ref $args
Truth table100%3/3
A and B
A= $self->autoderefB= ref $args
ABresult
0X0
100
111
161 38 given ( ref($args) ) {
162 38 when ('ARRAY') { return $class->$method( @{$args} ); }
163 32 when ('HASH') { return $class->$method( %{$args} ); }
164 15 when ('SCALAR') { return $class->$method( ${$args} ); }
165 14 when ('REF') { return $class->$method( ${$args} ); }
166 13 when ('GLOB') { return $class->$method( *{$args} ); }
167 12 when ('CODE') { return $class->$method( $args->($key) ); }
168 1 default {
169 1 ▶ carp( "cant autoderef argument ref('"
Branch: 1 branch
BranchTrueFalseTotal
if not $self->silence 1 0 1
170 . ref($args)
171 . "') for class '$class'" )
172 if !$self->silence;
173 }
174 }
175 }
176  
177 10 return $class->$method($args);
178 }
179  
180 4 ▶sub get_fallback_for_key {
Subroutine get_fallback_for_key: called CC=1 SCAR=0.0
Pod documented
181 4 my ( $self, $key ) = @_;
182  
183 4 return $self->_build_object_from_args( $self->fallback, $key );
184 }
185  
186 48 ▶sub resolve {
Subroutine resolve: called CC=3 SCAR=11.0
Pod documented
187 48 my ( $self, $key ) = @_;
188  
189 48 my $class = $self->build_class;
190 48 ▶ if ( $self->has_build_conf_for($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_build_conf_for($key)) { } 43 5 48
elsif ($self->has_fallback) { } 3 2 5
191 return try {
192 $self->_build_object_from_args( $self->get_build_conf_for($key),
193 $key );
194 }
195 catch {
196 $self->on_error->(
197 { exception => $_, factory => $self, key => $key } );
198 43 };
199 }
200 elsif ( $self->has_fallback ) {
201 3 return $self->get_fallback_for_key($key);
202 }
203  
204 2 confess("instance of '$class' named '$key' not found");
205 }
206  
207 6 ▶sub add_build_conf_for {
Subroutine add_build_conf_for: called CC=4 SCAR=13.9
Pod documented
208 6 my ( $self, $key, $conf, %conf ) = @_;
209  
210 6 ▶ if ( $self->has_build_conf_for($key) && $conf{not_override} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->has_build_conf_for($key) and $conf{'not_override'}) { } 1 5 6
elsif ($self->has_build_conf_for($key)) { } 3 2 5
Condition100%3/3
$self->has_build_conf_for($key) and $conf{"not_override"}
!ll&&!rl&&r
231
MC/DC100%2/2
$self->has_build_conf_for($key) and $conf{"not_override"}
$self->has_build_conf_for($key) $conf{"not_override"}
Truth table100%3/3
A and B
A= $self->has_build_conf_for($key)B= $conf{"not_override"}
ABresult
0X0
100
111
211 1 croak("cannot override exiting configuration for key '$key'");
212 }
213 elsif ( $self->has_build_conf_for($key) ) {
214  
215 # if we are using cache
216 # and we substitute the configuration for some reason
217 # we should first remove the cache for this particular key
218 3 $self->_cache_remove($key);
219 }
220  
221 5 return $self->_add_build_conf_for( $key => $conf );
222 }
223  
224 13 ▶sub _get_urn_for_cache {
Subroutine _get_urn_for_cache: called CC=1 SCAR=0.0
225 13 my ( $self, $key ) = @_;
226  
227 13 join q<:>, $self->build_class, $key;
228 }
229  
230 3 ▶sub _cache_remove {
Subroutine _cache_remove: called CC=2 SCAR=6.9
231 3 my ( $self, $key ) = @_;
232  
233 3 ▶ return if !$self->has_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cache 2 1 3
234  
235 1 $self->cache->remove( $self->_get_urn_for_cache($key) );
236 }
237  
238 38 ▶sub _cache_set {
Subroutine _cache_set: called CC=2 SCAR=6.9
239 38 my ( $self, $key, $value ) = @_;
240  
241 38 ▶ return if !$self->has_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cache 33 5 38
242  
243 5 my $urn = $self->_get_urn_for_cache($key);
244  
245 5 $self->cache->set( $urn => $value );
246 }
247  
248 48 ▶sub _cache_get {
Subroutine _cache_get: called CC=3 SCAR=11.0
249 48 my ( $self, $key ) = @_;
250  
251 48 ▶ return if !$self->has_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cache 41 7 48
252  
253 7 my $urn = $self->_get_urn_for_cache($key);
254  
255 7 my $cached = $self->cache->get($urn);
256  
257 7 ▶ return $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 2 5 7
258 }
259  
260 48 ▶around [qw(resolve get_fallback_for_key)] => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
261 48 my $orig = shift;
262 48 my ( $self, $key, @keys ) = @_;
263  
264 48 my $cached_value = $self->_cache_get($key);
265  
266 48 ▶ return $cached_value->[0] if $cached_value;
Branch: 1 branch
BranchTrueFalseTotal
if $cached_value 2 46 48
267  
268 46 my $instance = $self->$orig($key);
269  
270 38 $self->_cache_set( $key => [$instance] );
271  
272 38 ▶ if ( scalar(@keys) && $instance->can('resolve') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @keys and $instance->can("resolve")) 6 32 38
Condition66%2/3
scalar @keys and $instance->can("resolve")
!ll&&!rl&&r
3206
MC/DC50%1/2
scalar @keys and $instance->can("resolve")
scalar @keys $instance->can("resolve")
Truth table66%2/3
A and B
A= scalar @keysB= $instance->can("resolve")
ABresult
0X0
100
111
273 6 return $instance->$orig(@keys);
274 }
275  
276 32 return $instance;
277 6 };
278  
279 6 1;
280  
281 __END__
↑ Summary