Depot.pm

filter: Statementstmt 89.3% 177 / 198 Branchbran 51.1% 46 / 90 Conditioncond 62.5% 15 / 24 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 88.0% 44 / 50 Podpod 100% 14 / 14 totaltotal 77.7% 304 / 391 CC 55 SCAR 17.9
CC
55
Cov
76%
_process_key_arg
17.4
alias_key
14.0
has_injection
12.0
CRAP 6.0
↑ Summary Depot/Role.pm »
1 package Object::Depot;
2 7 our $VERSION = '0.04';
3 7 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf8
7  
8 =head1 NAME
9  
10 Object::Depot - Decouple object instantiation from usage.
11  
12 =head1 SYNOPSIS
13  
14 use Object::Depot;
15  
16 my $depot = Object::Depot->new(
17 class => 'CHI',
18 # CHI->new returns a CHI::Driver object.
19 type => InstanceOf[ 'CHI::Driver' ],
20 );
21  
22 $depot->add_key(
23 sessions => {
24 driver => 'Memory',
25 global => 1,
26 },
27 );
28  
29 $depot->store( ip2geo => CHI->new(...) );
30  
31 my $sessions = $depot->fetch('sessions');
32 my $ip2geo = $depot->fetch('ip2geo');
33  
34 =head1 DESCRIPTION
35  
36 Object depots encapsulate object construction so that users of objects
37 do not need to know how to create the objects in order to use them.
38  
39 The primary use case for this library is for storing the connection
40 logic to external services and making these connections globally
41 available to all application logic. See L<Object::Depot::Role> for
42 turning your depot object into a global singleton.
43  
44 =cut
45  
46 7 ▶use Guard qw( guard );
Subroutine BEGIN: called CC=1 SCAR=0.0
47 7 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 7 ▶use Role::Tiny qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 7 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
50 7 ▶use Sub::Name qw( subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
51 7 ▶use Types::Common::String qw( NonEmptySimpleStr );
Subroutine BEGIN: called CC=1 SCAR=0.0
52 7 ▶use Types::Standard qw( Bool CodeRef HashRef Object InstanceOf );
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 8 ▶sub croak {
Subroutine croak: called CC=1 SCAR=0.0
56 8 local $Carp::Internal{'Object::Depot'} = 1;
57 8 local $Carp::Internal{'Object::Depot::Role'} = 1;
58 8 return Carp::croak( @_ );
59 }
60  
61 sub croakf {
62 my $msg = shift;
63 $msg = sprintf( $msg, @_ );
64 @_ = ( $msg );
65 goto &croak;
66 }
67  
68 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 12 ▶sub _normalize_args {
Subroutine _normalize_args: called CC=5 SCAR=16.6
72 12 my ($self, $args) = @_;
73  
74 12 ▶ return {} if !@$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 8 4 12
75 4 ▶ return $args->[0] if @$args==1 and ref($args->[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @$args == 1 and ref $args->[0] eq "HASH" 3 1 4
Condition66%2/3
@$args == 1 and ref $args->[0] eq "HASH"
!ll&&!rl&&r
103
MC/DC50%1/2
@$args == 1 and ref $args->[0] eq "HASH"
@$args == 1 ref $args->[0] eq "HASH"
Truth table66%2/3
A and B
A= @$args == 1B= ref $args->[0] eq "HASH"
ABresult
0X0
100
111
76 1 ▶ return { @$args } unless @$args % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args % 2 1 0 1
77  
78 0 croakf(
79 'Odd number of arguments passed to %s()',
80 scalar( caller ),
81 );
82 }
83  
84 68 ▶sub _process_key_arg {
Subroutine _process_key_arg: called CC=13 SCAR=28.5
85 68 my ($self, $args) = @_;
86  
87 68 my $caller_sub_name = (caller 1)[3];
88 68 ▶ $caller_sub_name = '__ANON__' if !defined $caller_sub_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $caller_sub_name 0 68 68
89 68 $caller_sub_name =~ s{^.*::}{};
90  
91 68 my $key;
92  
93 68 ▶ $key = shift @$args
Branch: 1 branch
BranchTrueFalseTotal
if @$args and not builtin::blessed($args->[0]) 50 18 68
Condition100%3/3
@$args and not builtin::blessed($args->[0])
!ll&&!rl&&r
16250
MC/DC100%2/2
@$args and not builtin::blessed($args->[0])
@$args not builtin::blessed($args->[0])
Truth table100%3/3
A and not(B)
A= @$argsB= not builtin::blessed($args->[0])
ABresult
0X0
100
111
94 if @$args and !blessed $args->[0];
95  
96 68 ▶ if ($self->_has_default_key() and !defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_default_key and not defined $key) { } 14 54 68
Condition100%3/3
$self->_has_default_key and not defined $key
!ll&&!rl&&r
51314
MC/DC100%2/2
$self->_has_default_key and not defined $key
$self->_has_default_key not defined $key
Truth table100%3/3
A and not(B)
A= $self->_has_default_keyB= not defined $key
ABresult
0X0
100
111
97 14 $key = $self->default_key();
98 }
99 else {
100 54 ▶ croak "No key was passed to $caller_sub_name()"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 8 46 54
101 if !defined $key;
102  
103 46 ▶ if (!NonEmptySimpleStr->check( $key )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&NonEmptySimpleStr()->check($key)) 0 46 46
104 0 ▶ $key = defined($key) ? ["$key"] : 'UNDEF';
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
105 0 croak "Invalid key, $key, passed to $caller_sub_name(): " .
106 NonEmptySimpleStr->message( $key );
107 }
108 }
109  
110 60 ▶ $key = $self->_aliases->{$key} if exists $self->_aliases->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_aliases->{$key} 1 59 60
111  
112 60 ▶ if ($self->strict_keys() and !exists $self->_key_args->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->strict_keys and not exists $self->_key_args->{$key}) 0 60 60
Condition33%1/3
$self->strict_keys and not exists $self->_key_args->{$key}
!ll&&!rl&&r
6000
MC/DC0%0/2
$self->strict_keys and not exists $self->_key_args->{$key}
$self->strict_keys not exists $self->_key_args->{$key}
Truth table33%1/3
A and not(B)
A= $self->strict_keysB= not exists $self->_key_args->{$key}
ABresult
0X0
100
111
113 0 ▶ $key = defined($key) ? qq["$key"] : 'UNDEF';
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 0 0 0
114 0 croak "Undeclared key, $key, passed to $caller_sub_name()"
115 }
116  
117 60 return $key;
118 }
119  
120 has _all_objects => (
121 is => 'ro',
122 15 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
123 7 );
124  
125 40 ▶sub _objects {
Subroutine _objects: called CC=4 SCAR=23.2
126 40 my ($self) = @_;
127  
128 40 ▶ return $self->_all_objects() if !$self->per_process();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->per_process 40 0 40
129  
130 0 my $key = $$;
131 0 ▶ $key .= '-' . threads->tid() if $INC{'threads.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"threads.pm"} 0 0 0
132  
133 0 ▶ return $self->_all_objects->{$key} ||= {};
Condition0%0/2
$self->_all_objects->{$key} ||= {}
l!l
00
MC/DC0%0/1
$self->_all_objects->{$key} ||= {}
$self->_all_objects->{$key}
Truth table0%0/2
A
A= $self->_all_objects->{$key}
Aresult
11
00
134 }
135  
136 has _key_args => (
137 is => 'ro',
138 15 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
139 7 );
140  
141 has _aliases => (
142 is => 'ro',
143 15 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
144 7 );
145  
146 has _injections => (
147 is => 'ro',
148 15 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
149 7 );
150  
151 =head1 ARGUMENTS
152  
153 =head2 class
154  
155 class => 'CHI',
156  
157 The class which objects in this depot are expected to be. This
158 argument defaults the L</constructor> and L</type> arguments.
159  
160 Does not have a default.
161  
162 Leaving this argument unset causes L</fetch> to fail on keys that were
163 not first populated with L</store> as the L</constructor> subroutine
164 will just return C<undef>.
165  
166 =cut
167  
168 7 has class => (
169 is => 'ro',
170 isa => NonEmptySimpleStr,
171 predicate => '_has_class',
172 );
173  
174 =head2 constructor
175  
176 constuctor => sub{
177 my ($args) = @_;
178 return __PACKAGE__->depot->class->new( $args );
179 },
180  
181 Set this to a code ref to control how objects get constructed.
182  
183 When declaring a custom constructor be careful not to create memory
184 leaks via circular references.
185  
186 L</create> validates the objects produced by this constructor and will
187 throw an exception if they do not match L</type>.
188  
189 The default code ref is similar to the above example if L</class> is
190 set. If it is not set then the default code ref will return C<undef>.
191  
192 =cut
193  
194 7 has constructor => (
195 is => 'lazy',
196 isa => CodeRef,
197 );
198  
199 0 ▶my $undef_constructor = subname 'undef_constructor', sub{ undef };
Subroutine undef_constructor: not called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=1 SCAR=2.6
200  
201 10 ▶sub _build_constructor {
Subroutine _build_constructor: called CC=2 SCAR=7.1
202 10 my ($self) = @_;
203  
204 10 ▶ return $undef_constructor if !$self->_has_class();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_class 0 10 10
205  
206 10 return _build_class_constructor( $self->class() );
207 }
208  
209 10 ▶sub _build_class_constructor {
Subroutine _build_class_constructor: called CC=1 SCAR=0.0
210 10 my ($class) = @_;
211  
212 15 ▶ return subname 'class_constructor', sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
213 15 my @args = @_;
214 15 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
215 15 $class->new( @args );
216 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 croakf(
218 'Error creating object %s: %s',
219 $class,
220 $_,
221 );
222 15 };
223 10 };
224 }
225  
226 =head2 type
227  
228 type => InstanceOf[ 'CHI::Driver' ],
229  
230 Set this to a L<Type::Tiny> type to control how objects in the depot
231 are validated when they are stored.
232  
233 Defaults to C<InstanceOf> L</class>, if set. If the class is not set
234 then this defaults to C<Object> (both are from L<Types::Standard>).
235  
236 =cut
237  
238 7 has type => (
239 is => 'lazy',
240 isa => InstanceOf[ 'Type::Tiny' ],
241 );
242  
243 11 ▶sub _build_type {
Subroutine _build_type: called CC=2 SCAR=6.9
244 11 my ($self) = @_;
245 11 ▶ return InstanceOf[ $self->class() ] if $self->_has_class();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_class 10 1 11
246 1 return Object;
247 }
248  
249 =head2 injection_type
250  
251 injection_type => Object,
252  
253 By default objects that are injected (see L</inject>) are validated
254 against L</type>. Set this to a type that injections validate
255 against if it needs to be different (such as to support mock
256 objects).
257  
258 =cut
259  
260 7 has injection_type => (
261 is => 'lazy',
262 isa => InstanceOf[ 'Type::Tiny' ],
263 );
264  
265 3 ▶sub _build_injection_type {
Subroutine _build_injection_type: called CC=1 SCAR=0.0
266 3 my ($self) = @_;
267 3 return $self->type();
268 }
269  
270 =head2 per_process
271  
272 per_process => 1,
273  
274 Turn this on to store objects per-process; meaning, if the TID (thread
275 ID) or PID (process ID) change then this depot will act as if no
276 objects have been stored. Generally you will not want to turn this
277 on. On occasion, though, some objects are not thread or forking safe
278 and it is necessary.
279  
280 Defaults off.
281  
282 =cut
283  
284 7 has per_process => (
285 is => 'ro',
286 isa => Bool,
287 default => 0,
288 );
289  
290 =head2 disable_store
291  
292 disable_store => 1,
293  
294 When on this causes L</store> to silently not store, causing all
295 L</fetch> calls for non-injected keys to return a new object.
296  
297 Defaults off.
298  
299 =cut
300  
301 7 has disable_store => (
302 is => 'ro',
303 isa => Bool,
304 default => 0,
305 );
306  
307 =head2 strict_keys
308  
309 strict_keys => 1,
310  
311 Turn this on to require that all keys used must first be declared
312 via L</add_key> before they can be stored in the depot.
313  
314 Defaults to off, meaning keys may be used without having to
315 pre-declare them.
316  
317 =cut
318  
319 7 has strict_keys => (
320 is => 'ro',
321 isa => Bool,
322 default => 0,
323 );
324  
325 =head2 default_key
326  
327 default_key => 'generic',
328  
329 If no key is passed to key-accepting methods like L</fetch> then they
330 will use this default key if available.
331  
332 Defaults to no default key.
333  
334 =cut
335  
336 7 has default_key => (
337 is => 'ro',
338 isa => NonEmptySimpleStr,
339 predicate => '_has_default_key',
340 );
341  
342 =head2 key_argument
343  
344 key_argument => 'connection_key',
345  
346 When set, this causes L</arguments> to include an extra argument to be
347 passed to the class during object construction. The argument's key
348 will be whatever you set this to and the value will be the key used to
349 fetch the object.
350  
351 You will still need to write the code in your class to capture the
352 argument, such as:
353  
354 has connection_key => ( is=>'ro' );
355  
356 Defaults to no key argument.
357  
358 =cut
359  
360 7 has key_argument => (
361 is => 'ro',
362 isa => NonEmptySimpleStr,
363 predicate => '_has_key_argument',
364 );
365  
366 =head2 default_arguments
367  
368 default_arguments => {
369 arg => 'value',
370 ...
371 },
372  
373 When set, these arguments will be included in calls to L</arguments>.
374  
375 Defaults to an empty hash ref.
376  
377 =cut
378  
379 has default_arguments => (
380 is => 'lazy',
381 isa => HashRef,
382 11 ▶ default => sub{ {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
383 7 );
384  
385 =head2 export_name
386  
387 export_name => 'myapp_cache',
388  
389 Set the name of a function that L<Object::Depot::Role> will
390 export to importers of your depot package.
391  
392 Has no default. If this is not set, then nothing will be exported.
393  
394 =cut
395  
396 7 has export_name => (
397 is => 'ro',
398 isa => NonEmptySimpleStr,
399 predicate => '_has_export_name',
400 );
401  
402 =head2 always_export
403  
404 always_export => 1,
405  
406 Turning this on causes L<Object::Depot::Role> to always export
407 the L</export_name>, rather than only when listed in the import
408 arguments. This is synonymous with the difference between
409 L<Exporter>'s C<@EXPORT_OK> and C<@EXPORT>.
410  
411 =cut
412  
413 7 has always_export => (
414 is => 'ro',
415 isa => Bool,
416 default => 0,
417 );
418  
419 =head1 METHODS
420  
421 =head2 active_objects
422  
423 my @objects = $depot->active_objects();
424  
425 Return an array containing all active objects the depot created via calls to $depot->create().
426  
427 If per_process is set, returns only active objects created by the current process/thread.
428  
429 =cut
430  
431 0 ▶sub active_objects {
Subroutine active_objects: not called CC=1 SCAR=6.9
Pod documented
432 0 my $self = shift;
433  
434 0 return values %{$self->_objects};
435 }
436  
437 =head2 fetch
438  
439 my $object = $depot->fetch( $key );
440  
441 =cut
442  
443 31 ▶sub fetch {
Subroutine fetch: called CC=2 SCAR=7.0
Pod documented
444 31 my $self = shift;
445  
446 31 my $key = $self->_process_key_arg( \@_ );
447 27 ▶ croak 'Too many arguments passed to fetch()' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 27 27
448  
449 27 return $self->_fetch( $key );
450 }
451  
452 27 ▶sub _fetch {
Subroutine _fetch: called CC=4 SCAR=13.9
453 27 my ($self, $key) = @_;
454  
455 27 my $object = $self->_injections->{ $key };
456 27 ▶ $object ||= $self->_objects->{$key};
Condition100%3/3
$object ||= $self->_objects->{$key}
l!l&&r!l&&!r
31014
MC/DC100%2/2
$object ||= $self->_objects->{$key}
$object $self->_objects->{$key}
Truth table100%3/3
A ||= B
A= $objectB= $self->_objects->{$key}
ABresult
1X1
011
000
457 27 ▶ return $object if $object;
Branch: 1 branch
BranchTrueFalseTotal
if $object 13 14 27
458  
459 14 ▶ return undef if !$self->_has_class();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_class 2 12 14
460  
461 12 $object = $self->_create( $key, {} );
462  
463 12 $self->_store( $key, $object );
464  
465 12 return $object;
466 }
467  
468 =head2 store
469  
470 $depot->store( $key => $object );
471  
472 =cut
473  
474 1 ▶sub store {
Subroutine store: called CC=5 SCAR=16.8
Pod documented
475 1 my $self = shift;
476  
477 1 my $key = $self->_process_key_arg( \@_ );
478 1 ▶ croak 'Too many arguments passed to store()' if @_>1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 1 1
479 1 ▶ croak 'Not enough arguments passed to store()' if @_<1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 1 1
480  
481 1 my $object = shift;
482 1 ▶ croakf(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->type->check($object) 0 1 1
483 'Invalid object passed to store(): %s',
484 $self->type->get_message( $object ),
485 ) if !$self->type->check( $object );
486  
487 croak qq[Already stored key, "$key", passed to store()]
488 1 ▶ if exists $self->_objects->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_objects->{$key} 0 1 1
489  
490 1 return $self->_store( $key, $object );
491 }
492  
493 13 ▶sub _store {
Subroutine _store: called CC=2 SCAR=7.0
494 13 my ($self, $key, $object) = @_;
495  
496 13 ▶ return if $self->disable_store();
Branch: 1 branch
BranchTrueFalseTotal
if $self->disable_store 0 13 13
497  
498 13 $self->_objects->{$key} = $object;
499  
500 13 return;
501 }
502  
503 =head2 remove
504  
505 $depot->remove( $key );
506  
507 =cut
508  
509 2 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
510 2 my $self = shift;
511  
512 2 my $key = $self->_process_key_arg( \@_ );
513 2 ▶ croak 'Too many arguments passed to remove()' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
514  
515 2 return $self->_remove( $key );
516 }
517  
518 2 ▶sub _remove {
Subroutine _remove: called CC=1 SCAR=0.0
519 2 my ($self, $key) = @_;
520  
521 2 return delete $self->_objects->{$key};
522 }
523  
524 =head2 create
525  
526 my $object = $depot->create( $key, %extra_args );
527  
528 Gathers arguments from L</arguments> and then calls L</constructor>
529 on them, returning a new object. Extra arguments may be passed and
530 they will take precedence.
531  
532 =cut
533  
534 3 ▶sub create {
Subroutine create: called CC=1 SCAR=0.0
Pod documented
535 3 my $self = shift;
536  
537 3 my $key = $self->_process_key_arg( \@_ );
538  
539 3 my $extra_args = $self->_normalize_args( \@_ );
540  
541 3 return $self->_create( $key, $extra_args );
542 }
543  
544 15 ▶sub _create {
Subroutine _create: called CC=4 SCAR=17.1
545 15 my ($self, $key, $extra_args) = @_;
546  
547 15 my $args = $self->_arguments( $key, $extra_args );
548  
549 15 my $object = $self->constructor->( $args );
550  
551 15 ▶ croakf(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->type->check($object) 0 15 15
defined $object ? : 0 0 0
Condition0%0/2
ref $object or qq["$object"]
l!l
00
MC/DC0%0/1
ref $object or qq["$object"]
ref $object
Truth table0%0/2
A
A= ref $object
Aresult
11
00
552 'Constructor returned an invalid value, %s, for key %s: %s',
553 defined($object) ? (ref($object) || qq["$object"]) : 'UNDEF',
554 qq["$key"],
555 $self->type->get_message( $object ),
556 ) if !$self->type->check( $object );
557  
558 15 return $object;
559 }
560  
561 =head2 arguments
562  
563 my $args = $depot->arguments( $key, %extra_args );
564  
565 This method returns an arguments hash ref that would be used to
566 instantiate a new L</class> object. You could, for example, use this
567 to produce a base-line set of arguments, then sprinkle in some more,
568 and make yourself a special mock object to be injected.
569  
570 =cut
571  
572 3 ▶sub arguments {
Subroutine arguments: called CC=1 SCAR=0.0
Pod documented
573 3 my $self = shift;
574  
575 3 my $key = $self->_process_key_arg( \@_ );
576  
577 3 my $extra_args = $self->_normalize_args( \@_ );
578  
579 3 return $self->_arguments( $key, $extra_args );
580 }
581  
582 18 ▶sub _arguments {
Subroutine _arguments: called CC=3 SCAR=11.0
583 18 my ($self, $key, $extra_args) = @_;
584  
585 my $args = {
586 18 %{ $self->default_arguments() },
587 18 ▶ %{ $self->_key_args->{$key} || {} },
Condition100%2/2
$self->_key_args->{$key} || {}
l!l
612
MC/DC100%1/1
$self->_key_args->{$key} || {}
$self->_key_args->{$key}
Truth table100%2/2
A
A= $self->_key_args->{$key}
Aresult
11
00
588 %$extra_args,
589 };
590  
591 18 ▶ $args->{ $self->key_argument() } = $key
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_key_argument 3 15 18
592 if $self->_has_key_argument();
593  
594 18 return $args;
595 }
596  
597 =head2 declared_keys
598  
599 my $keys = $depot->declared_keys();
600 foreach my $key (@$keys) { ... }
601  
602 Returns an array ref containing all the keys declared with
603 L</add_key>.
604  
605 =cut
606  
607 0 ▶sub declared_keys {
Subroutine declared_keys: not called CC=1 SCAR=6.9
Pod documented
608 0 my $self = shift;
609 0 return [ keys %{ $self->_key_args() } ];
610 }
611  
612 =head2 inject
613  
614 $depot->inject( $key, $object );
615  
616 Takes an object of your making and forces L</fetch> to return the
617 injected object. This is useful for injecting mock objects in tests.
618  
619 The injected object must validate against L</type>.
620  
621 =cut
622  
623 3 ▶sub inject {
Subroutine inject: called CC=5 SCAR=16.7
Pod documented
624 3 my $self = shift;
625  
626 3 my $key = $self->_process_key_arg( \@_ );
627 3 ▶ croak 'Too many arguments passed to inject()' if @_>1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 3 3
628 3 ▶ croak 'Not enough arguments passed to inject()' if @_<1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 3 3
629  
630 3 my $object = shift;
631 3 ▶ croakf(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->injection_type->check($object) 0 3 3
632 'Invalid object passed to inject(): %s',
633 $self->injection_type->get_message( $object ),
634 ) if !$self->injection_type->check( $object );
635  
636 croak qq[Already injected key, "$key", passed to inject()]
637 3 ▶ if exists $self->_injections->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_injections->{$key} 0 3 3
638  
639 3 $self->_injections->{$key} = $object;
640  
641 3 return;
642 }
643  
644 =head2 inject_with_guard
645  
646 my $guard = $depot->inject_with_guard( $key => $object );
647  
648 This is just like L</inject> except it returns a L<Guard> object
649 which, when it leaves scope and is destroyed, will automatically
650 call L</clear_injection>.
651  
652 =cut
653  
654 1 ▶sub inject_with_guard {
Subroutine inject_with_guard: called CC=1 SCAR=0.0
Pod documented
655 1 my $self = shift;
656  
657 1 my $key = $self->_process_key_arg( \@_ );
658  
659 1 $self->inject( $key, @_ );
660  
661 1 ▶ return guard {
Subroutine __ANON__: called CC=1 SCAR=0.0
662 1 return $self->clear_injection( $key );
663 1 };
664 }
665  
666 =head2 clear_injection
667  
668 my $object = $depot->clear_injection( $key );
669  
670 Removes and returns the injected object, restoring the original
671 behavior of L</fetch>.
672  
673 =cut
674  
675 3 ▶sub clear_injection {
Subroutine clear_injection: called CC=2 SCAR=7.0
Pod documented
676 3 my $self = shift;
677  
678 3 my $key = $self->_process_key_arg( \@_ );
679 3 ▶ croak 'Too many arguments passed to clear_injection()' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3 3
680  
681 3 return delete $self->_injections->{$key};
682 }
683  
684 =head2 injection
685  
686 my $object = $depot->injection( $key );
687  
688 Returns the injected object, or C<undef> if none has been injected.
689  
690 =cut
691  
692 9 ▶sub injection {
Subroutine injection: called CC=2 SCAR=7.0
Pod documented
693 9 my $self = shift;
694  
695 9 my $key = $self->_process_key_arg( \@_ );
696 9 ▶ croak 'Too many arguments passed to injection()' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 9 9
697  
698 9 return $self->_injections->{ $key };
699 }
700  
701 =head2 has_injection
702  
703 if ($depot->has_injection( $key )) { ... }
704  
705 Returns true if an injection is in place for the specified key.
706  
707 =cut
708  
709 0 ▶sub has_injection {
Subroutine has_injection: not called CC=3 SCAR=24.8
Pod documented
710 0 my $self = shift;
711  
712 0 my $key = $self->_process_key_arg( \@_ );
713 0 ▶ croak 'Too many arguments passed to has_injection()' if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
714  
715 0 ▶ return exists($self->_injections->{$key}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->_injections->{$key} ? : 0 0 0
716 }
717  
718 =head2 add_key
719  
720 $depot->add_key( $key, %arguments );
721  
722 Declares a new key and, optionally, the arguments used to construct
723 the L</class> object.
724  
725 Arguments are optional, but if present they will be saved and used
726 by L</fetch> when calling C<new()> (via L</arguments>) on L</class>.
727  
728 =cut
729  
730 6 ▶sub add_key {
Subroutine add_key: called CC=4 SCAR=15.6
Pod documented
731 6 my ($self, $key, @args) = @_;
732  
733 6 ▶ croakf(
Branch: 2 branches
BranchTrueFalseTotal
unless &NonEmptySimpleStr()->check($key) 0 6 6
defined $key ? : 0 0 0
734 'Invalid key, %s, passed to add_key(): %s',
735 defined($key) ? qq["$key"] : 'UNDEF',
736 NonEmptySimpleStr->get_message( $key ),
737 ) if !NonEmptySimpleStr->check( $key );
738  
739 croak "Already declared key, \"$key\", passed to add_key()"
740 6 ▶ if exists $self->_key_args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_key_args->{$key} 0 6 6
741  
742 6 $self->_key_args->{$key} = $self->_normalize_args( \@args );
743  
744 6 return;
745 }
746  
747 =head2 alias_key
748  
749 $depot->alias_key( $alias_key => $real_key );
750  
751 Adds a key that is an alias to another key.
752  
753 =cut
754  
755 1 ▶sub alias_key {
Subroutine alias_key: called CC=8 SCAR=26.4
Pod documented
756 1 my ($self, $alias, $key) = @_;
757  
758 1 ▶ croakf(
Branch: 2 branches
BranchTrueFalseTotal
unless &NonEmptySimpleStr()->check($alias) 0 1 1
defined $alias ? : 0 0 0
759 'Invalid alias, %s, passed to alias_key(): %s',
760 defined($alias) ? qq["$alias"] : 'UNDEF',
761 NonEmptySimpleStr->get_message( $alias ),
762 ) if !NonEmptySimpleStr->check( $alias );
763  
764 1 ▶ croakf(
Branch: 2 branches
BranchTrueFalseTotal
unless &NonEmptySimpleStr()->check($key) 0 1 1
defined $key ? : 0 0 0
765 'Invalid key, %s, passed to alias_key(): %s',
766 defined($key) ? qq["$key"] : 'UNDEF',
767 NonEmptySimpleStr->get_message( $key ),
768 ) if !NonEmptySimpleStr->check( $key );
769  
770 croak "Already declared alias, \"$alias\", passed to alias_key()"
771 1 ▶ if exists $self->_aliases->{$alias};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_aliases->{$alias} 0 1 1
772  
773 croak "Undeclared key, \"$key\", passed to alias_key()"
774 1 ▶ if $self->strict_keys() and !exists $self->_key_args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->strict_keys and not exists $self->_key_args->{$key} 0 1 1
Condition33%1/3
$self->strict_keys and not exists $self->_key_args->{$key}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->strict_keys and not exists $self->_key_args->{$key}
$self->strict_keys not exists $self->_key_args->{$key}
Truth table33%1/3
A and not(B)
A= $self->strict_keysB= not exists $self->_key_args->{$key}
ABresult
0X0
100
111
775  
776 1 $self->_aliases->{$alias} = $key;
777  
778 1 return;
779 }
780  
781 7 1;
782 __END__
↑ Summary Depot/Role.pm »