File Coverage

blib/lib/Data/Rx/CommonType.pm
Criterion Covered Total %
statement 39 52 75.0
branch 8 14 57.1
condition 1 3 33.3
subroutine 9 15 60.0
pod 0 9 0.0
total 57 93 61.2


line stmt bran cond sub pod time code
1 1     1   413 use v5.12.0;
  1         3  
2 1     1   6 use warnings;
  1         2  
  1         34  
3             package Data::Rx::CommonType 0.200008;
4             # ABSTRACT: base class for core Rx types
5              
6 1     1   6 use Carp ();
  1         2  
  1         12  
7 1     1   6 use Scalar::Util ();
  1         2  
  1         31  
8 1     1   415 use Data::Rx::Failure;
  1         3  
  1         29  
9 1     1   419 use Data::Rx::FailureSet;
  1         2  
  1         509  
10              
11             # requires: new_checker, type, type_uri, rx, assert_valid
12              
13 0     0 0 0 sub new_checker { Carp::croak "$_[0] did not implement new_checker" }
14 0     0 0 0 sub type_uri { Carp::croak "$_[0] did not implement type_uri" }
15 0     0 0 0 sub type { Carp::croak "$_[0] did not implement type" }
16 0     0 0 0 sub rx { Carp::croak "$_[0] did not implement rx" }
17 0     0 0 0 sub assert_valid { Carp::croak "$_[0] did not implement assert_valid" }
18              
19             sub check {
20 0     0 0 0 my ($self, $value) = @_;
21 0         0 local $@;
22              
23 0 0       0 return 1 if eval { $self->assert_valid($value); };
  0         0  
24 0         0 my $error = $@;
25              
26             # If you wanted the failure, you should've used assert_valid.
27 0 0       0 return 0 if eval { $error->isa('Data::Rx::FailureSet') };
  0         0  
28              
29 0         0 die $error;
30             }
31              
32             sub new_fail {
33 1507     1507 0 2485 my ($self, $struct) = @_;
34              
35 1507   33     6424 $struct->{type} ||= $self->type;
36              
37 1507         3613 Data::Rx::FailureSet->new({
38             rx => $self->rx,
39             failures => [
40             Data::Rx::Failure->new({
41             rx => $self->rx,
42             struct => $struct,
43             })
44             ]
45             });
46             }
47              
48             sub fail {
49 1460     1460 0 2935 my ($self, $struct) = @_;
50              
51 1460         2934 die $self->new_fail($struct);
52             }
53              
54             sub perform_subchecks {
55 193     193 0 360 my ($self, $subchecks) = @_;
56              
57 193         270 my @fails;
58              
59 193         339 foreach my $subcheck (@$subchecks) {
60 421 100       986 if (Scalar::Util::blessed($subcheck)) {
61 47         99 push @fails, $subcheck;
62 47         101 next;
63             }
64              
65 374         697 my ($value, $checker, $context) = @$subcheck;
66              
67 374 100       507 next if eval { $checker->assert_valid($value) };
  374         989  
68              
69 148         416 my $failure = $@;
70             Carp::confess($failure)
71 148 50       237 unless eval { $failure->isa('Data::Rx::FailureSet') ||
  148 50       554  
72             $failure->isa('Data::Rx::Failure') };
73              
74 148         379 $failure->contextualize({
75             type => $self->type,
76             %$context,
77             });
78              
79 148         367 push @fails, $failure;
80             }
81              
82 193 100       432 if (@fails) {
83 125         289 die Data::Rx::FailureSet->new( { rx => $self->rx, failures => \@fails } );
84             }
85              
86 68         132 return 1;
87             }
88              
89             1;
90              
91             __END__