File Coverage

blib/lib/Repl/Spec/Type/CheckedHashType.pm
Criterion Covered Total %
statement 14 32 43.7
branch 0 4 0.0
condition 1 3 33.3
subroutine 4 6 66.6
pod 3 3 100.0
total 22 48 45.8


line stmt bran cond sub pod time code
1             =head1 NAME
2            
3             Repl::Spec::Type::CheckedHashType - A parameter guard for hashes.
4            
5             =head1 SYNOPSIS
6            
7             This type guard ensures that a hash was passed containing values
8             of another nested type guard provided by the user. An example: a hash of integers
9             is ensured by creating a CheckedHashType with nested IntegerType.
10             The guards can be nested arbitrarily to obtain a guard for whatever complex nested type.
11            
12             =head1 DESCRIPTION
13            
14             =head1 Methods
15            
16             =over 4
17            
18             =item C
19            
20             Parameters: A nested guard that will be applied to the values of the hash.
21            
22             =item C
23            
24             Parameters: A single expression.
25             Returns: A new hash where the values are converted by the nested type guard.
26            
27             =item C
28            
29             =head1 SEE ALSO
30            
31             L
32             L
33             L
34             L
35             L
36             L
37             L
38             L
39            
40             =cut
41            
42             package Repl::Spec::Type::CheckedHashType;
43            
44 1     1   6 use strict;
  1         2  
  1         32  
45 1     1   5 use warnings;
  1         1  
  1         26  
46 1     1   6 use Carp;
  1         1  
  1         379  
47            
48             # Parameter:
49             # - The type of the hash values.
50             sub new
51             {
52 1     1 1 2 my $invocant = shift;
53 1   33     6 my $class = ref($invocant) || $invocant;
54            
55 1         2 my $eltyp = shift;
56            
57 1         4 my $self= {TYPE=>$eltyp};
58 1         57 return bless $self, $class;
59             }
60            
61             sub guard
62             {
63 0     0 1   my $self = shift;
64 0           my $arg = shift;
65            
66 0           my $eltyp = $self->{TYPE};
67            
68 0 0         if(ref($arg) eq 'HASH')
69             {
70 0           my $result = {};
71 0           my $idx = 0;
72 0           foreach my $el (keys %$arg)
73             {
74 0           my $val;
75 0           eval {$val = $eltyp->guard($arg->{$el})};
  0            
76 0 0         croak sprintf("Expected %s but the value of element '%s' does not comply.\n%s.", $self->name(), $el, $@)if ($@);
77 0           $result->{$el} = $val;
78 0           $idx = $idx + 1;
79             }
80 0           return $result;
81             }
82             else
83             {
84 0           croak sprintf("Expected %s but received '%s'.", $self->name(), $arg);
85             }
86             }
87            
88             sub name
89             {
90 0     0 1   my $self = shift;
91 0           my $eltyp = $self->{TYPE};
92 0           return sprintf("HASH of %s", $eltyp->name());
93             }
94            
95             1;