line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Fix::Condition::SimpleAllTest; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
1813
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
6
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use Moo::Role; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
9
|
|
8
|
1
|
|
|
1
|
|
491
|
use namespace::clean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
with 'Catmandu::Fix::Condition'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
requires 'path'; |
13
|
|
|
|
|
|
|
requires 'emit_test'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub emit { |
16
|
0
|
|
|
0
|
0
|
|
my ($self, $fixer, $label) = @_; |
17
|
0
|
|
|
|
|
|
my $path = $fixer->split_path($self->path); |
18
|
0
|
|
|
|
|
|
my $key = pop @$path; |
19
|
|
|
|
|
|
|
|
20
|
0
|
|
|
|
|
|
my $pass_fixes = $self->pass_fixes; |
21
|
0
|
|
|
|
|
|
my $fail_fixes = $self->fail_fixes; |
22
|
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
my $fail_label; |
24
|
|
|
|
|
|
|
my $fail_block = $fixer->emit_block( |
25
|
|
|
|
|
|
|
sub { |
26
|
0
|
|
|
0
|
|
|
$fail_label = shift; |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
$fixer->emit_fixes($fail_fixes); |
29
|
|
|
|
|
|
|
} |
30
|
0
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
0
|
|
|
|
|
|
my $has_match_var = $fixer->generate_var; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
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
|
0
|
|
|
0
|
|
|
my $var = shift; |
41
|
|
|
|
|
|
|
$fixer->emit_get_key( |
42
|
|
|
|
|
|
|
$var, $key, |
43
|
|
|
|
|
|
|
sub { |
44
|
0
|
|
|
|
|
|
my $var = shift; |
45
|
0
|
|
|
|
|
|
my $perl = "${has_match_var} ||= 1;"; |
46
|
0
|
|
|
|
|
|
$perl .= "unless (" |
47
|
|
|
|
|
|
|
. $self->emit_test($var, $fixer) . ") {"; |
48
|
0
|
0
|
|
|
|
|
if (@$fail_fixes) { |
49
|
0
|
|
|
|
|
|
$perl .= "goto ${fail_label};"; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
0
|
|
|
|
|
|
$perl .= "last ${label};"; |
53
|
|
|
|
|
|
|
} |
54
|
0
|
|
|
|
|
|
$perl .= "}"; |
55
|
0
|
|
|
|
|
|
$perl; |
56
|
|
|
|
|
|
|
} |
57
|
0
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
} |
59
|
0
|
|
|
|
|
|
); |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
$perl .= "if (${has_match_var}) {"; |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
$perl .= $fixer->emit_fixes($pass_fixes); |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
$perl .= "last ${label};"; |
66
|
0
|
|
|
|
|
|
$perl .= "}"; |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
if (@$fail_fixes) { |
69
|
0
|
|
|
|
|
|
$perl .= $fail_block; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$perl; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
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 |