| 1 | package Data::DynamicValidator::Filter; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Class responds for filtering data paths | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 11 | $Data::DynamicValidator::Filter::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 Eval::Closure; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 13 | 11 | ▶ | fallback => 1, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 94 | ▶ | '&{}' => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 94 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 94 | ▶ | return sub { $self->filter(@_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 11 | }; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 25 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 25 | my ($class, $condition) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 25 | my $self = { | |||||||||||||||||||||||||||||||||||||||||
| 22 | _condition => $condition | ||||||||||||||||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 25 | bless $self => $class; | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 94 | ▶ | sub _prepare { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepare: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 94 | my ($self, $data, $context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 29 | 94 | my %environment; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 94 | my $c = $self->{_condition}; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 94 | my $type = ref($data); | |||||||||||||||||||||||||||||||||||||||||
| 32 | 94 | ▶ | if ($c =~ /\bsize\b/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 20 | my $value; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 20 | ▶ | if ($type eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | ▶ | $value = sub { scalar keys %$data }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | } elsif ($type eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 9 | ▶ | $value = sub { scalar @$data }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 20 | ▶ | if ($value) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 12 | my $val = $value->(); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 12 | $environment{'$size'} = \$val; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 94 | ▶ | if ($c =~ /\bvalue\b/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 30 | ▶ | $environment{'$value'} = \$data if(defined($data)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 94 | ▶ | if ($c =~ /\bindex\b/ && ref($context) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$c =~ /\bindex\b/ and ref $context eq "HASH" and defined $context->{"index"}
$c =~ /\bindex\b/ and ref $context eq "HASH"
MC/DC100%3/3
$c =~ /\bindex\b/ and ref $context eq "HASH" and defined $context->{"index"}
$c =~ /\bindex\b/
ref $context eq "HASH"
defined $context->{"index"}
Truth table100%4/4
A and B and C
A= $c =~ /\bindex\b/B= ref $context eq "HASH"C= defined $context->{"index"}
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | && defined($context->{index}) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 9 | $environment{'$index'} = \$context->{index}; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 94 | ▶ | if ($c =~ /\bkey\b/ && ref($context) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$c =~ /\bkey\b/ and ref $context eq "HASH" and defined $context->{"key"}
$c =~ /\bkey\b/ and ref $context eq "HASH"
MC/DC100%3/3
$c =~ /\bkey\b/ and ref $context eq "HASH" and defined $context->{"key"}
$c =~ /\bkey\b/
ref $context eq "HASH"
defined $context->{"key"}
Truth table100%4/4
A and B and C
A= $c =~ /\bkey\b/B= ref $context eq "HASH"C= defined $context->{"key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | && defined($context->{key}) ) { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 31 | $environment{'$key'} = \$context->{key}; | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # substitute to environment values in condition | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 94 | for (keys %environment) { | |||||||||||||||||||||||||||||||||||||||||
| 57 | 80 | s/^\$//; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 80 | $c =~ s/\b($_)\b/\$$1/; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 94 | return ($c, \%environment); | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 94 | ▶ | sub filter { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 94 | my ($self, $data, $context) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 94 | my ($c, $environment) = $self->_prepare($data, $context); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 94 | my $source = "sub { $c }"; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 94 | warn "-- evaluating condition as source: $source \n" if(DEBUG); | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | # evaluation phase | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 94 | my $result = 0; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 94 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 94 | my $code = eval_closure( | |||||||||||||||||||||||||||||||||||||||||
| 74 | source => $source, | ||||||||||||||||||||||||||||||||||||||||||
| 75 | environment => $environment, | ||||||||||||||||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 80 | $result = $code->(); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 14 | ▶ | } catch { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 14 | warn "-- exection has been thrown, considering false: $_ \n" if(DEBUG); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 94 | }; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 94 | warn "-- result: $result: \n" if(DEBUG); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 94 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||||||||||||||||