Depot/Role.pm

filter: Statementstmt 95.8% 69 / 72 Branchbran 50.0% 6 / 12 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 100% 17 / 17 Podpod 100% 3 / 3 totaltotal 81.3% 100 / 123 CC 12 SCAR 14.4
CC
12
Cov
83%
import
8.5
init_depot
7.6
__ANON__
1.0
CRAP 4.2
« Depot.pm ↑ Summary
1 package Object::Depot::Role;
2 2 our $VERSION = '0.04';
3 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf8
7  
8 =head1 NAME
9  
10 Object::Depot::Role - Expose Object::Depot as a global singleton.
11  
12 =head1 SYNOPSIS
13  
14 See L<Object::Depot/SYNOPSIS>.
15  
16 =head1 DESCRIPTION
17  
18 This role rolls up an L<Object::Depot> into a singleton available to
19 all code in your application. This role is ideal for creating global,
20 simplified, and centralized access to shared resources such as
21 connections to internal and cloud services.
22  
23 =cut
24  
25 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Object::Depot;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Sub::Name qw( subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 my %DEPOTS;
34  
35 4 ▶sub import {
Subroutine import: called CC=8 SCAR=21.4
36 4 my $class = shift;
37 4 my $target = caller();
38 4 my $depot = $class->depot();
39  
40 4 ▶ return if !$depot->_has_export_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $depot->_has_export_name 0 4 4
41  
42 4 my $name = $depot->export_name();
43 4 my $do_it = $depot->always_export();
44  
45 4 foreach my $arg (@_) {
46 2 ▶ if (defined($arg) and $arg eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg and $arg eq $name) 2 0 2
Condition33%1/3
defined $arg and $arg eq $name
!ll&&!rl&&r
002
MC/DC0%0/2
defined $arg and $arg eq $name
defined $arg $arg eq $name
Truth table33%1/3
A and B
A= defined $argB= $arg eq $name
ABresult
0X0
100
111
47 2 $do_it = 1;
48 2 next;
49 }
50  
51 0 ▶ croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 0 0 0
52 'Unknown export, %s, passed to %s',
53 defined($arg) ? qq["$arg"] : 'undef',
54 $target,
55 );
56 }
57  
58 4 ▶ return if !$do_it;
Branch: 1 branch
BranchTrueFalseTotal
unless $do_it 1 3 4
59  
60 3 my $sub = $class->can($name);
61 3 ▶ $sub ||= subname $name => sub{ $class->fetch(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine myapp_test: called CC=1 SCAR=0.0
Condition66%2/3
$sub ||= subname($name, sub { $class->fetch(@_); } )
l!l&&r!l&&!r
120
MC/DC0%0/2
$sub ||= subname($name, sub { $class->fetch(@_); } )
$sub subname($name, sub { $class->fetch(@_); } )
Truth table66%2/3
A ||= B
A= $subB= subname($name, sub { $class->fetch(@_); } )
ABresult
1X1
011
000
62  
63 {
64 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
66 3 *{"$target\::$name"} = $sub
67 }
68  
69 3 return;
70 }
71  
72  
73 =head1 CLASS ATTRIBUTES
74  
75 =head2 depot
76  
77 The L<Object::Depot> singleton object. Will return C<undef> if
78 L</init_depot> has not yet been called.
79  
80 =cut
81  
82 10 ▶sub depot {
Subroutine depot: called CC=1 SCAR=0.0
Pod documented
83 10 my ($class) = @_;
84 10 return $DEPOTS{ $class };
85 }
86  
87 =head1 CLASS METHODS
88  
89 =head2 init_depot
90  
91 __PACKAGE__->init_depot( $depot );
92  
93 Takes an L<Object::Depot> object and saves it for later retrieval by
94 L</depot>.
95  
96 =cut
97  
98 3 ▶sub init_depot {
Subroutine init_depot: called CC=5 SCAR=20.3
Pod documented
99 3 my $class = shift;
100  
101 croak "init_depot() has already been called on $class"
102 3 ▶ if $DEPOTS{ $class };
Branch: 1 branch
BranchTrueFalseTotal
if $DEPOTS{$class} 0 3 3
103  
104 3 ▶ if (@_==1 and blessed($_[0]) and $_[0]->isa('Object::Depot')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa("Object::Depot")) 0 3 3
Condition33%2/6
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa("Object::Depot")
!ll&&!rl&&r
300
@_ == 1 and builtin::blessed($_[0])
!ll&&!rl&&r
300
MC/DC0%0/3
@_ == 1 and builtin::blessed($_[0]) and $_[0]->isa("Object::Depot")
@_ == 1 builtin::blessed($_[0]) $_[0]->isa("Object::Depot")
Truth table25%1/4
A and B and C
A= @_ == 1B= builtin::blessed($_[0])C= $_[0]->isa("Object::Depot")
ABCresult
0XX0
10X0
1100
1111
105 0 $DEPOTS{ $class } = shift;
106 0 return;
107 }
108  
109 3 $DEPOTS{ $class } = Object::Depot->new( @_ );
110  
111 3 return;
112 }
113  
114 =head1 PROXIED METHODS
115  
116 These class methods proxy to the L</depot> object.
117  
118 =over
119  
120 =item L<Object::Depot/fetch>
121  
122 =item L<Object::Depot/store>
123  
124 =item L<Object::Depot/remove>
125  
126 =item L<Object::Depot/create>
127  
128 =item L<Object::Depot/arguments>
129  
130 =item L<Object::Depot/declared_keys>
131  
132 =item L<Object::Depot/inject>
133  
134 =item L<Object::Depot/inject_with_guard>
135  
136 =item L<Object::Depot/clear_injection>
137  
138 =item L<Object::Depot/injection>
139  
140 =item L<Object::Depot/has_injection>
141  
142 =item L<Object::Depot/add_key>
143  
144 =item L<Object::Depot/alias_key>
145  
146 =back
147  
148 =cut
149  
150 2 foreach my $method (qw(
151 fetch
152 store
153 remove
154 create
155 arguments
156 declared_keys
157 inject
158 inject_with_guard
159 clear_injection
160 injection
161 has_injection
162 add_key
163 alias_key
164 )) {
165 4 ▶ my $sub = subname( $method => sub{
Subroutine has_injection: called CC=1 SCAR=0.0
Pod documented
166 4 my $class = shift;
167 4 local $Carp::CarpInternal{ (__PACKAGE__) } = 1;
168 4 return $class->depot->$method( @_ );
169 26 });
170  
171 2 ▶ { no strict 'refs'; *{__PACKAGE__ . "::$method"} = $sub }
Subroutine BEGIN: called CC=1 SCAR=0.0
172 }
173  
174 2 1;
175 __END__
« Depot.pm ↑ Summary