Simple/Library/Hash.pm

filter: Statementstmt 100% 36 / 36 Branchbran 100% 16 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 59 / 59 CC 11 SCAR 14.0
CC
11
Cov
100%
__ANON__
6.0
__ANON__
5.0
__ANON__
2.0
CRAP 4.1
« Simple/Library/Array.pm ↑ Summary Simple/Library/Scalar.pm »
1 =head1 NAME
2  
3 Declare::Constraints::Simple::Library::Hash - Hash Constraints
4  
5 =cut
6  
7 package Declare::Constraints::Simple::Library::Hash;
8 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Declare::Constraints::Simple-Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 SYNOPSIS
14  
15 my $constraint = And(
16  
17 # make sure all keys are present
18 HasAllKeys( qw(foo bar) ),
19  
20 # constraints for the keys
21 OnHashKeys( foo => IsInt, bar => HasLength )
22  
23 );
24  
25 =head1 DESCRIPTION
26  
27 This module contains all constraints that can be applied to hash
28 references.
29  
30 =head2 HasAllKeys(@keys)
31  
32 The value has to be a hashref, and contain all keys listed in
33 C<@keys> to pass this constraint.
34  
35 The stack or path part of C<HasAllKeys> is C<HasAllKeys[$key]> where
36 C<$key> is the missing key.
37  
38 =cut
39  
40 constraint 'HasAllKeys',
41 10 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 10 my @vk = @_;
43 26 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
44 26 ▶ return _false('Undefined Value') unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 25 26
45 25 ▶ return _false('Not a HashRef') unless ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 2 23 25
46 23 for (@vk) {
47 55 ▶ unless (exists $_[0]{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[0]{$_}) 5 50 55
48 5 _info($_);
49 5 return _false("No '$_' key present");
50 }
51 }
52 18 return _true;
53 10 };
54 13 };
55  
56 =head2 OnHashKeys(key => $constraint, key => $constraint, ...)
57  
58 This allows you to pass a constraint for each specific key in
59 a hash reference. If a specified key is not in the validated
60 hash reference, the validation for this key is not done. To make
61 a key a requirement, use L<HasAllKeys(@keys)> above in combination
62 with this, e.g. like:
63  
64 And( HasAllKeys( qw(foo bar baz) )
65 OnHashKeys( foo => IsInt,
66 bar => Matches(qr/bar/),
67 baz => IsArrayRef( HasLength )));
68  
69 Also, as you might see, you don't have to check for C<IsHashRef>
70 validity here. The hash constraints are already doing that by
71 themselves.
72  
73 The stack or path part of C<OnHashKeys> looks like C<OnHashKeys[$key]>
74 where C<$key> is the key of the failing value.
75  
76 =cut
77  
78 constraint 'OnHashKeys',
79 12 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
80 12 my %def = my @def = @_;
81 12 my @key_order;
82 12 ▶ while (my $key = shift @def) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = shift @def) 21 12 33
83 21 my $val = shift @def;
84 21 push @key_order, $key;
85 }
86 28 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
87 28 ▶ return _false('Undefined Value') unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 27 28
88 27 ▶ return _false('Not a HashRef') unless ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 1 26 27
89 26 for (@key_order) {
90 64 my @vc = @{_listify($def{$_})};
91 64 ▶ next unless exists $_[0]{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{$_} 1 63 64
92 63 my $r = _apply_checks($_[0]{$_}, \@vc, $_);
93 63 ▶ return $r unless $r->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->is_valid 14 49 63
94 }
95 12 return _true;
96 12 };
97 13 };
98  
99 =head1 SEE ALSO
100  
101 L<Declare::Constraints::Simple>, L<Declare::Constraints::Simple::Library>
102  
103 =head1 AUTHOR
104  
105 Robert 'phaylon' Sedlacek C<E<lt>phaylon@dunkelheit.atE<gt>>
106  
107 =head1 LICENSE AND COPYRIGHT
108  
109 This module is free software, you can redistribute it and/or modify it
110 under the same terms as perl itself.
111  
112 =cut
113  
114 13 1;
« Simple/Library/Array.pm ↑ Summary Simple/Library/Scalar.pm »