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 20     20   714017 use Carp;
  20         55  
  20         1166  
6              
7 20     20   9563 use String::ShellQuote;
  20         18353  
  20         1266  
8              
9 20     20   3087 use Types::Standard qw[ Str ];
  20         379327  
  20         157  
10 20     20   23984 use IPC::PrettyPipe::Arg::Format;
  20         92  
  20         730  
11              
12 20     20   173 use Moo;
  20         73  
  20         83  
13              
14             our $VERSION = '0.13';
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 20     20   7927 use namespace::clean;
  20         56  
  20         217  
29              
30              
31             BEGIN {
32              
33 20     20   8128 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 235     235 0 96309 my $class = shift;
66              
67 235 100       792 if ( @_ == 1 ) {
68              
69 10 50       211 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 225         3930 return {@_};
79             }
80              
81 25     25 1 38841 sub quoted_name { shell_quote( $_[0]->name ) }
82              
83 6     6 1 10724 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 111     111 1 3440 my $self = shift;
96              
97 111         1998 my $fmt = $self->fmt;
98              
99 111 100       3945 my $name = ( $fmt->has_pfx ? $fmt->pfx : '' ) . $self->name;
100              
101 111 100       332 if ( $self->has_value ) {
102              
103 14 100       47 if ( $fmt->has_sep ) {
104              
105 7         135 return join( '', $name, $fmt->sep, $self->value );
106              
107             }
108             else {
109              
110 7         54 return $name, $self->value;
111             }
112             }
113              
114             else {
115              
116 97         314 return $name;
117             }
118              
119             }
120              
121             sub valmatch {
122              
123 107     107 1 349 my $self = shift;
124 107         159 my $pattern = shift;
125              
126 107   100     1667 return $self->has_value && $self->value =~ /$pattern/;
127             }
128              
129             sub valsubst {
130              
131 58     58 1 227 my $self = shift;
132              
133 58         142 my ( $pattern, $rep ) = @_;
134              
135 58 100 100     560 if ( $self->has_value && ( my $value = $self->value ) =~ s/$pattern/$rep/ )
136             {
137              
138 44         1138 $self->_set_value( $value );
139              
140 44         1605 return 1;
141              
142             }
143              
144 14         53 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__