| 1 | package Valiemon::Attributes::Dependencies; | ||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 2 | ▶ | use utf8; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 2 | ▶ | use parent qw(Valiemon::Attributes); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | |||||||||||||||||||
| 7 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 2 | ▶ | use List::MoreUtils qw(all any); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 27 | ▶ | sub attr_name { 'dependencies' } | ||||||||||||||||
|
Subroutine
attr_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | sub is_valid { | ||||||||||||||||||
| 13 | 27 | ▶ | my ($class, $context, $schema, $data) = @_; | ||||||||||||||||
|
Subroutine
is_valid: called CC=1 SCAR=0.1
Pod
undocumented
| |||||||||||||||||||
| 14 | $context->in_attr($class, sub { | ||||||||||||||||||
| 15 | 27 | ▶ | return 1 unless ref $data eq 'HASH'; # ignore | ||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=27.5
Branch: 1 branch
| |||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | 26 | my $dependencies = $schema->{dependencies}; | |||||||||||||||||
| 18 | 26 | ▶ | unless (ref $dependencies eq 'HASH') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 19 | 0 | croak sprintf '`dependencies` must be an object at %s', $context->position | |||||||||||||||||
| 20 | } | ||||||||||||||||||
| 21 | |||||||||||||||||||
| 22 | 26 | for my $name (keys %$dependencies) { | |||||||||||||||||
| 23 | 26 | my $subschema_or_propertyset = $dependencies->{$name}; | |||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | 26 | ▶ | if (ref $subschema_or_propertyset eq 'HASH') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 26 | # schema dependencies | ||||||||||||||||||
| 27 | 9 | my $sub_v = $context->sub_validator($subschema_or_propertyset); | |||||||||||||||||
| 28 | 9 | ▶ | next unless exists $data->{$name}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 29 | 8 | ▶ | next if $sub_v->validate($data); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 30 | 6 | return 0; | |||||||||||||||||
| 31 | } elsif (ref $subschema_or_propertyset eq 'ARRAY') { | ||||||||||||||||||
| 32 | # property dependencies | ||||||||||||||||||
| 33 | 16 | ▶ | unless ( scalar @$subschema_or_propertyset > 0 ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 34 | 0 | croak sprintf 'In case value of `dependencies` is an array, it must have at least one element at %s', $context->position; | |||||||||||||||||
| 35 | } | ||||||||||||||||||
| 36 | # assume all values are string | ||||||||||||||||||
| 37 | 16 | ▶ | unless ( all { $context->prims->is_string($_) } @$subschema_or_propertyset ) { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||
| 38 | 0 | croak sprintf 'All elements of value of `dependencies` must be a string at %s', $context->position; | |||||||||||||||||
| 39 | } | ||||||||||||||||||
| 40 | # assume are values are unique | ||||||||||||||||||
| 41 | 16 | ▶ | unless ( $class->check_all_uniqueness($context, $subschema_or_propertyset) ) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | 0 | croak sprintf 'All elements of value of `dependencies` must be unique at %s', $context->position; | |||||||||||||||||
| 43 | } | ||||||||||||||||||
| 44 | 16 | ▶ | next unless exists $data->{$name}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 45 | 10 | ▶ | next if all { exists $data->{$_} } @$subschema_or_propertyset; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||
| 46 | 7 | return 0; | |||||||||||||||||
| 47 | } elsif ( $context->prims->is_string($subschema_or_propertyset) ) { | ||||||||||||||||||
| 48 | # In draft 3, string was allowed (as a singly value of propertyset) | ||||||||||||||||||
| 49 | 1 | croak sprintf '`dependencies` member values can no longer be single strings at %s', $context->position; | |||||||||||||||||
| 50 | } else { | ||||||||||||||||||
| 51 | 0 | croak sprintf 'Invalid value of `dependencies` at %s', $context->position; | |||||||||||||||||
| 52 | } | ||||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | 12 | return 1; | |||||||||||||||||
| 55 | 27 | }); | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | sub check_all_uniqueness { | ||||||||||||||||||
| 59 | 16 | ▶ | my ($class, $context, $elements) = @_; | ||||||||||||||||
|
Subroutine
check_all_uniqueness: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | 16 | my $unique_elements = []; | |||||||||||||||||
| 62 | 16 | for my $elem (@$elements) { | |||||||||||||||||
| 63 | 28 | ▶ | return 0 if any { $context->prims->is_equal($_, $elem) } @$unique_elements; | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||
| 64 | 28 | push @$unique_elements, $elem; | |||||||||||||||||
| 65 | } | ||||||||||||||||||
| 66 | 16 | return 1; | |||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | |||||||||||||||||||
| 69 | 2 | 1; | |||||||||||||||||