blib/lib/Pod/Elemental/MakeSelector.pm

filter: Statementstmt 85.8% 85 / 99 Branchbran 82.3% 28 / 34 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 92.0% 23 / 25 Podpod 11.1% 1 / 9 totaltotal 80.6% 142 / 176 CC 19 SCAR 10.0
CC
19
Cov
84%
smart_match
6.4
region_action
4.0
make_selector
3.3
CRAP 2.7
↑ Summary
1 #---------------------------------------------------------------------
2 package Pod::Elemental::MakeSelector;
3 #
4 # Copyright 2012 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: 5 Jun 2012
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Build complex selectors as a single sub
18 #---------------------------------------------------------------------
19  
20 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 our $VERSION = '0.12';
25 # This file is part of Pod-Elemental-MakeSelector 0.12 (October 17, 2015)
26  
27 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 exports => [ qw(make_selector) ],
31 groups => { default => [ qw(make_selector) ]},
32 3 };
33  
34 #=====================================================================
35 # Recturn true if the first element of the arrayref is not a string
36 # starting with -
37  
38 sub _has_optional_parameter
39 34 ▶{
Subroutine _has_optional_parameter: called CC=3 SCAR=11.2
40 34 my ($inputR) = @_;
41  
42 34 ▶ @$inputR and (ref $inputR->[0] or not $inputR->[0] =~ /^-/);
Branch: 1 branch
BranchTrueFalseTotal
@$inputR and ref $inputR->[0] || !($inputR->[0] =~ /^-/) 26 8 34
Condition66%4/6
@$inputR && (ref $inputR->[0] || !($inputR->[0] =~ /^-/))
!ll&&!rl&&r
8026
ref $inputR->[0] or not $inputR->[0] =~ /^-/
l!l&&r!l&&!r
10160
MC/DC33%1/3
@$inputR && (ref $inputR->[0] || !($inputR->[0] =~ /^-/))
@$inputR ref $inputR->[0] not $inputR->[0] =~ /^-/
Truth table75%3/4
A && B or not(C)
A= @$inputRB= ref $inputR->[0]C= not $inputR->[0] =~ /^-/
ABCresult
0XX0
1000
11X1
1011
43 } # end _has_optional_parameter
44  
45 #---------------------------------------------------------------------
46 sub required_parameter
47 25 ▶{
Subroutine required_parameter: called CC=2 SCAR=7.1
Pod undocumented
48 25 my ($inputR, $error_message) = @_;
49  
50 25 ▶ croak($error_message) unless defined(my $val = shift @$inputR);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $val = shift @$inputR) 0 25 25
51  
52 25 $val;
53 } # end required_parameter
54  
55 #---------------------------------------------------------------------
56 sub add_value
57 57 ▶{
Subroutine add_value: called CC=1 SCAR=0.0
Pod undocumented
58 57 my ($valuesR, $value) = @_;
59  
60 57 push @$valuesR, $value;
61  
62 57 '$val' . $#$valuesR;
63 } # end add_value
64  
65 #---------------------------------------------------------------------
66 sub join_expressions
67 51 ▶{
Subroutine join_expressions: called CC=2 SCAR=6.9
Pod undocumented
68 51 my ($op, $expressionsR) = @_;
69  
70 51 ▶ return @$expressionsR unless @$expressionsR > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @$expressionsR > 1 4 47 51
71  
72 47 '(' . join("\n $op ", @$expressionsR) . "\n )";
73 } # end join_expressions
74  
75 #---------------------------------------------------------------------
76 # Supports only string, Regexp, or arrayref of either.
77 # Nested arrayrefs should work, but are not documented.
78  
79 sub smart_match
80 67 ▶{
Subroutine smart_match: called CC=6 SCAR=18.6
Pod undocumented
81 67 my ($valuesR, $value, $match) = @_;
82  
83 TEST_REF: {
84 67 my $ref = ref $match;
85  
86 67 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 10 57 67
elsif ($ref) { } 17 40 57
87 10 my $count = @$match;
88 10 ▶ if ($count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count == 0) { } 2 8 10
elsif ($count == 1) { } 0 8 8
89 2 return '0'; # Empty array never matches
90 } elsif ($count == 1) {
91 0 $match = $match->[0];
92 0 redo TEST_REF;
93 } else {
94 8 my $exp = join_expressions('or',
95 [ map smart_match($valuesR, '$v', $_), @$match ]
96 );
97 8 ▶ if ($value eq '$v') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '$v') { } 0 8 8
98 0 return $exp;
99 } else {
100 8 return sprintf 'do { my $v = %s; %s }', $value, $exp;
101 }
102 }
103 } elsif ($ref) {
104 17 return "$value =~ " . add_value($valuesR, $match);
105 } else {
106 40 return "$value eq " . add_value($valuesR, $match);
107 }
108 }
109  
110 0 die "Can't reach";
111 } # end smart_match
112  
113 #---------------------------------------------------------------------
114 sub conjunction_action
115 9 ▶{
Subroutine conjunction_action: called CC=2 SCAR=7.0
Pod undocumented
116 9 my ($op, $valuesR, $inputR) = @_;
117  
118 9 my $arrayR = shift @$inputR;
119 9 ▶ croak "Expected arrayref for -$op, got $arrayR"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arrayR eq "ARRAY" 0 9 9
120 unless ref($arrayR) eq 'ARRAY';
121  
122 9 my @expressions;
123 9 build_selector($valuesR, \@expressions, @$arrayR);
124  
125 9 join_expressions($op, \@expressions);
126 } # end conjunction_action
127  
128 #---------------------------------------------------------------------
129 sub region_action
130 10 ▶{
Subroutine region_action: called CC=4 SCAR=13.9
Pod undocumented
131 10 my ($valuesR, $inputR, $pod) = @_;
132  
133 10 my @expressions = type_action(qw(isa Element::Pod5::Region));
134  
135 10 ▶ push @expressions, ($pod ? '' : 'not ') . '$para->is_pod'
Branch: 2 branches
BranchTrueFalseTotal
if defined $pod 6 4 10
$pod ? : 4 2 6
136 if defined $pod;
137  
138 10 ▶ if (_has_optional_parameter($inputR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_has_optional_parameter($inputR)) 4 6 10
139 4 push @expressions, smart_match($valuesR, '$para->format_name',
140 shift @$inputR);
141 } # end if specific format(s) listed
142  
143 10 join_expressions(and => \@expressions);
144 } # end region_action
145  
146 #---------------------------------------------------------------------
147 sub type_action
148 44 ▶{
Subroutine type_action: called CC=1 SCAR=0.0
Pod undocumented
149 44 my ($check, $class) = @_;
150  
151 44 "\$para->$check('Pod::Elemental::$class')";
152 } # end type_action
153  
154 #---------------------------------------------------------------------
155 our %action = (
156 3 ▶ -and => sub { conjunction_action(and => @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 6 ▶ -or => sub { conjunction_action(or => @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
158 3 ▶ -blank => sub { type_action(qw(isa Element::Generic::Blank)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
159 7 ▶ -flat => sub { type_action(qw(does Flat)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
160 0 ▶ -node => sub { type_action(qw(does Node)) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
161  
162 0 ▶ -code => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
163 0 my ($valuesR, $inputR) = @_;
164  
165 0 my $name = add_value($valuesR,
166 required_parameter($inputR, "-code requires a value"));
167 0 "$name->(\$para)";
168 }, #end -code
169  
170 24 ▶ -command => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
171 24 my ($valuesR, $inputR) = @_;
172  
173 24 my @expressions = type_action(qw(does Command));
174  
175 24 ▶ if (_has_optional_parameter($inputR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_has_optional_parameter($inputR)) 22 2 24
176 22 push @expressions, smart_match($valuesR, '$para->command', shift @$inputR);
177 } # end if specific command(s) listed
178  
179 24 join_expressions(and => \@expressions);
180 }, #end -command
181  
182 25 ▶ -content => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
183 25 my ($valuesR, $inputR) = @_;
184  
185 25 smart_match($valuesR, '$para->content',
186 required_parameter($inputR, "-content requires a value"));
187 }, #end -content
188  
189 -region => \&region_action,
190 4 ▶ -podregion => sub { region_action(@_, 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
191 2 ▶ -nonpodregion => sub { region_action(@_, 0) },
Subroutine __ANON__: called CC=1 SCAR=0.0
192 3 ); # end %action
193  
194  
195 #---------------------------------------------------------------------
196 sub build_selector
197 51 ▶{
Subroutine build_selector: called CC=3 SCAR=11.0
Pod undocumented
198 51 my $valuesR = shift;
199 51 my $expR = shift;
200  
201 51 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 78 51 129
202 78 my $type = shift;
203  
204 78 ▶ my $action = $action{$type}
Branch: 1 branch
BranchTrueFalseTotal
my $action = $Pod::Elemental::MakeSelector::action{$type} or croak("Expected selector type, got $type") 78 0 78
205 or croak "Expected selector type, got $type";
206  
207 78 push @$expR, $action->($valuesR, \@_);
208 } # end while more selectors
209 } # end build_selector
210 #---------------------------------------------------------------------
211  
212 # FIXME: These subs will be documented when I figure out how
213 # make_selector should be extended.
214  
215  
216 sub make_selector
217 42 ▶{
Subroutine make_selector: called CC=3 SCAR=11.9
Pod documented
218 42 my @values;
219 my @expressions;
220  
221 42 build_selector(\@values, \@expressions, @_);
222  
223 42 my $code = ("sub { my \$para = shift; return (\n " .
224 join("\n and ", @expressions) .
225 "\n)}\n");
226  
227 $code = sprintf("my (%s) = \@values;\n\n%s",
228 42 ▶ join(', ', map { '$val' . $_ } 0 .. $#values),
Branch: 1 branch
BranchTrueFalseTotal
if @values 28 14 42
229 $code)
230 if @values;
231  
232 #print STDERR $code;
233 42 my ($sub, $err);
234 {
235 42 local $@;
236 42 $sub = eval $code;
237 42 $err = $@;
238 }
239  
240 42 ▶ unless (ref $sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $sub) 0 42 42
241 0 my $lineNum = ($code =~ tr/\n//);
242 0 my $fmt = '%' . length($lineNum) . 'd: ';
243 0 $lineNum = 0;
244 0 $code =~ s/^/sprintf $fmt, ++$lineNum/gem;
245  
246 0 die "Building selector failed:\n$code$err";
247 }
248  
249 42 $sub;
250 } # end make_selector
251  
252 #=====================================================================
253 # Package Return Value:
254  
255 3 1;
256  
257 __END__
↑ Summary