blib/lib/Config/YAARG.pm

filter: Statementstmt 96.0% 98 / 102 Branchbran 66.6% 20 / 30 Conditioncond 52.6% 10 / 19 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 20 / 20 Podpod 0.0% 0 / 6 totaltotal 79.7% 150 / 188 CC 29 SCAR 14.4
CC
29
Cov
85%
_yaarg_transform_values_r
8.5
ARGS
6.2
_yaarg_transform_keys
5.3
CRAP 4.2
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # Copyright (C) 2011 - Anthony J. Lucas - kaoyoriketsu@ansoni.com
5  
6  
7  
8 package Config::YAARG;
9 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12  
13 2 our $VERSION = '0.023';
14  
15  
16  
17 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22  
23 2 ▶use Class::ISA ( );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Getopt::Long ( );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26  
27  
28 #EXPORT CONFIGURATION
29  
30  
31 2 our @PUBLIC_CONSTANTS = qw/
32 ARG_PASSTHROUGH
33 ARG_IGNORE
34 /;
35  
36 2 our @SCRIPT_ROUTINES = qw/ARGS/;
37 2 our @CLASS_METHODS = qw/process_args/;
38 2 our @STANDARD_ROUTINES = qw/ProcessArgs/;
39  
40 2 our @EXPORT_OK = (
41 @PUBLIC_CONSTANTS,
42 @STANDARD_ROUTINES,
43 @SCRIPT_ROUTINES,
44 @CLASS_METHODS
45 );
46  
47 2 our %EXPORT_TAGS = (
48 'class' => [ @PUBLIC_CONSTANTS, @CLASS_METHODS ],
49 'script' => [ @PUBLIC_CONSTANTS, @SCRIPT_ROUTINES ]
50 );
51  
52  
53  
54 #CONSTANTS
55  
56  
57 2 ▶use constant ARG_PASSTHROUGH => 'pass';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use constant ARG_IGNORE => 'ignore';
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60  
61 1 ▶sub ARG_NAME_LIST {};
Subroutine ARG_NAME_LIST: called CC=1 SCAR=0.0
Pod undocumented
62 1 ▶sub ARG_NAME_MAP {};
Subroutine ARG_NAME_MAP: called CC=1 SCAR=0.0
Pod undocumented
63 1 ▶sub ARG_VALUE_TRANS {};
Subroutine ARG_VALUE_TRANS: called CC=1 SCAR=0.0
Pod undocumented
64  
65  
66  
67 #SCRIPT HELPER ROUTINE
68  
69  
70 1 ▶sub ARGS {
Subroutine ARGS: called CC=6 SCAR=18.3
Pod undocumented
71  
72 1 ▶ my $class = $_[0] || caller();
Condition33%1/3
$_[0] || caller()
l!l&&r!l&&!r
010
MC/DC0%0/2
$_[0] || caller()
$_[0] caller()
Truth table33%1/3
A || B
A= $_[0]B= caller()
ABresult
1X1
011
000
73 1 my $config = _yaarg_fetch_config(__PACKAGE__, $class);
74 1 my $names = $config->{names};
75 1 ▶ return unless ($names);
Branch: 1 branch
BranchTrueFalseTotal
unless $names 0 1 1
76  
77 1 my @names = @$names;
78 1 s/=.*?$// foreach(@$names);
79  
80 1 my %args = ();
81 Getopt::Long::GetOptions(\%args,
82 1 ▶ map { (!s/=b$// and !/=/) ? "$_=s" : $_ } @names);
Branch: 1 branch
BranchTrueFalseTotal
!(s/=b$// || /=/) ? : 2 1 3
Condition66%2/3
s/=b$// || /=/
l!l&&r!l&&!r
102
MC/DC50%1/2
s/=b$// || /=/
s/=b$// /=/
Truth table66%2/3
A || B
A= s/=b$//B= /=/
ABresult
1X1
011
000
83 1 return %{ProcessArgs(__PACKAGE__, $config, %args)};
84 }
85  
86  
87  
88 #CLASS HELPER ROUTINE
89  
90  
91 1 ▶sub process_args {
Subroutine process_args: called CC=3 SCAR=11.0
Pod undocumented
92  
93 1 my ($self, @args) = @_;
94  
95 #detect alt call signatures
96 1 ▶ my $target = (@args % 2)
Branch: 1 branch
BranchTrueFalseTotal
@args % 2 ? : 1 0 1
97 ? shift(@args)
98 : {};
99  
100 #gather config and process args
101 1 my $result = $self->ProcessArgs(
102 $self->_yaarg_fetch_config,
103 @args);
104  
105 #copy results to target struct
106 $target->{$_} = $result->{$_}
107 1 foreach (keys(%{$result}));
108  
109 1 return $target;
110 }
111  
112  
113  
114 #CORE ROUTINES
115  
116  
117 2 ▶sub ProcessArgs {
Subroutine ProcessArgs: called CC=4 SCAR=14.3
Pod undocumented
118  
119 2 my ($context, $config, %args) = @_;
120  
121 2 my $map = $config->{'map'};
122 2 my $trans = $config->{'trans'};
123  
124 2 my $t_args;
125 2 ▶ $t_args = $context->_yaarg_transform_values(\%args, $trans)
Branch: 1 branch
BranchTrueFalseTotal
if $trans 2 0 2
126 if ($trans);
127 2 ▶ $context->_yaarg_transform_keys($t_args, $map, 1)
Branch: 1 branch
BranchTrueFalseTotal
if $map 2 0 2
128 if ($map);
129  
130 2 ▶ return $t_args || {};
Condition50%1/2
$t_args || {}
l!l
20
MC/DC0%0/1
$t_args || {}
$t_args
Truth table50%1/2
A
A= $t_args
Aresult
11
00
131 }
132  
133  
134 2 ▶sub _yaarg_fetch_config {
Subroutine _yaarg_fetch_config: called CC=4 SCAR=14.1
135  
136 2 my ($context, $class) = @_;
137 2 ▶ $class ||= (ref($context) || $context);
Condition50%3/6
$class ||= ref $context || $context
l!l&&r!l&&!r
110
ref $context || $context
l!l&&r!l&&!r
100
MC/DC0%0/3
$class ||= ref $context || $context
$class ref $context $context
Truth table0%0/4
A ||= B || C
A= $classB= ref $contextC= $context
ABCresult
1XX1
01X1
0011
0000
138  
139 2 my @ISA = Class::ISA::self_and_super_path($class);
140 2 my (@map, @trans, @names);
141 2 foreach (@ISA) {
142 5 my ($m, $t, $n) =
143 $context->_yaarg_fetch_class_config($_);
144 5 push(@map, $context->_yaarg_to_list($m));
145 5 push(@trans, $context->_yaarg_to_list($t));
146 5 push(@names, $context->_yaarg_to_list($n));
147 }
148 return {
149 2 map => {@map},
150 trans => {@trans},
151 names => \@names
152 };
153 }
154  
155  
156 5 ▶sub _yaarg_fetch_class_config {
Subroutine _yaarg_fetch_class_config: called CC=4 SCAR=13.9
157  
158 5 my $class = $_[1];
159 5 my @return;
160  
161 5 foreach (qw/
162 ARG_NAME_MAP
163 ARG_VALUE_TRANS
164 ARG_NAME_LIST/) {
165  
166 15 ▶ push(@return, (($class->can($_))
Branch: 1 branch
BranchTrueFalseTotal
$class->can($_) ? : 12 3 15
Condition100%2/2
$class->$_ or undef
l!l
93
MC/DC100%1/1
$class->$_ or undef
$class->$_
Truth table100%2/2
A
A= $class->$_
Aresult
11
00
167 ? $class->$_() || undef
168 : undef));
169 }
170 5 return @return;
171 }
172  
173  
174  
175 #UTILITY ROUTINES
176  
177  
178  
179 15 ▶sub _yaarg_to_list {
Subroutine _yaarg_to_list: called CC=3 SCAR=11.0
180  
181 15 ▶ return %{$_[1]} if (ref($_[1]) eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "HASH" 6 9 15
182 9 ▶ return @{$_[1]} if (ref($_[1]) eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[1] eq "ARRAY" 3 6 9
183 6 return ();
184 }
185  
186  
187 2 ▶sub _yaarg_transform_keys {
Subroutine _yaarg_transform_keys: called CC=5 SCAR=16.7
188  
189 2 my ($self, $hash, $key_map, $no_dup) = @_;
190
191 2 ▶ my ($thash, $v) = $no_dup ? $hash : {};
Branch: 1 branch
BranchTrueFalseTotal
$no_dup ? : 2 0 2
192 2 foreach (keys %$key_map) {
193 5 ▶ if (defined($v = $hash->{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($v = $hash->{$_})) 5 0 5
194 5 $thash->{$key_map->{$_}} = $v;
195 5 ▶ delete($thash->{$_}) if ($no_dup);
Branch: 1 branch
BranchTrueFalseTotal
if $no_dup 5 0 5
196 }
197 }
198 2 return $thash;
199 }
200  
201  
202 2 ▶sub _yaarg_transform_values {
Subroutine _yaarg_transform_values: called CC=1 SCAR=0.0
203 2 _yaarg_transform_values_r(@_[0..2], '');
204 }
205  
206  
207 9 ▶sub _yaarg_transform_values_r {
Subroutine _yaarg_transform_values_r: called CC=7 SCAR=21.4
208  
209 9 my ($self, $struct, $type_map, $key) = @_;
210  
211  
212 #attempt reading common data structures
213 9 given (ref($struct)) {
214  
215 9 when ('ARRAY') {
216 return [ map {
217 0 $self->_yaarg_transform_values_r($_, $type_map, $key);
218 } @$struct ];
219 }
220 9 default {
221  
222 my $target = (defined($key) and $type_map)
223 9 ▶ ? $type_map->{$key}
Branch: 1 branch
BranchTrueFalseTotal
defined $key && $type_map ? : 9 0 9
Condition33%1/3
defined $key and $type_map
!ll&&!rl&&r
009
MC/DC0%0/2
defined $key and $type_map
defined $key $type_map
Truth table33%1/3
A and B
A= defined $keyB= $type_map
ABresult
0X0
100
111
224 : undef;
225
226 #attempt custom type mapping
227 9 ▶ if ($target) {
Branch: 2 branches
BranchTrueFalseTotal
if ($target) { } 3 6 9
elsif (ref $struct eq 'HASH') { } 3 3 6
228 3 given (ref($target)) {
229 3 when ('CODE') {
230 3 return $target->($struct);
231 }
232 0 when ('') {
233 0 ▶ return $target->new($struct) if ($target);
Branch: 1 branch
BranchTrueFalseTotal
if $target 0 0 0
234 }
235 }
236 } elsif (ref($struct) eq 'HASH') {
237  
238 3 $target = {};
239 3 foreach (keys(%$struct)) {
240 $target->{$_} = $self->_yaarg_transform_values_r(
241 7 $struct->{$_}, $type_map, $_);
242 }
243 3 return $target;
244 }
245 }
246 }
247 #otherwise return unchanged
248 3 return $struct;
249 }
250  
251  
252  
253  
254  
255 2 1;
↑ Summary