Valiemon.pm

filter: Statementstmt 100% 66 / 66 Branchbran 85.0% 17 / 20 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 5 totaltotal 85.0% 102 / 120 CC 16 SCAR 13.5
CC
16
Cov
93%
validate
7.0
resolve_ref
6.1
new
3.1
CRAP 3.9
« Valiemon/Attributes/Properties.pm ↑ Summary Valiemon/Attributes/AdditionalItems.pm »
1 package Valiemon;
2 25 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Valiemon::Primitives;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶use Valiemon::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use Valiemon::Attributes qw(attr);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 25 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ro => [qw(schema options pos schema_cache)],
14 25 );
15  
16 25 our $VERSION = "0.05";
17  
18 590 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod undocumented
19 590 my ($class, $schema, $options) = @_;
20  
21 # TODO should validate own schema
22 590 ▶ if ($options->{validate_schema}) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"validate_schema"}) 0 590 590
23 590 ▶ croak 'schema must be a hashref' unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 590 590
24  
25 590 return bless {
26 schema => $schema,
27 options => $options,
28 schema_cache => +{},
29 }, $class;
30 }
31  
32 1055 ▶sub validate {
Subroutine validate: called CC=7 SCAR=19.5
Pod undocumented
33 1055 my ($self, $data, $context) = @_;
34 1055 my $schema = $self->schema;
35  
36 1055 ▶ $context //= Valiemon::Context->new($self, $schema);
Condition66%2/3
$context //= "Valiemon::Context"->new($self, $schema)
l!l&&r!l&&!r
3636920
MC/DC0%0/2
$context //= "Valiemon::Context"->new($self, $schema)
$context "Valiemon::Context"->new($self, $schema)
Truth table66%2/3
A //= B
A= $contextB= "Valiemon::Context"->new($self, $schema)
ABresult
1X1
011
000
37  
38 1055 for my $key (keys %{$schema}) {
39 1322 my $attr = attr($key);
40 1320 ▶ if ($attr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr) 1193 127 1320
41 1193 my ($is_valid, $error) = $attr->is_valid($context, $schema, $data);
42 1137 ▶ unless ($is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_valid) 382 755 1137
43 382 $error->set_detail(
44 expected => $schema,
45 actual => $data,
46 );
47 382 $context->push_error($error);
48 382 next;
49 }
50 }
51 }
52  
53 997 my $errors = $context->errors;
54 997 ▶ my $is_valid = scalar @$errors ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
scalar @$errors ? : 390 607 997
55 997 ▶ return wantarray ? ($is_valid, $errors->[0]) : $is_valid;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 622 375 997
56 }
57  
58 973 ▶sub prims {
Subroutine prims: called CC=2 SCAR=7.1
Pod undocumented
59 973 my ($self) = @_;
60 973 ▶ return $self->{prims} //= Valiemon::Primitives->new(
Condition66%2/3
$self->{"prims"} //= "Valiemon::Primitives"->new($self->options)
l!l&&r!l&&!r
7562170
MC/DC0%0/2
$self->{"prims"} //= "Valiemon::Primitives"->new($self->options)
$self->{"prims"} "Valiemon::Primitives"->new($self->options)
Truth table66%2/3
A //= B
A= $self->{"prims"}B= "Valiemon::Primitives"->new($self->options)
ABresult
1X1
011
000
61 $self->options
62 );
63 }
64  
65 86 ▶sub ref_schema_cache {
Subroutine ref_schema_cache: called CC=2 SCAR=6.9
Pod undocumented
66 86 my ($self, $ref, $schema) = @_;
67 return defined $schema
68 ? $self->schema_cache->{$ref} = $schema
69 86 ▶ : $self->{schema_cache}->{ref};
Branch: 1 branch
BranchTrueFalseTotal
defined $schema ? : 42 44 86
70 }
71  
72 58 ▶sub resolve_ref {
Subroutine resolve_ref: called CC=6 SCAR=18.0
Pod undocumented
73 58 my ($self, $ref) = @_;
74  
75 # TODO follow the standard referencing
76 58 ▶ unless ($ref =~ qr|^#/|) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref =~ qr"^#/"u) 14 44 58
77 14 croak 'This package support only single scope and `#/` referencing';
78 }
79  
80 44 ▶ return $self->ref_schema_cache($ref) || do {
Condition33%1/3
$self->ref_schema_cache($ref) || do { my $paths = do { my @p = split(m[/]u, $ref, 0); [splice(@p, 1)] }; my $sub_schema = $self->schema; { eval { do { $sub_schema = $sub_schema->{$_} foreach (@$paths) } }; croak(sprintf('referencing `%s` cause error', $ref)) if $@; croak(sprintf('schema `%s` not found', $ref)) unless $sub_schema; } ; $self->ref_schema_cache($ref, $sub_schema); $sub_schema }
l!l&&r!l&&!r
0440
MC/DC0%0/2
$self->ref_schema_cache($ref) || do { my $paths = do { my @p = split(m[/]u, $ref, 0); [splice(@p, 1)] }; my $sub_schema = $self->schema; { eval { do { $sub_schema = $sub_schema->{$_} foreach (@$paths) } }; croak(sprintf('referencing `%s` cause error', $ref)) if $@; croak(sprintf('schema `%s` not found', $ref)) unless $sub_schema; } ; $self->ref_schema_cache($ref, $sub_schema); $sub_schema }
$self->ref_schema_cache($ref) do { my $paths = do { my @p = split(m[/]u, $ref, 0); [splice(@p, 1)] }; my $sub_schema = $self->schema; { eval { do { $sub_schema = $sub_schema->{$_} foreach (@$paths) } }; croak(sprintf('referencing `%s` cause error', $ref)) if $@; croak(sprintf('schema `%s` not found', $ref)) unless $sub_schema; } ; $self->ref_schema_cache($ref, $sub_schema); $sub_schema }
Truth table33%1/3
A || B
A= $self->ref_schema_cache($ref)B= do { my $paths = do { my @p = split(m[/]u, $ref, 0); [splice(@p, 1)] }; my $sub_schema = $self->schema; { eval { do { $sub_schema = $sub_schema->{$_} foreach (@$paths) } }; croak(sprintf('referencing `%s` cause error', $ref)) if $@; croak(sprintf('schema `%s` not found', $ref)) unless $sub_schema; } ; $self->ref_schema_cache($ref, $sub_schema); $sub_schema }
ABresult
1X1
011
000
81 44 my $paths = do {
82 44 my @p = split '/', $ref;
83 44 [ splice @p, 1 ]; # remove '#'
84 };
85 44 my $sub_schema = $self->schema;
86 {
87 44 eval { $sub_schema = $sub_schema->{$_} for @$paths };
88 44 ▶ croak sprintf 'referencing `%s` cause error', $ref if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 42 44
89 42 ▶ croak sprintf 'schema `%s` not found', $ref unless $sub_schema;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub_schema 0 42 42
90 }
91 42 $self->ref_schema_cache($ref, $sub_schema); # caching
92 42 $sub_schema;
93 };
94 }
95  
96  
97 25 1;
98  
99 __END__
« Valiemon/Attributes/Properties.pm ↑ Summary Valiemon/Attributes/AdditionalItems.pm »