| 1 | package Data::Verifier::Nested; | ||||||||||||||||||||
| 2 | 1 | $Data::Verifier::Nested::VERSION = '0.66'; | |||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | # ABSTRACT: Nested profile based data verification with Moose type constraints. | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | extends 'Data::Verifier'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | ## private helper functions | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 5 | ▶ | my $_is_profile_spec = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||
| 13 | 5 | my $spec = shift; | |||||||||||||||||||
| 14 | 5 | ▶ | return 0 unless ref $spec eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 15 | 5 | my @keys = grep { ref $spec->{ $_ } eq 'HASH' } keys %$spec; | |||||||||||||||||||
| 16 | 5 | ▶ | ($_ eq 'dependent') && return 1 foreach @keys; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | 5 | ▶ | return 0 if @keys; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 18 | 4 | return 1; | |||||||||||||||||||
| 19 | 1 | }; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | my $_collapse_profile; | |||||||||||||||||||
| 22 | 2 | ▶ | $_collapse_profile = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||
| 23 | 2 | my ($profile, $acc, $prefix) = @_; | |||||||||||||||||||
| 24 | 2 | foreach my $k ( keys %$profile ) { | |||||||||||||||||||
| 25 | 5 | ▶ | my $full_k = join '.' => ($prefix || (), $k); | ||||||||||||||||||
|
Condition66%2/3
$prefix || ()
MC/DC0%0/2
$prefix || ()
$prefix
()
Truth table66%2/3
A || B
A= $prefixB= ()
| |||||||||||||||||||||
| 26 | 5 | ▶ | if ( $_is_profile_spec->( $profile->{ $k } ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 4 | $acc->{ $full_k } = $profile->{ $k } | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | else { | ||||||||||||||||||||
| 30 | 1 | ▶ | (ref $profile->{ $k } eq 'HASH') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | || die "Can only collapse HASH refs"; | ||||||||||||||||||||
| 32 | 1 | $_collapse_profile->( $profile->{ $k }, $acc, $full_k ); | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | 1 | }; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 1 | ▶ | my $collapse_profile = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | 1 | my ($profile) = @_; | |||||||||||||||||||
| 39 | 1 | my $acc = {}; | |||||||||||||||||||
| 40 | 1 | $_collapse_profile->( $profile, $acc ); | |||||||||||||||||||
| 41 | 1 | $acc; | |||||||||||||||||||
| 42 | 1 | }; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 1 | my $_collapse_data_for_profile; | |||||||||||||||||||
| 45 | 4 | ▶ | $_collapse_data_for_profile = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||
| 46 | 4 | my ($profile, $data, $acc, $prefix) = @_; | |||||||||||||||||||
| 47 | 4 | foreach my $k ( keys %$data ) { | |||||||||||||||||||
| 48 | 9 | ▶ | my $full_k = join '.' => ($prefix || (), $k); | ||||||||||||||||||
|
Condition66%2/3
$prefix || ()
MC/DC0%0/2
$prefix || ()
$prefix
()
Truth table66%2/3
A || B
A= $prefixB= ()
| |||||||||||||||||||||
| 49 | 9 | ▶ | if ( exists $profile->{ $full_k } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | 7 | $acc->{ $full_k } = $data->{ $k } | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||
| 53 | 2 | ▶ | (ref $data->{ $k } eq 'HASH') | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | || die "Can only collapse HASH refs"; | ||||||||||||||||||||
| 55 | 2 | $_collapse_data_for_profile->( $profile, $data->{ $k }, $acc, $full_k ); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | 1 | }; | |||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | ▶ | my $collapse_data_for_profile = sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 61 | 2 | my ($profile, $data) = @_; | |||||||||||||||||||
| 62 | 2 | my $acc = {}; | |||||||||||||||||||
| 63 | 2 | $_collapse_data_for_profile->( $profile, $data, $acc ); | |||||||||||||||||||
| 64 | 2 | $acc; | |||||||||||||||||||
| 65 | 1 | }; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | # now to the subclass ... | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 1 | ▶ | sub BUILDARGS { | ||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 70 | 1 | my $self = shift; | |||||||||||||||||||
| 71 | 1 | my $params = $self->SUPER::BUILDARGS( @_ ); | |||||||||||||||||||
| 72 | # NOTE: | ||||||||||||||||||||
| 73 | # this is required, but we don't want to | ||||||||||||||||||||
| 74 | # make assumptions, if it is not here, then | ||||||||||||||||||||
| 75 | # it will die later on with the right error | ||||||||||||||||||||
| 76 | # so we can just process it if we have it. | ||||||||||||||||||||
| 77 | # - SL | ||||||||||||||||||||
| 78 | $params->{'profile'} = $collapse_profile->( $params->{'profile'} ) | ||||||||||||||||||||
| 79 | 1 | ▶ | if exists $params->{'profile'}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 1 | $params; | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 2 | ▶ | sub verify { | ||||||||||||||||||
|
Subroutine
verify: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 84 | 2 | my ($self, $params, $members) = @_; | |||||||||||||||||||
| 85 | 2 | $self->SUPER::verify( | |||||||||||||||||||
| 86 | $collapse_data_for_profile->( | ||||||||||||||||||||
| 87 | $self->profile, | ||||||||||||||||||||
| 88 | $params | ||||||||||||||||||||
| 89 | ), | ||||||||||||||||||||
| 90 | $members | ||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 1 | 1; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||