Board/Traversable.pm

filter: Statementstmt 91.3% 53 / 58 Branchbran 85.0% 34 / 40 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 85.7% 102 / 119 SCAR 32.3
CC
24
Cov
87%
_get_container_or_service
14.2
fetch
13.0
get_root_container
2.0
CRAP 25.3
« Board/Service/Inferred.pm ↑ Summary Board/Service.pm »
1 package Bread::Board::Traversable;
2 81 our $AUTHORITY = 'cpan:STEVAN';
3 # ABSTRACT: role for traversing a container service tree
4 81 $Bread::Board::Traversable::VERSION = '0.35';
5 81 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 81 with 'MooseX::Clone' => { -version => 0.05 };
8  
9 81 has 'parent' => (
10 is => 'rw',
11 isa => 'Bread::Board::Traversable',
12 weak_ref => 1,
13 clearer => 'detach_from_parent',
14 predicate => 'has_parent',
15 );
16  
17 sub get_root_container {
18 43 ▶ my $c = shift;
Subroutine get_root_container: called CC=2 SCAR=6.9
Pod documented
19 43 ▶ while ($c->has_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->has_parent) 33 43 76
20 33 $c = $c->parent;
21 }
22 43 return $c;
23 }
24  
25 sub fetch {
26 373 ▶ my ($self, $path) = @_;
Subroutine fetch: called CC=13 SCAR=25.7
Pod documented
27  
28 373 my $root;
29 373 ▶ if ($path =~ /^\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^/]) { } 43 330 373
30 43 $root = $self->get_root_container;
31 }
32 else {
33 330 $root = $self;
34 330 ▶ while (!$root->isa('Bread::Board::Container')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($root->isa("Bread::Board::Container")) 228 330 558
35 228 $root = $root->parent;
36 }
37 }
38  
39 373 my @path = grep { $_ } split /\// => $path;
40  
41 373 ▶ if ($path[0] eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path[0] eq "..") 14 359 373
42 14 my $c = $root;
43 14 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined $path[0] and $path[0] eq ".." and $c->has_parent and do { do { shift @path; $c = $c->parent || confess('Expected parent for ' . $c->name . ' but found none') } } 2 14 16
Condition83%5/6
defined $path[0] and $path[0] eq ".." and $c->has_parent
!ll&&!rl&&r
1402
defined $path[0] and $path[0] eq ".."
!ll&&!rl&&r
2122
MC/DC66%2/3
defined $path[0] and $path[0] eq ".." and $c->has_parent
defined $path[0] $path[0] eq ".." $c->has_parent
Truth table75%3/4
A and B and C
A= defined $path[0]B= $path[0] eq ".."C= $c->has_parent
ABCresult
0XX0
10X0
1100
1111
44 16 shift @path;
45 16 ▶ $c = $c->parent
Condition33%1/3
$c->parent || confess('Expected parent for ' . $c->name . ' but found none')
l!l&&r!l&&!r
1600
MC/DC0%0/2
$c->parent || confess('Expected parent for ' . $c->name . ' but found none')
$c->parent confess('Expected parent for ' . $c->name . ' but found none')
Truth table33%1/3
A || B
A= $c->parentB= confess('Expected parent for ' . $c->name . ' but found none')
ABresult
1X1
011
000
46 || confess "Expected parent for " . $c->name . " but found none";
47 } while (defined $path[0] && $path[0] eq '..' && $c->has_parent);
48 14 $root = $c;
49 }
50  
51 373 ▶ return $root unless @path;
Branch: 1 branch
BranchTrueFalseTotal
unless @path 2 371 373
52  
53 371 my $c = $root;
54 371 ▶ while (my $h = shift @path) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = shift @path) 467 368 835
55 467 $c = _get_container_or_service($c, $h);
56 }
57 368 ▶ if (!$self->isa('Bread::Board::Service::Alias')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->isa("Bread::Board::Service::Alias")) 345 23 368
58 345 my %seen;
59 345 ▶ while ($c->isa('Bread::Board::Service::Alias')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->isa("Bread::Board::Service::Alias")) 48 338 386
60 48 $c = $c->aliased_from;
61 47 ▶ confess "Cycle detected in aliases" if exists $seen{$c};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen{$c} 6 41 47
62 41 $seen{$c}++;
63 }
64 }
65 361 return $c;
66 }
67  
68 sub _get_container_or_service {
69 467 ▶ my ($c, $name) = @_;
Subroutine _get_container_or_service: called CC=11 SCAR=26.5
70  
71 467 ▶ (blessed $c)
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($c) or confess("Expected object, got $c") 0 467 467
72 || confess "Expected object, got $c";
73  
74 467 ▶ if ($c->isa('Bread::Board::Dependency')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->isa("Bread::Board::Dependency")) 0 467 467
75 # make sure to evaluate this from the parent
76 0 return _get_container_or_service($c->parent->parent, $name);
77 }
78  
79 467 ▶ if ($c->does('Bread::Board::Service::WithDependencies')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->does("Bread::Board::Service::WithDependencies")) 35 432 467
80 35 ▶ return $c->get_dependency($name) if $c->has_dependency($name);
Branch: 1 branch
BranchTrueFalseTotal
if $c->has_dependency($name) 35 0 35
81 0 confess "Could not find dependency ($name) from service " . $c->name;
82 }
83  
84 # name() is implemented in Service and Container
85 # get_sub_container and get_service is implemented in Container
86 # there must be a better way to do this
87  
88 432 ▶ if ($c->does('Bread::Board::Service')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($c->does('Bread::Board::Service')) { } 0 432 432
elsif ($c->isa('Bread::Board::Container')) { } 431 1 432
89 0 ▶ if ($c->name eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->name eq $name) 0 0 0
90 0 warn "Traversing into the current service ($name) is deprecated."
91 . " You should remove the $name component from the path.";
92 0 return $c;
93 }
94 }
95 elsif ($c->isa('Bread::Board::Container')) {
96 431 ▶ if ($c->name eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->name eq $name) 1 430 431
97 1 warn "Traversing into the current container ($name) is deprecated;"
98 . " you should remove the $name component from the path";
99 1 return $c;
100 }
101 430 ▶ return $c->get_sub_container($name) if $c->has_sub_container($name);
Branch: 1 branch
BranchTrueFalseTotal
if $c->has_sub_container($name) 85 345 430
102 345 ▶ return $c->get_service($name) if $c->has_service($name);
Branch: 1 branch
BranchTrueFalseTotal
if $c->has_service($name) 343 2 345
103 }
104  
105 3 confess "Could not find container or service for $name in " . $c->name;
106 }
107  
108 81 ▶no Moose::Role; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
109  
110 __END__
111  
112 =pod
113  
114 =encoding UTF-8
115  
116 =head1 NAME
117  
118 Bread::Board::Traversable - role for traversing a container service tree
119  
120 =head1 VERSION
121  
122 version 0.35
123  
124 =head1 SYNOPSIS
125  
126 my $service = $container->fetch('/some/service/path');
127  
128 my $root = $service->get_root_container;
129  
130 =head1 DESCRIPTION
131  
132 This role provides the basic functionality to traverse a container /
133 service tree. Instances of classes consuming this role will get a
134 parent-child relationship between them.
135  
136 =head1 ATTRIBUTES
137  
138 =head2 C<parent>
139  
140 Weak ref to another L<Bread::Board::Traversable> object, read/write
141 accessor (although you should probably not change this value directly
142 in normal code).
143  
144 =head1 METHODS
145  
146 =head2 C<has_parent>
147  
148 Predicate for the L</parent> attribute, true if a parent has been set.
149  
150 =head2 C<detach_from_parent>
151  
152 Clearer for the L</parent> attribute, you should probably not call
153 this method in normal code.
154  
155 =head2 C<get_root_container>
156  
157 Returns the farthest ancestor of the invocant, i.e. the top-most
158 container this object is a part of.
159  
160 =head2 C<fetch>
161  
162 my $service = $this->fetch('/absolute/path');
163 my $service = $this->fetch('relative/path');
164 my $service = $this->fetch('../relative/path');
165  
166 Given a (relative or absolute) path to a service or container, this
167 method walks the tree and returns the L<Bread::Board::Service> or
168 L<Bread::Board::Container> instance for that path. Dies if no object
169 can be found for the given
170 path.
171  
172 L<Aliases|Bread::Board::Service::Alias> are resolved in this call, by
173 calling L<< C<aliased_from>|Bread::Board::Service::Alias/aliased_from
174 >> until we get an actual service.
175  
176 =head1 AUTHOR
177  
178 Stevan Little <stevan@iinteractive.com>
179  
180 =head1 BUGS
181  
182 Please report any bugs or feature requests on the bugtracker website
183 https://github.com/stevan/BreadBoard/issues
184  
185 When submitting a bug or request, please include a test-file or a
186 patch to an existing test-file that illustrates the bug or desired
187 feature.
188  
189 =head1 COPYRIGHT AND LICENSE
190  
191 This software is copyright (c) 2017, 2016, 2015, 2014, 2013, 2011, 2009 by Infinity Interactive.
192  
193 This is free software; you can redistribute it and/or modify it under
194 the same terms as the Perl 5 programming language system itself.
195  
196 =cut
« Board/Service/Inferred.pm ↑ Summary Board/Service.pm »