Bolts/Role/RootLocator.pm

filter: Statementstmt 91.3% 63 / 69 Branchbran 81.2% 26 / 32 Conditioncond 78.2% 18 / 23 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 84.7% 128 / 151 CC 25 SCAR 21.9
CC
25
Cov
86%
_get_from
16.6
acquire
8.0
acquire_all
5.2
CRAP 8.9
« Bolts/Util.pm ↑ Summary Bolts/Artifact.pm »
1 package Bolts::Role::RootLocator;
2 11 $Bolts::Role::RootLocator::VERSION = '0.143171';
3 # ABSTRACT: Interface for locating artifacts from some root bag
4  
5 11 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 with 'Bolts::Role::Locator';
8  
9 11 ▶use Bolts::Locator;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Bolts::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # TODO Rename root to something better.
17 11 requires 'root';
18  
19  
20 1029 ▶sub acquire {
Subroutine acquire: called CC=8 SCAR=20.8
Pod documented
21 1029 my ($self, @path) = @_;
22 # use Data::Dumper;
23 # Carp::cluck(Dumper(\@path));
24  
25 1029 my $options = {};
26 1029 ▶ if (@path > 1 and ref $path[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path > 1 and ref $path[-1]) 770 259 1029
Condition100%3/3
@path > 1 and ref $path[-1]
!ll&&!rl&&r
107152770
MC/DC100%2/2
@path > 1 and ref $path[-1]
@path > 1 ref $path[-1]
Truth table100%3/3
A and B
A= @path > 1B= ref $path[-1]
ABresult
0X0
100
111
27 770 $options = pop @path;
28 }
29
30 1029 my $current_path = '';
31  
32 1029 my $item = $self->root;
33 1029 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 1506 1022 2528
34 1506 my $component = shift @path;
35  
36 1506 my $bag = $item;
37 1506 $item = $self->_get_from($bag, $component, $current_path);
38 1502 $item = $self->resolve($bag, $item, $options);
39  
40 # If the $item is a locator, pass control of the process to that
41 1500 ▶ if (@path && $item->$_can('does') && $item->$_does('Bolts::Role::Locator')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path and $item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Locator")) 474 1026 1500
Condition100%6/6
@path and $item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Locator")
!ll&&!rl&&r
10251474
@path and $item->$Bolts::Role::RootLocator::_can("does")
!ll&&!rl&&r
10223475
MC/DC100%3/3
@path and $item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Locator")
@path $item->$Bolts::Role::RootLocator::_can("does") $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Locator")
Truth table100%4/4
A and B and C
A= @pathB= $item->$Bolts::Role::RootLocator::_can("does")C= $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Locator")
ABCresult
0XX0
10X0
1100
1111
42 474 $item->acquire(@path, $options);
43 }
44  
45 1499 ▶ $current_path .= ' ' if $current_path;
Branch: 1 branch
BranchTrueFalseTotal
if $current_path 473 1026 1499
46 1499 $current_path .= qq["$component"];
47 }
48  
49 1022 return $item;
50 }
51  
52  
53 63 ▶sub acquire_all {
Subroutine acquire_all: called CC=5 SCAR=16.5
Pod documented
54 63 my ($self, @path) = @_;
55  
56 63 my $options = {};
57 63 ▶ if (@path > 1 and ref $path[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path > 1 and ref $path[-1]) 0 63 63
Condition33%1/3
@path > 1 and ref $path[-1]
!ll&&!rl&&r
6300
MC/DC0%0/2
@path > 1 and ref $path[-1]
@path > 1 ref $path[-1]
Truth table33%1/3
A and B
A= @path > 1B= ref $path[-1]
ABresult
0X0
100
111
58 0 $options = pop @path;
59 }
60
61 63 my $bag = $self->acquire(@path, $options);
62 63 ▶ if ('ARRAY' eq ref $bag) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $bag) { } 59 4 63
elsif ('HASH' eq ref $bag) { } 2 2 4
63 return [
64 59 map { $self->resolve($bag, $_, $options) } @$bag
65 ];
66 }
67  
68 elsif ('HASH' eq ref $bag) {
69 return [
70 2 map { $self->resolve($bag, $_, $options) } values %$bag
71 ];
72 }
73  
74 else {
75 2 return [ $bag ];
76 }
77 }
78  
79  
80 1571 ▶sub resolve {
Subroutine resolve: called CC=3 SCAR=11.0
Pod documented
81 1571 my ($self, $bag, $item, $options) = @_;
82  
83 1571 ▶ return $item->get($bag, %$options)
Branch: 1 branch
BranchTrueFalseTotal
if $item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Artifact") 1036 535 1571
Condition100%3/3
$item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Artifact")
!ll&&!rl&&r
724631036
MC/DC100%2/2
$item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Artifact")
$item->$Bolts::Role::RootLocator::_can("does") $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Artifact")
Truth table100%3/3
A and B
A= $item->$Bolts::Role::RootLocator::_can("does")B= $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Artifact")
ABresult
0X0
100
111
84 if $item->$_can('does')
85 and $item->$_does('Bolts::Role::Artifact');
86  
87 535 return $item;
88 }
89  
90 1506 ▶sub _get_from {
Subroutine _get_from: called CC=12 SCAR=28.1
91 1506 my ($self, $bag, $component, $current_path) = @_;
92 1506 ▶ $current_path //= '';
Condition50%1/2
$current_path //= ""
l!l
15060
MC/DC0%0/1
$current_path //= ""
$current_path
Truth table50%1/2
A
A= $current_path
Aresult
11
00
93  
94 1506 ▶ Carp::croak("unable to acquire artifact for [$current_path]")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bag 0 1506 1506
95 unless defined $bag;
96  
97 # A bag can be any blessed object...
98 1506 ▶ if (Scalar::Util::blessed($bag)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($bag)) { } 1503 3 1506
elsif (ref $bag eq 'HASH') { } 1 2 3
elsif (ref $bag eq 'ARRAY') { } 2 0 2
99  
100 # If it is marked as an Opaque object, we can't locate within it
101 1503 ▶ if ($bag->can('does') && $bag->does('Bolts::Role::Opaque')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bag->can('does') and $bag->does('Bolts::Role::Opaque')) { } 1 1502 1503
elsif ($bag->can($component)) { } 1502 0 1502
Condition66%2/3
$bag->can("does") and $bag->does("Bolts::Role::Opaque")
!ll&&!rl&&r
015021
MC/DC50%1/2
$bag->can("does") and $bag->does("Bolts::Role::Opaque")
$bag->can("does") $bag->does("Bolts::Role::Opaque")
Truth table66%2/3
A and B
A= $bag->can("does")B= $bag->does("Bolts::Role::Opaque")
ABresult
0X0
100
111
102 1 Carp::croak(qq{may not examine "$component" at opaque path [$current_path]});
103 }
104  
105 # So long as it has that method
106 elsif ($bag->can($component)) {
107 1502 return $bag->$component;
108 }
109
110 # We don't know how to deal with it otherwise
111 else {
112 0 Carp::croak(qq{no artifact named "$component" at [$current_path]});
113 }
114 }
115  
116 # Or any unblessed hash
117 elsif (ref $bag eq 'HASH') {
118 1 ▶ if (exists $bag->{ $component }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $bag->{$component}) { } 0 1 1
119 0 return $bag->{ $component };
120 }
121 else {
122 1 Carp::croak(qq{no artifact keyed "$component" at [$current_path]});
123 }
124 }
125  
126 # Or any unblessed array
127 elsif (ref $bag eq 'ARRAY') {
128 2 ▶ if ($component =~ /^\d+$/ && @{ $bag } > $component) {
Branch: 1 branch
BranchTrueFalseTotal
if ($component =~ /^\d+$/ and @{$bag;} > $component) { } 0 2 2
Condition66%2/3
$component =~ /^\d+$/ and @{$bag;} > $component
!ll&&!rl&&r
110
MC/DC0%0/2
$component =~ /^\d+$/ and @{$bag;} > $component
$component =~ /^\d+$/ @{$bag;} > $component
Truth table66%2/3
A and B
A= $component =~ /^\d+$/B= @{$bag;} > $component
ABresult
0X0
100
111
129 0 return $bag->[ $component ];
130 }
131 else {
132 2 Carp::croak(qq{no artifact indexed "$component" at [$current_path]});
133 }
134 }
135  
136 # But nothing else...
137 else {
138 0 my $path = join ' ', grep defined, $current_path, $component;
139 0 Carp::croak(qq{not able to acquire artifact for [$path]});
140 }
141 }
142  
143 11 1;
144  
145 __END__
« Bolts/Util.pm ↑ Summary Bolts/Artifact.pm »