DynamicValidator/Filter.pm

filter: Statementstmt 100% 61 / 61 Branchbran 100% 16 / 16 Conditioncond 92.8% 13 / 14 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 2 totaltotal 96.5% 111 / 115 CC 15 SCAR 19.9
CC
15
Cov
99%
_prepare
14.0
BEGIN
2.0
BEGIN
1.0
CRAP 7.3
« DynamicValidator.pm ↑ Summary DynamicValidator/Path.pm »
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
l!l
011
MC/DC0%0/1
$ENV{'DATA_DYNAMICVALIDATOR_DEBUG'} || 0
$ENV{'DATA_DYNAMICVALIDATOR_DEBUG'}
Truth table50%1/2
A
A= $ENV{'DATA_DYNAMICVALIDATOR_DEBUG'}
Aresult
11
00
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
BranchTrueFalseTotal
if ($c =~ /\bsize\b/) 20 74 94
33 20 my $value;
34 20 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 3 17 20
elsif ($type eq 'ARRAY') { } 9 8 17
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
BranchTrueFalseTotal
if ($value) 12 8 20
40 12 my $val = $value->();
41 12 $environment{'$size'} = \$val;
42 }
43 }
44 94 ▶ if ($c =~ /\bvalue\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\bvalue\b/) 30 64 94
45 30 ▶ $environment{'$value'} = \$data if(defined($data));
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 28 2 30
46 }
47 94 ▶ if ($c =~ /\bindex\b/ && ref($context) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\bindex\b/ and ref $context eq "HASH" and defined $context->{"index"}) 9 85 94
Condition100%6/6
$c =~ /\bindex\b/ and ref $context eq "HASH" and defined $context->{"index"}
!ll&&!rl&&r
8419
$c =~ /\bindex\b/ and ref $context eq "HASH"
!ll&&!rl&&r
83110
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"}
ABCresult
0XX0
10X0
1100
1111
48 && defined($context->{index}) ) {
49 9 $environment{'$index'} = \$context->{index};
50 }
51 94 ▶ if ($c =~ /\bkey\b/ && ref($context) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /\bkey\b/ and ref $context eq "HASH" and defined $context->{"key"}) 31 63 94
Condition100%6/6
$c =~ /\bkey\b/ and ref $context eq "HASH" and defined $context->{"key"}
!ll&&!rl&&r
62131
$c =~ /\bkey\b/ and ref $context eq "HASH"
!ll&&!rl&&r
61132
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"}
ABCresult
0XX0
10X0
1100
1111
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__
« DynamicValidator.pm ↑ Summary DynamicValidator/Path.pm »