| 1 | package Pickles::Container; | ||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 4 | ▶ | use Carp (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 7 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 6 | 7 | my ($class, %args) = @_; | |||||||||||||||||||
| 7 | bless { | ||||||||||||||||||||
| 8 | components => {}, | ||||||||||||||||||||
| 9 | home => $args{home}, | ||||||||||||||||||||
| 10 | 7 | objects => {}, | |||||||||||||||||||
| 11 | scoped_objects => {}, | ||||||||||||||||||||
| 12 | }, $class; | ||||||||||||||||||||
| 13 | } | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 6 | ▶ | sub Pickles::Container::ScopeGuard::DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 6 | my $self = shift; | |||||||||||||||||||
| 17 | 6 | $self->{container}->clear_scope(); | |||||||||||||||||||
| 18 | } | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 6 | ▶ | sub new_scope { | ||||||||||||||||||
|
Subroutine
new_scope: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 21 | 6 | my $self = shift; | |||||||||||||||||||
| 22 | 6 | return bless { container => $self }, 'Pickles::Container::ScopeGuard'; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 6 | ▶ | sub clear_scope { | ||||||||||||||||||
|
Subroutine
clear_scope: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 26 | 6 | my $self = shift; | |||||||||||||||||||
| 27 | 6 | $self->{scoped_objects} = {}; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 6 | ▶ | sub load { | ||||||||||||||||||
|
Subroutine
load: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||
| 31 | 6 | my ($self, $file) = @_; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 6 | my $o = \®ister; | |||||||||||||||||||
| 34 | 4 | ▶ | no warnings 'redefine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 14 | ▶ | local *register = sub($$;$) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | 14 | $o->( $self, @_ ); | |||||||||||||||||||
| 37 | 6 | }; | |||||||||||||||||||
| 38 | 1 | ▶ | local *load_file = sub(@) { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||
| 39 | 1 | my $c = $self->get('config'); | |||||||||||||||||||
| 40 | # XXX what if there's no config? | ||||||||||||||||||||
| 41 | 1 | my $file = $c->path_to(@_); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 1 | delete $INC{$file}; | |||||||||||||||||||
| 44 | 1 | my $rv = require $file; | |||||||||||||||||||
| 45 | 1 | ▶ | Carp::croak("Failed to parse file $file: $@") if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 1 | ▶ | Carp::croak("Failed to run file (did you return a true value?)") unless $rv; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 1 | return $rv; | |||||||||||||||||||
| 48 | 6 | }; | |||||||||||||||||||
| 49 | 6 | my $result = do $file; | |||||||||||||||||||
| 50 | 6 | ▶ | die "Failed to parse file $file: $@" if $@; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 6 | ▶ | die "Failed to run file (did you return a true value?)" unless $result; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 6 | $self; | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 18 | ▶ | sub components { | ||||||||||||||||||
|
Subroutine
components: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 56 | 18 | my $self = shift; | |||||||||||||||||||
| 57 | 18 | my $h = $self->{components}; | |||||||||||||||||||
| 58 | 18 | ▶ | if (! $h) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | $self->{components} = ($h = {}); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 18 | return $h; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | ▶ | sub home { $_[0]->{home} } | ||||||||||||||||||
|
Subroutine
home: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 65 | 15 | ▶ | sub objects { $_[0]->{objects} } | ||||||||||||||||||
|
Subroutine
objects: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 66 | 6 | ▶ | sub scoped_objects { $_[0]->{scoped_objects} } | ||||||||||||||||||
|
Subroutine
scoped_objects: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 23 | ▶ | sub register { | ||||||||||||||||||
|
Subroutine
register: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 69 | 23 | my ($self, $name, $component, $opts) = @_; | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 23 | ▶ | if (ref $component eq 'CODE') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 10 | ▶ | $opts ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$opts ||= {}
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||
| 73 | 10 | my %data = ( | |||||||||||||||||||
| 74 | %$opts, | ||||||||||||||||||||
| 75 | initializer => $component, | ||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||
| 77 | 10 | $self->components->{ $name } = \%data; | |||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||
| 79 | 13 | $self->objects->{$name} = $component; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 16 | ▶ | sub get { | ||||||||||||||||||
|
Subroutine
get: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||
| 84 | 16 | my ($self, $name, @args) = @_; | |||||||||||||||||||
| 85 | 16 | ▶ | my $object = $self->{objects}->{$name} || $self->{scoped_objects}->{$name}; | ||||||||||||||||||
|
Condition66%2/3
$self->{'objects'}{$name} || $self->{'scoped_objects'}{$name}
MC/DC50%1/2
$self->{'objects'}{$name} || $self->{'scoped_objects'}{$name}
$self->{'objects'}{$name}
$self->{'scoped_objects'}{$name}
Truth table66%2/3
A || B
A= $self->{'objects'}{$name}B= $self->{'scoped_objects'}{$name}
| |||||||||||||||||||||
| 86 | 16 | ▶ | if (! $object) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 8 | my $data = $self->components->{ $name }; | |||||||||||||||||||
| 88 | 8 | $object = $self->_construct_object($data, @args); | |||||||||||||||||||
| 89 | 8 | ▶ | if ($object) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 8 | ▶ | if ($data->{persistent}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | 2 | $self->objects->{$name} = $object; | |||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||
| 93 | 6 | $self->scoped_objects->{$name} = $object; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | 16 | return $object; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 8 | ▶ | sub _construct_object { | ||||||||||||||||||
|
Subroutine
_construct_object: called CC=2 SCAR=7.6
| |||||||||||||||||||||
| 101 | 8 | my ($self, $data, @args) = @_; | |||||||||||||||||||
| 102 | 8 | ▶ | if (! $data) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 0 | return (); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | 8 | $data->{initializer}->( $self, @args ); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 4 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||