Bolts/Role/RootLocator.pm

filter: Statementstmt 89.8% 62 / 69 Branchbran 76.6% 23 / 30 Conditioncond 82.6% 19 / 23 MC/DCmcdc 64.2% 9 / 14 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 84.7% 128 / 151 SCAR 32.5
CC
24
Cov
85%
_get_from
15.6
acquire
8.0
acquire_all
4.8
CRAP 25.8
« Bolts/Util.pm ↑ Summary Bolts/Artifact.pm »
1 package Bolts::Role::RootLocator;
2 10 $Bolts::Role::RootLocator::VERSION = '0.143170';
3 # ABSTRACT: Interface for locating artifacts from some root bag
4  
5 10 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 with 'Bolts::Role::Locator';
8  
9 10 ▶use Bolts::Locator;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Bolts::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # TODO Rename root to something better.
17 10 requires 'root';
18  
19  
20 sub acquire {
21 1012 ▶ my ($self, @path) = @_;
Subroutine acquire: called CC=8 SCAR=20.8
Pod documented
22 # use Data::Dumper;
23 # Carp::cluck(Dumper(\@path));
24  
25 1012 my $options = {};
26 1012 ▶ if (@path > 1 and ref $path[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path > 1 and ref $path[-1]) 698 314 1012
Condition100%3/3
@path > 1 and ref $path[-1]
!ll&&!rl&&r
165149698
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 698 $options = pop @path;
28 }
29
30 1012 my $current_path = '';
31  
32 1012 my $item = $self->root;
33 1012 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 1483 1005 2488
34 1483 my $component = shift @path;
35  
36 1483 my $bag = $item;
37 1483 $item = $self->_get_from($bag, $component, $current_path);
38 1479 $item = $self->resolve($bag, $item, $options);
39  
40 # If the $item is a locator, pass control of the process to that
41 1477 ▶ 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")) 468 1009 1477
Condition100%6/6
@path and $item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Locator")
!ll&&!rl&&r
10081468
@path and $item->$Bolts::Role::RootLocator::_can("does")
!ll&&!rl&&r
10053469
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 468 $item->acquire(@path, $options);
43 }
44  
45 1476 ▶ $current_path .= ' ' if $current_path;
Branch: 1 branch
BranchTrueFalseTotal
if $current_path 467 1009 1476
46 1476 $current_path .= qq["$component"];
47 }
48  
49 1005 return $item;
50 }
51  
52  
53 sub acquire_all {
54 57 ▶ my ($self, @path) = @_;
Subroutine acquire_all: called CC=4 SCAR=15.8
Pod documented
55  
56 57 my $options = {};
57 57 ▶ if (@path > 1 and ref $path[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path > 1 and ref $path[-1]) 0 57 57
Condition33%1/3
@path > 1 and ref $path[-1]
!ll&&!rl&&r
5700
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 57 my $bag = $self->acquire(@path);
62 57 ▶ if (ref $bag eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bag eq 'ARRAY') { } 57 0 57
63 return [
64 57 map { $self->resolve($bag, $_, $options) } @$bag
65 ];
66 }
67  
68 else {
69 0 return [];
70 }
71 }
72  
73  
74 sub resolve {
75 1536 ▶ my ($self, $bag, $item, $options) = @_;
Subroutine resolve: called CC=3 SCAR=11.0
Pod documented
76  
77 1536 ▶ 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") 1019 517 1536
Condition100%3/3
$item->$Bolts::Role::RootLocator::_can("does") and $item->$Bolts::Role::RootLocator::_does("Bolts::Role::Artifact")
!ll&&!rl&&r
604571019
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
78 if $item->$_can('does')
79 and $item->$_does('Bolts::Role::Artifact');
80  
81 517 return $item;
82 }
83  
84  
85 sub get {
86 1 ▶ my ($self, $component) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
87 1 return $self->_get_from($self->root, $component);
88 }
89  
90 sub _get_from {
91 1484 ▶ my ($self, $bag, $component, $current_path) = @_;
Subroutine _get_from: called CC=12 SCAR=27.5
92 1484 ▶ $current_path //= '';
Condition100%2/2
$current_path //= ""
l!l
14831
MC/DC100%1/1
$current_path //= ""
$current_path
Truth table100%2/2
A
A= $current_path
Aresult
11
00
93  
94 1484 ▶ Carp::croak("unable to acquire artifact for [$current_path]")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bag 0 1484 1484
95 unless defined $bag;
96  
97 # A bag can be any blessed object...
98 1484 ▶ if (Scalar::Util::blessed($bag)) {
Branch: 3 branches
BranchTrueFalseTotal
if (builtin::blessed($bag)) { } 1481 3 1484
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 1481 ▶ if ($bag->can('does') && $bag->does('Bolts::Role::Opaque')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bag->can('does') and $bag->does('Bolts::Role::Opaque')) { } 1 1480 1481
elsif ($bag->can($component)) { } 1480 0 1480
Condition66%2/3
$bag->can("does") and $bag->does("Bolts::Role::Opaque")
!ll&&!rl&&r
014801
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 1480 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 10 1;
144  
145 __END__
146  
147 =pod
148  
149 =encoding UTF-8
150  
151 =head1 NAME
152  
153 Bolts::Role::RootLocator - Interface for locating artifacts from some root bag
154  
155 =head1 VERSION
156  
157 version 0.143170
158  
159 =head1 DESCRIPTION
160  
161 This is the interface that any locator must implement. A locator's primary job is to provide a way to find artifacts within a bag or selection of bags. This performs the acquisition and resolution process. This actually also implements everything needed for the process except for the L</root>.
162  
163 =head1 ROLES
164  
165 =over
166  
167 =item *
168  
169 L<Bolts::Role::Locator>
170  
171 =back
172  
173 =head1 REQUIRED METHODS
174  
175 =head2 root
176  
177 This is the object to use as the bag to start searching. It may be an object, a reference to an array, or a reference to a hash.
178  
179 B<Caution:> This will be renamed in the future.
180  
181 =head1 METHODS
182  
183 =head2 acquire
184  
185 my $artifact = $loc->acquire(@path, \%options);
186  
187 Given a C<@path> of symbol names to traverse, this goes through each artifact in turn, resolves it, if necessary, and then continues to the next path component.
188  
189 After it finds the artifact, it will resolve the artifact using the L</resolve> method, which is passed the (optional) B<%options>.
190  
191 When complete, the complete, resolved artifact found is returned.
192  
193 Acquisition in this implementation proceeds according to the following rules:
194  
195 =over
196  
197 =item 1.
198  
199 If the bag is an object:
200  
201 =over
202  
203 =item *
204  
205 If the object implements the L<Bolts::Role::Opaque> role, lookup will end in an error.
206  
207 =item *
208  
209 If the object implements the L<Bolts::Role::Locator> role, control of the lookup for the remaining components in C<@path> will pass over to that object.
210  
211 =item *
212  
213 If the C<can> method on the object returns false for the next component in C<@path>, lookup will end in an error.
214  
215 =item *
216  
217 Finally, the method named in the next component in C<@path> will be called and the result used as either the value to resolve or the next bag to locate within (repeating this process).
218  
219 =back
220  
221 =item 2.
222  
223 If the bag is a hash, the name of the next component in C<@path> will be used as a key in the hash to find the next value to resolve or the next bag to locate within.
224  
225 =item 3.
226  
227 If the bag is an array and the name of the next component in C<@path> is a number, it will be used as the index to fetch from the array to use as the value to resolve or the next bag to locate within.
228  
229 =item 4.
230  
231 Anything else will result in lookup ending in an error.
232  
233 =back
234  
235 =head2 acquire_all
236  
237 my @artifacts = @{ $loc->acquire_all(\@path) };
238  
239 This is similar to L<acquire>, but if the last bag is a reference to an array, then all the artifacts within that bag are acquired, resolved, and returned as a reference to an array.
240  
241 If the last item found at the path is not an array, it returns an empty list.
242  
243 =head2 resolve
244  
245 my $resolved_artifact = $loc->resolve($bag, $artifact, \%options);
246  
247 After the artifact has been found, this method resolves the a partial artifact implementing the L<Bolts::Role::Artifact> and turns it into the complete artifact.
248  
249 =head2 get
250  
251 my $artifact = $log->get($component);
252  
253 Given a single symbol name as the path component to find during acquisition it returns the partial artifact for it. This artifact is incomplete and still needs to be resolved.
254  
255 =head1 AUTHOR
256  
257 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
258  
259 =head1 COPYRIGHT AND LICENSE
260  
261 This software is copyright (c) 2014 by Qubling Software LLC.
262  
263 This is free software; you can redistribute it and/or modify it under
264 the same terms as the Perl 5 programming language system itself.
265  
266 =cut
« Bolts/Util.pm ↑ Summary Bolts/Artifact.pm »