blib/lib/Catmandu/Fix/Condition/SimpleAllTest.pm

filter: Statementstmt 100% 43 / 43 Branchbran 100% 4 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 98.1% 54 / 55 CC 3 SCAR 3.7
CC
3
Cov
100%
emit
2.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.4
« blib/lib/Catmandu/Importer/Text.pm ↑ Summary blib/lib/Catmandu/Fix/Condition.pm »
1 package Catmandu::Fix::Condition::SimpleAllTest;
2  
3 14 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 14 our $VERSION = '1.0201_02';
6  
7 14 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 14 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 14 with 'Catmandu::Fix::Condition';
11  
12 14 requires 'path';
13 14 requires 'emit_test';
14  
15 sub emit {
16 23 ▶ my ($self, $fixer, $label) = @_;
Subroutine emit: called CC=2 SCAR=6.9
Pod undocumented
17 23 my $path = $fixer->split_path($self->path);
18 23 my $key = pop @$path;
19  
20 23 my $pass_fixes = $self->pass_fixes;
21 23 my $fail_fixes = $self->fail_fixes;
22  
23 23 my $fail_label;
24 my $fail_block = $fixer->emit_block(
25 sub {
26 23 ▶ $fail_label = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
27  
28 23 $fixer->emit_fixes($fail_fixes);
29 }
30 23 );
31  
32 23 my $has_match_var = $fixer->generate_var;
33  
34 23 my $perl = $fixer->emit_declare_vars($has_match_var, '0');
35  
36 $perl .= $fixer->emit_walk_path(
37 $fixer->var,
38 $path,
39 sub {
40 23 ▶ my $var = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
41 $fixer->emit_get_key(
42 $var, $key,
43 sub {
44 23 ▶ my $var = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
45 23 my $perl = "${has_match_var} ||= 1;";
46 23 $perl .= "unless ("
47 . $self->emit_test($var, $fixer) . ") {";
48 23 ▶ if (@$fail_fixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fail_fixes) { } 17 6 23
49 17 $perl .= "goto ${fail_label};";
50 }
51 else {
52 6 $perl .= "last ${label};";
53 }
54 23 $perl .= "}";
55 23 $perl;
56 }
57 23 );
58 }
59 23 );
60  
61 23 $perl .= "if (${has_match_var}) {";
62  
63 23 $perl .= $fixer->emit_fixes($pass_fixes);
64  
65 23 $perl .= "last ${label};";
66 23 $perl .= "}";
67  
68 23 ▶ if (@$fail_fixes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$fail_fixes) 17 6 23
69 17 $perl .= $fail_block;
70 }
71  
72 23 $perl;
73 }
74  
75 14 1;
76  
77 __END__
78  
79 =pod
80  
81 =head1 NAME
82  
83 Catmandu::Fix::Condition::SimpleAllTest - Base class to ease the construction of all match conditionals
84  
85 =head1 SYNOPSIS
86  
87 package Catmandu::Fix::Condition::is_even
88  
89 use Catmandu::Sane;
90 use Moo;
91 use Catmandu::Fix::Has;
92  
93 has path => (fix_arg => 1);
94  
95 with 'Catmandu::Fix::Condition::SimpleAllTest';
96  
97 sub emit_test {
98 my ($self, $var) = @_;
99 "is_value(${var}) && ${var} % 2 == 0";
100 }
101  
102 1;
103  
104 # Now you can write in your fixes
105 is_even('my_field') # True when my_field is 0,2,4,6,...
106 is_even('my_field.*') # True when all my_field's are 0,2,4,6,...
107  
108 =head1 DESCRIPTION
109  
110 The is a base class to ease the construction of Catmandu::Fix::Conditional-s. An 'all' test matches
111 when all node on a path match a condition. E.g.
112  
113 all_match('title','abc') # true when the title field contains 'abc'
114  
115 all_match('title.*','abc') # true when all title fields contain 'abc'
116  
117 =head1 SEE ALSO
118  
119 L<Catmandu::Fix::Condition::all_match>,
120 L<Catmandu::Fix::Condition::greater_than>,
121 L<Catmandu::Fix::Condition::less_than>
122  
123 =cut
« blib/lib/Catmandu/Importer/Text.pm ↑ Summary blib/lib/Catmandu/Fix/Condition.pm »