File Coverage

blib/lib/Error/ROP/Imp.pm
Criterion Covered Total %
statement 36 45 80.0
branch 12 16 75.0
condition 3 3 100.0
subroutine 5 6 83.3
pod 0 2 0.0
total 56 72 77.7


line stmt bran cond sub pod time code
1             package Error::ROP::Imp;
2 5     5   1476 use Moose;
  5         2002639  
  5         38  
3              
4             has value => (is => 'ro', required => 0, default => undef);
5             has failure => (is => 'ro', required => 0, default => '');
6              
7             sub is_valid {
8 46     46 0 1050 return shift->failure eq '';
9             }
10              
11             sub _then_hash {
12 13     13   24 my ($self, @then_clauses) = @_;
13 13         20 my $either = $self;
14              
15 13         42 my $length = @then_clauses / 2;
16 13         49 for(my $i = 0; $i < $length; $i++) {
17 15         25 my $err = $then_clauses[2 * $i];
18 15         23 my $fn = $then_clauses[2 * $i + 1];
19 15 100       29 if ($either->is_valid) {
20 12         276 local $_ = $either->value;
21 12         16 my $res = eval {
22 12         30 $fn->($_);
23             };
24 12 100       85 if ($@) {
25 3 100 100     13 my $msg = length $err > 0 && $err ne 'undef' ? $err : $@;
26 3         60 return Error::ROP::Imp->new(failure => $msg);
27             }
28 9         205 $either = Error::ROP::Imp->new(value => $res);
29             }
30             }
31              
32 10         97 return $either;
33             }
34              
35             sub _then_list {
36 7     7   15 my ($self, @then_clauses) = @_;
37 7         9 my $either = $self;
38              
39 7         9 my $length = @then_clauses;
40 7         21 for(my $i = 0; $i < $length; $i++) {
41 7         11 my $fn = $then_clauses[$i];
42 7 100       13 if ($either->is_valid) {
43 3         58 local $_ = $either->value;
44 3         6 my $res = eval {
45 3         6 $fn->($_);
46             };
47 3 100       25 if ($@) {
48 2         79 return Error::ROP::Imp->new(failure => $@);
49             }
50 1         20 $either = Error::ROP::Imp->new(value => $res);
51             }
52             }
53              
54 5         18 return $either;
55             }
56              
57             sub _wrap_call_with_dollar {
58 0     0   0 my $either = shift;
59 0         0 my $fn = shift;
60 0 0       0 if ($either->is_valid) {
61 0         0 local $_ = $either->value;
62 0         0 my $res = eval {
63 0         0 $fn->($_);
64             };
65 0 0       0 if ($@) {
66 0         0 return Error::ROP::Imp->new(failure => $@);
67             }
68 0         0 $either = Error::ROP::Imp->new(value => $res);
69             }
70             }
71              
72             sub then {
73 20     20 0 128 my ($self, @then_clauses) = @_;
74              
75 20 100       44 if((scalar @then_clauses) % 2 == 0) {
76 13         30 $self->_then_hash(@then_clauses);
77             }
78             else {
79 7         15 $self->_then_list(@then_clauses);
80             }
81             }
82              
83             __PACKAGE__->meta->make_immutable;
84             1;