| 1 | package Data::DynamicValidator::Path; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Class represents "splitted" to labelled componets path. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 11 | $Data::DynamicValidator::Path::VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Scalar::Util qw/looks_like_number/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use overload fallback => 1, q/""/ => sub { $_[0]->to_string }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use constant DEBUG => $ENV{DATA_DYNAMICVALIDATOR_DEBUG} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'DATA_DYNAMICVALIDATOR_DEBUG'} || 0
MC/DC0%0/1
$ENV{'DATA_DYNAMICVALIDATOR_DEBUG'} || 0
$ENV{'DATA_DYNAMICVALIDATOR_DEBUG'}
Truth table50%1/2
A
A= $ENV{'DATA_DYNAMICVALIDATOR_DEBUG'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 89 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 89 | my ($class, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 89 | my $self = { }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 89 | bless $self => $class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 89 | $self->_build_components($path); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 89 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 89 | ▶ | sub _build_components { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_components: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 89 | my ($self, $path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # handle escaped path components | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 89 | $_ = $path; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # substitute all '/'-symbols to "`" and strip | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # surrounding(wraping) ` | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 89 | s[`(.+?)`][my $x=$1;$x=~ tr{/}{`};$x]ge; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 89 | my @elements = split '/'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 89 | for(@elements) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 293 | tr {`}{/}; # roll back slashes again | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 89 | for my $i (0..@elements-1) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 293 | my @parts = split(':', $elements[$i]); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 293 | ▶ | if (@parts > 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 40 | $elements[$i] = $parts[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 40 | $self->{_labels}->{ $parts[0] } = $i; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # special name _ for the last route component | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 89 | $self->{_labels}->{'_'} = @elements-1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 89 | $self->{_components} = \@elements; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 296 | ▶ | sub components { shift->{_components} } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
components: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 151 | ▶ | sub to_string { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | join('/', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 500 | ▶ | map { /\// ? "`$_`" : $_ } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 151 | @{ shift->{_components} }) | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 24 | ▶ | sub labels { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 24 | my $labels = $self->{_labels}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 24 | sort { $labels->{$a} <=> $labels->{$b} } grep { $_ ne '_' } keys %$labels; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 6 | ▶ | sub named_route { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
named_route: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | my ($self, $label) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | croak("No label '$label' in path '$self'") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | ▶ | unless exists $self->{_labels}->{$label}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 6 | return $self->_clone_to($self->{_labels}->{$label}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 38 | ▶ | sub named_component { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
named_component: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 38 | my ($self, $label) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | croak("No label '$label' in path '$self'") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 38 | ▶ | unless exists $self->{_labels}->{$label}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 38 | my $idx = $self->{_labels}->{$label}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 38 | return $self->{_components}->[$idx]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | ▶ | sub _clone_to { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clone_to: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 6 | my ($self, $index) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 6 | my @components; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 6 | for my $i (0 .. $index) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 19 | push @components, $self->{_components}->[$i] | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | ▶ | while ( my ($name, $idx) = each(%{ $self->{_labels} })) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 20 | ▶ | $components[$idx] = join(':', $name, $components[$idx]) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$idx <= $index and $name ne "_"
MC/DC100%2/2
$idx <= $index and $name ne "_"
$idx <= $index
$name ne "_"
Truth table100%3/3
A and B
A= $idx <= $indexB= $name ne "_"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | if( $idx <= $index && $name ne '_'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | my $path = join('/', @components); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 6 | return Data::DynamicValidator::Path->new($path); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 98 | ▶ | sub value { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
value: called CC=11 SCAR=26.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 98 | my ($self, $data, $label) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 98 | ▶ | $label //= '_'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$label //= "_"
MC/DC100%1/1
$label //= "_"
$label
Truth table100%2/2
A
A= $label
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | croak("No label '$label' in path '$self'") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 98 | ▶ | if(!exists $self->{_labels}->{$label}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 98 | my $idx = $self->{_labels}->{$label}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 98 | my $value = $data; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 98 | for my $i (1 .. $idx) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 224 | my $element = $self->{_components}->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 224 | ▶ | if (ref($value) && ref($value) eq 'HASH' && exists $value->{$element}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%4/6
ref $value and ref $value eq "HASH" and exists $value->{$element}
ref $value and ref $value eq "HASH"
MC/DC33%1/3
ref $value and ref $value eq "HASH" and exists $value->{$element}
ref $value
ref $value eq "HASH"
exists $value->{$element}
Truth table50%2/4
A and B and C
A= ref $valueB= ref $value eq "HASH"C= exists $value->{$element}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 150 | $value = $value->{$element}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 150 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | ▶ | elsif (ref($value) && ref($value) eq 'ARRAY' | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%3/9
ref $value and ref $value eq "ARRAY" and &looks_like_number($element) and $element < @$value
ref $value and ref $value eq "ARRAY" and &looks_like_number($element)
ref $value and ref $value eq "ARRAY"
MC/DC0%0/4
ref $value and ref $value eq "ARRAY" and &looks_like_number($element) and $element < @$value
ref $value
ref $value eq "ARRAY"
&looks_like_number($element)
$element < @$value
Truth table20%1/5
A and B and C and D
A= ref $valueB= ref $value eq "ARRAY"C= &looks_like_number($element)D= $element < @$value
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | && looks_like_number($element) && $element < @$value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 74 | $value = $value->[$element]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 74 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | croak "I don't know how to get element#$i ($element) at $self"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 98 | if (DEBUG) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | warn "-- value for $self is " | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | . (defined($value)? $value : 'undefined') . "\n" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 98 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||