File Coverage

blib/lib/IPC/PrettyPipe/Arg.pm
Criterion Covered Total %
statement 42 46 91.3
branch 11 14 78.5
condition 6 12 50.0
subroutine 13 14 92.8
pod 6 7 85.7
total 78 93 83.8


line stmt bran cond sub pod time code
1             package IPC::PrettyPipe::Arg;
2              
3             # ABSTRACT: An argument to an IPC::PrettyPipe::Cmd command
4              
5 17     17   556038 use Carp;
  17         46  
  17         984  
6              
7 17     17   6667 use String::ShellQuote;
  17         13104  
  17         1006  
8              
9 17     17   2390 use Types::Standard qw[ Str ];
  17         309726  
  17         119  
10 17     17   17842 use IPC::PrettyPipe::Arg::Format;
  17         60  
  17         519  
11              
12 17     17   121 use Moo;
  17         51  
  17         66  
13              
14             our $VERSION = '0.12';
15              
16             has name => (
17             is => 'ro',
18             isa => Str,
19             required => 1,
20             );
21              
22             has value => (
23             is => 'rwp',
24             isa => Str,
25             predicate => 1,
26             );
27              
28 17     17   5984 use namespace::clean;
  17         55  
  17         130  
29              
30              
31             BEGIN {
32              
33 17     17   5599 IPC::PrettyPipe::Arg::Format->shadow_attrs;
34              
35             }
36              
37             with 'MooX::Attributes::Shadow::Role';
38              
39              
40             shadowable_attrs( 'fmt',
41             values %{ IPC::PrettyPipe::Arg::Format->shadowed_attrs } );
42              
43             with 'IPC::PrettyPipe::Queue::Element';
44              
45             has fmt => (
46             is => 'ro',
47             lazy => 1,
48             handles => [ keys %{ IPC::PrettyPipe::Arg::Format->shadowed_attrs } ],
49             default => sub {
50             IPC::PrettyPipe::Arg::Format->new_from_attrs( shift );
51             },
52             );
53              
54              
55              
56              
57              
58              
59             # accept full attribute interface, or
60             # new( name );
61             # new( [ name, value ] );
62              
63             sub BUILDARGS {
64              
65 208     208 0 82049 my $class = shift;
66              
67 208 100       621 if ( @_ == 1 ) {
68              
69 10 50       158 return $_[0] if 'HASH' eq ref( $_[0] );
70              
71             return { name => $_[0][0], value => $_[0][1] }
72 0 0 0     0 if 'ARRAY' eq ref( $_[0] ) && @{ $_[0] } == 2;
  0         0  
73              
74 0         0 return { name => $_[0] };
75              
76             }
77              
78 198         3391 return {@_};
79             }
80              
81 23     23 1 34427 sub quoted_name { shell_quote( $_[0]->name ) }
82              
83 6     6 1 10681 sub quoted_value { shell_quote( $_[0]->value ) }
84              
85              
86             # for render templates
87             sub has_blank_value {
88              
89 0   0 0 1 0 return $_[0]->has_value && $_[0]->value eq '';
90              
91             }
92              
93             sub render {
94              
95 86     86 1 2681 my $self = shift;
96              
97 86         1543 my $fmt = $self->fmt;
98              
99 86 100       3067 my $name = ( $fmt->has_pfx ? $fmt->pfx : '' ) . $self->name;
100              
101 86 100       190 if ( $self->has_value ) {
102              
103 8 100       17 if ( $fmt->has_sep ) {
104              
105 4         57 return join( '', $name, $fmt->sep, $self->value );
106              
107             }
108             else {
109              
110 4         27 return $name, $self->value;
111             }
112             }
113              
114             else {
115              
116 78         235 return $name;
117             }
118              
119             }
120              
121             sub valmatch {
122              
123 107     107 1 267 my $self = shift;
124 107         129 my $pattern = shift;
125              
126 107   100     1025 return $self->has_value && $self->value =~ /$pattern/;
127             }
128              
129             sub valsubst {
130              
131 58     58 1 211 my $self = shift;
132              
133 58         125 my ( $pattern, $rep ) = @_;
134              
135 58 100 100     464 if ( $self->has_value && ( my $value = $self->value ) =~ s/$pattern/$rep/ )
136             {
137              
138 44         919 $self->_set_value( $value );
139              
140 44         1311 return 1;
141              
142             }
143              
144 14         49 return 0;
145             }
146              
147             1;
148              
149             #
150             # This file is part of IPC-PrettyPipe
151             #
152             # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
153             #
154             # This is free software, licensed under:
155             #
156             # The GNU General Public License, Version 3, June 2007
157             #
158              
159             __END__