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