blib/lib/Object/Depot.pm

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