| 1 | package Valiemon::Context; | ||||||||||
| 2 | 25 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 25 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 25 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 25 | ▶ | use Valiemon::ValidationError; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 25 | ▶ | use constant ACCESSORS => [qw(root_validator root_schema errors positions)]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 25 | ▶ | use Class::Accessor::Lite ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 25 | ro => ACCESSORS(), | |||||||||
| 11 | 25 | ); | |||||||||
| 12 | 25 | ▶ | use Clone qw(clone); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 695 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 15 | 695 | my ($class, $validator, $schema) = @_; | |||||||||
| 16 | 695 | return bless { | |||||||||
| 17 | root_validator => $validator, | ||||||||||
| 18 | root_schema => $schema, | ||||||||||
| 19 | errors => [], | ||||||||||
| 20 | positions => [], | ||||||||||
| 21 | }, $class; | ||||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | 44 | ▶ | sub clone_from { | ||||||||
|
Subroutine
clone_from: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 44 | my ($class, $instance) = @_; | |||||||||
| 26 | return bless { | ||||||||||
| 27 | 44 | map { $_ => clone($instance->$_) } @{ACCESSORS()} | |||||||||
| 28 | }, $class; | ||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 408 | ▶ | sub rv { $_[0]->root_validator } | ||||||||
|
Subroutine
rv: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 32 | 0 | ▶ | sub rs { $_[0]->root_schema } | ||||||||
|
Subroutine
rs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 33 | |||||||||||
| 34 | 973 | ▶ | sub prims { $_[0]->root_validator->prims } # TODO refactor | ||||||||
|
Subroutine
prims: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 35 | |||||||||||
| 36 | 385 | ▶ | sub push_error { | ||||||||
|
Subroutine
push_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 37 | 385 | my ($self, $error) = @_; | |||||||||
| 38 | 385 | push @{$self->errors}, $error; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 1394 | ▶ | sub push_pos { | ||||||||
|
Subroutine
push_pos: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 42 | 1394 | my ($self, $pos) = @_; | |||||||||
| 43 | 1394 | push @{$self->positions}, $pos; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 1350 | ▶ | sub pop_pos { | ||||||||
|
Subroutine
pop_pos: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 47 | 1350 | my ($self) = @_; | |||||||||
| 48 | 1350 | pop @{$self->positions}; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | sub is_root { | ||||||||
|
Subroutine
is_root: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 52 | 0 | my ($self) = @_; | |||||||||
| 53 | 0 | ▶ | return scalar @{$self->positions} == 0 ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 400 | ▶ | sub position { | ||||||||
|
Subroutine
position: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 57 | 400 | my ($self) = @_; | |||||||||
| 58 | 400 | return '/' . join '/', @{$self->positions}; | |||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | 382 | ▶ | sub generate_error { | ||||||||
|
Subroutine
generate_error: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 62 | 382 | my ($self, $attr) = @_; | |||||||||
| 63 | 382 | return Valiemon::ValidationError->new($attr, $self->position); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 1109 | ▶ | sub in_attr ($&) { | ||||||||
|
Subroutine
in_attr: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 67 | 1109 | my ($self, $attr, $code) = @_; | |||||||||
| 68 | 1109 | $self->push_pos($attr->attr_name); | |||||||||
| 69 | 1109 | my $is_valid = $code->(); | |||||||||
| 70 | 1073 | ▶ | my @res = $is_valid ? (1, undef) : (0, $self->generate_error($attr)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1073 | $self->pop_pos(); | |||||||||
| 72 | 1073 | return @res; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 285 | ▶ | sub in ($&) { | ||||||||
|
Subroutine
in: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 76 | 285 | my ($self, $pos, $code) = @_; | |||||||||
| 77 | 285 | $self->push_pos($pos); | |||||||||
| 78 | 285 | my $res = $code->(); | |||||||||
| 79 | 277 | $self->pop_pos(); | |||||||||
| 80 | 277 | return $res; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 351 | ▶ | sub sub_validator { | ||||||||
|
Subroutine
sub_validator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 84 | 351 | my ($self, $sub_schema) = @_; | |||||||||
| 85 | 351 | require Valiemon; | |||||||||
| 86 | 351 | return Valiemon->new( | |||||||||
| 87 | $sub_schema, | ||||||||||
| 88 | $self->rv->options, # inherit options | ||||||||||
| 89 | ); | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 25 | 1; | |||||||||