| 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.37'; | |||||||||||||||||||||||||||||||||||||||||
| 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 | 43 | ▶ | sub get_root_container { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_root_container: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 43 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 43 | ▶ | while ($c->has_parent) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 33 | $c = $c->parent; | |||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 43 | return $c; | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 373 | ▶ | sub fetch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 373 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 373 | my $root; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 373 | ▶ | if ($path =~ /^\//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 14 | my $c = $root; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 14 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $path[0] and $path[0] eq ".." and $c->has_parent
defined $path[0] and $path[0] eq ".."
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
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 16 | shift @path; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 16 | ▶ | $c = $c->parent | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$c->parent || confess('Expected parent for ' . $c->name . ' but found none')
MC/DC0%0/1
$c->parent || confess('Expected parent for ' . $c->name . ' but found none')
$c->parent
Truth table50%1/2
A
A= $c->parent
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 371 | my $c = $root; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 371 | ▶ | while (my $h = shift @path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 467 | $c = _get_container_or_service($c, $h); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 368 | ▶ | if (!$self->isa('Bread::Board::Service::Alias')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 345 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 345 | ▶ | while ($c->isa('Bread::Board::Service::Alias')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 48 | $c = $c->aliased_from; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 47 | ▶ | confess "Cycle detected in aliases" if exists $seen{$c}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 41 | $seen{$c}++; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 361 | return $c; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 467 | ▶ | sub _get_container_or_service { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_container_or_service: called CC=11 SCAR=26.5
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 467 | my ($c, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 467 | ▶ | (blessed $c) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | || confess "Expected object, got $c"; | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 467 | ▶ | if ($c->isa('Bread::Board::Dependency')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 35 | ▶ | return $c->get_dependency($name) if $c->has_dependency($name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ($c->name eq $name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 345 | ▶ | return $c->get_service($name) if $c->has_service($name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||