| 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
Condition100%3/3
@path > 1 and ref $path[-1]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
@path and $item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Locator")
@path and $item->$Bolts::Role::RootLocator::_can("does")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 474 | $item->acquire(@path, $options); | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1499 | ▶ | $current_path .= ' ' if $current_path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
@path > 1 and ref $path[-1]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Artifact")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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 //= ""
MC/DC0%0/1
$current_path //= ""
$current_path
Truth table50%1/2
A
A= $current_path
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1506 | ▶ | Carp::croak("unable to acquire artifact for [$current_path]") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | unless defined $bag; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | # A bag can be any blessed object... | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 1506 | ▶ | if (Scalar::Util::blessed($bag)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$bag->can("does") and $bag->does("Bolts::Role::Opaque")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$component =~ /^\d+$/ and @{$bag;} > $component
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||