File Coverage

blib/lib/Perinci/Object/EnvResult.pm
Criterion Covered Total %
statement 58 63 92.0
branch 12 18 66.6
condition 3 8 37.5
subroutine 10 11 90.9
pod 8 8 100.0
total 91 108 84.2


line stmt bran cond sub pod time code
1             package Perinci::Object::EnvResult;
2              
3             our $DATE = '2021-01-02'; # DATE
4             our $VERSION = '0.311'; # VERSION
5              
6 2     2   558 use 5.010;
  2         8  
7 2     2   10 use strict;
  2         4  
  2         40  
8 2     2   9 use warnings;
  2         16  
  2         977  
9              
10             sub new {
11 1     1 1 3 my ($class, $res) = @_;
12 1   50     5 $res //= [0, "", undef];
13 1         2 my $obj = \$res;
14 1         4 bless $obj, $class;
15             }
16              
17             sub new_ok {
18 0     0 1 0 my $class = shift;
19 0         0 my $res = [200, "OK"];
20 0 0       0 if (@_) {
21 0         0 push @$res, $_[0];
22             }
23 0         0 $class->new($res);
24             }
25              
26             sub status {
27 5     5 1 18 my ($self, $new) = @_;
28 5 100       16 if (defined $new) {
29 2 50 33     18 die "Status must be an integer between 100 and 555" unless
      33        
30             int($new) eq $new && $new >= 100 && $new <= 555;
31 2         3 my $old = ${$self}->[0];
  2         6  
32 2         2 ${$self}->[0] = $new;
  2         5  
33 2         8 return $old;
34             }
35 3         5 ${$self}->[0];
  3         23  
36             }
37              
38             sub message {
39 3     3 1 8 my ($self, $new) = @_;
40 3 100       11 if (defined $new) {
41 1 50       4 die "Extra must be a string" if ref($new);
42 1         2 my $old = ${$self}->[1];
  1         3  
43 1         2 ${$self}->[1] = $new;
  1         2  
44 1         5 return $old;
45             }
46 2         4 ${$self}->[1];
  2         9  
47             }
48              
49             # avoid 'result' as this is ambiguous (the enveloped one? the naked one?). even
50             # avoid 'enveloped' (the payload being enveloped? the enveloped result
51             # (envelope+result inside)?)
52              
53             sub payload {
54 3     3 1 6 my ($self, $new) = @_;
55 3 100       10 if (defined $new) {
56 1         2 my $old = ${$self}->[2];
  1         3  
57 1         2 ${$self}->[2] = $new;
  1         3  
58 1         5 return $old;
59             }
60 2         3 ${$self}->[2];
  2         13  
61             }
62              
63             sub meta {
64 3     3 1 9 my ($self, $new) = @_;
65 3 100       11 if (defined $new) {
66 1 50       5 die "Extra must be a hashref" unless ref($new) eq 'HASH';
67 1         2 my $old = ${$self}->[3];
  1         3  
68 1         2 ${$self}->[3] = $new;
  1         3  
69 1         5 return $old;
70             }
71 2         3 ${$self}->[3];
  2         13  
72             }
73              
74             sub is_success {
75 2     2 1 6 my ($self) = @_;
76 2         3 my $status = ${$self}->[0];
  2         5  
77 2 50       17 $status >= 200 && $status <= 299;
78             }
79              
80             sub as_struct {
81 9     9 1 54 my ($self) = @_;
82 9         15 ${$self};
  9         50  
83             }
84              
85             1;
86             # ABSTRACT: Represent enveloped result
87              
88             __END__