File Coverage

blib/lib/Perinci/Object/Function.pm
Criterion Covered Total %
statement 51 51 100.0
branch 10 10 100.0
condition 9 16 56.2
subroutine 10 10 100.0
pod 6 6 100.0
total 86 93 92.4


line stmt bran cond sub pod time code
1             package Perinci::Object::Function;
2              
3             our $DATE = '2021-01-02'; # DATE
4             our $VERSION = '0.311'; # VERSION
5              
6 1     1   20 use 5.010;
  1         4  
7 1     1   5 use strict;
  1         2  
  1         22  
8 1     1   5 use warnings;
  1         2  
  1         31  
9              
10 1     1   447 use parent qw(Perinci::Object::Metadata);
  1         301  
  1         6  
11              
12 1     1 1 45 sub type { "function" }
13              
14             # convenience for accessing features property
15             sub feature {
16 7     7 1 64 my $self = shift;
17 7         13 my $name = shift;
18 7 100       19 if (@_) {
19 2 100       7 die "1.0 can't set feature" if $self->v eq 1.0;
20 1         4 my $value = shift;
21 1   50     2 ${$self}->{features} //= {};
  1         4  
22 1         2 my $old = ${$self}->{features}{$name};
  1         2  
23 1         3 ${$self}->{features}{$name} = $value;
  1         2  
24 1         5 return $old;
25             } else {
26 5         8 ${$self}->{features}{$name};
  5         27  
27             }
28             }
29              
30             sub features {
31 2     2 1 10 my $self = shift;
32 2   100     3 ${$self}->{features} // {};
  2         33  
33             }
34              
35             # transaction can be used to emulate dry run, by calling with -tx_action =>
36             # 'check_state' only
37             sub can_dry_run {
38 2     2 1 22 my $self = shift;
39 2   50     4 my $ff = ${$self}->{features} // {};
  2         8  
40 2   33     16 $ff->{dry_run} // $ff->{tx} && $ff->{tx}{v} == 2;
      66        
41             }
42              
43             sub default_dry_run {
44 4     4 1 19 my $self = shift;
45 4   50     5 my $ff = ${$self}->{features} // {};
  4         14  
46 4 100       25 ref($ff->{dry_run}) eq 'HASH' && $ff->{dry_run}{default};
47             }
48              
49             # convenience for accessing args property
50             sub arg {
51 7     7 1 46 my $self = shift;
52 7         12 my $name = shift;
53 7 100       17 if (@_) {
54 2 100       6 die "1.0 can't set arg" if $self->v eq 1.0;
55 1         2 my $value = shift;
56 1   50     2 ${$self}->{args} //= {};
  1         5  
57 1         2 my $old = ${$self}->{args}{$name};
  1         2  
58 1         2 ${$self}->{args}{$name} = $value;
  1         3  
59 1         5 return $old;
60             } else {
61 5         8 ${$self}->{args}{$name};
  5         39  
62             }
63             }
64              
65             1;
66             # ABSTRACT: Represent function metadata
67              
68             __END__