File Coverage

blib/lib/Data/Rx/CommonType.pm
Criterion Covered Total %
statement 40 53 75.4
branch 8 14 57.1
condition 1 3 33.3
subroutine 9 15 60.0
pod 0 9 0.0
total 58 94 61.7


line stmt bran cond sub pod time code
1 1     1   312 use strict;
  1         1  
  1         24  
2 1     1   3 use warnings;
  1         1  
  1         28  
3             package Data::Rx::CommonType;
4             # ABSTRACT: base class for core Rx types
5             $Data::Rx::CommonType::VERSION = '0.200007';
6 1     1   3 use Carp ();
  1         1  
  1         16  
7 1     1   3 use Scalar::Util ();
  1         1  
  1         10  
8 1     1   274 use Data::Rx::Failure;
  1         2  
  1         23  
9 1     1   275 use Data::Rx::FailureSet;
  1         2  
  1         588  
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 1235 my ($self, $struct) = @_;
34              
35 1507   33     4988 $struct->{type} ||= $self->type;
36              
37 1507         3112 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 1313 my ($self, $struct) = @_;
50              
51 1460         2367 die $self->new_fail($struct);
52             }
53              
54             sub perform_subchecks {
55 193     193 0 187 my ($self, $subchecks) = @_;
56              
57 193         140 my @fails;
58              
59 193         229 foreach my $subcheck (@$subchecks) {
60 421 100       798 if (Scalar::Util::blessed($subcheck)) {
61 47         52 push @fails, $subcheck;
62 47         64 next;
63             }
64              
65 374         517 my ($value, $checker, $context) = @$subcheck;
66              
67 374 100       287 next if eval { $checker->assert_valid($value) };
  374         751  
68              
69 148         322 my $failure = $@;
70             Carp::confess($failure)
71 148 50       129 unless eval { $failure->isa('Data::Rx::FailureSet') ||
  148 50       614  
72             $failure->isa('Data::Rx::Failure') };
73              
74 148         350 $failure->contextualize({
75             type => $self->type,
76             %$context,
77             });
78              
79 148         272 push @fails, $failure;
80             }
81              
82 193 100       326 if (@fails) {
83 125         231 die Data::Rx::FailureSet->new( { rx => $self->rx, failures => \@fails } );
84             }
85              
86 68         104 return 1;
87             }
88              
89             1;
90              
91             __END__