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