| 1 | package Catmandu::Fix::Condition::SimpleAnyTest; | ||
| 2 | |||
| 3 | 3 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 3 | our $VERSION = '1.0201_02'; | |
| 6 | |||
| 7 | 3 | ▶ | use Moo::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 3 | with 'Catmandu::Fix::Condition'; | |
| 11 | |||
| 12 | 3 | requires 'path'; | |
| 13 | 3 | requires 'emit_test'; | |
| 14 | |||
| 15 | sub emit { | ||
| 16 | 5 | ▶ | my ($self, $fixer, $label) = @_; |
|
Subroutine
emit: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 17 | 5 | my $path = $fixer->split_path($self->path); | |
| 18 | 5 | my $key = pop @$path; | |
| 19 | |||
| 20 | my $perl = $fixer->emit_walk_path( | ||
| 21 | $fixer->var, | ||
| 22 | $path, | ||
| 23 | sub { | ||
| 24 | 5 | ▶ | my $var = shift; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 25 | $fixer->emit_get_key( | ||
| 26 | $var, $key, | ||
| 27 | sub { | ||
| 28 | 5 | ▶ | my $var = shift; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 29 | 5 | my $perl | |
| 30 | = "if (" . $self->emit_test($var, $fixer) . ") {"; | ||
| 31 | |||
| 32 | 5 | $perl .= $fixer->emit_fixes($self->pass_fixes); | |
| 33 | |||
| 34 | 5 | $perl .= "last $label;"; | |
| 35 | 5 | $perl .= "}"; | |
| 36 | 5 | $perl; | |
| 37 | } | ||
| 38 | 5 | ); | |
| 39 | } | ||
| 40 | 5 | ); | |
| 41 | |||
| 42 | 5 | $perl .= $fixer->emit_fixes($self->fail_fixes); | |
| 43 | |||
| 44 | 5 | $perl; | |
| 45 | } | ||
| 46 | |||
| 47 | 3 | 1; | |
| 48 | |||
| 49 | __END__ | ||
| 50 | |||
| 51 | =pod; | ||
| 52 | |||
| 53 | =head1 NAME | ||
| 54 | |||
| 55 | Catmandu::Fix::Condition::SimpleAllTest - Base class to ease the construction of any match conditionals | ||
| 56 | |||
| 57 | =head1 SYNOPSIS | ||
| 58 | |||
| 59 | package Catmandu::Fix::Condition::has_even | ||
| 60 | |||
| 61 | use Catmandu::Sane; | ||
| 62 | use Moo; | ||
| 63 | use Catmandu::Fix::Has; | ||
| 64 | |||
| 65 | has path => (fix_arg => 1); | ||
| 66 | |||
| 67 | with 'Catmandu::Fix::Condition::SimpleAnyTest'; | ||
| 68 | |||
| 69 | sub emit_test { | ||
| 70 | my ($self, $var) = @_; | ||
| 71 | my $value = $self->value; | ||
| 72 | "is_value(${var}) && ${var} % 2 == 0"; | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | # Now you can write in your fixes | ||
| 78 | has_even('my_field') # True when my_field is 0,2,4,6,... | ||
| 79 | has_even('my_field.*') # True when at least one my_field is 0,2,4,6,... | ||
| 80 | |||
| 81 | =head1 DESCRIPTION | ||
| 82 | |||
| 83 | The is a base class to ease the construction of Catmandu::Fix::Conditional-s. An 'any' test matches | ||
| 84 | when at least one node on a path match a condition. E.g. | ||
| 85 | |||
| 86 | any_match('title','abc') # true when the title field contains 'abc' | ||
| 87 | |||
| 88 | any_match('title.*','abc') # true when at least one title fields contain 'abc' | ||
| 89 | |||
| 90 | =head1 SEE ALSO | ||
| 91 | |||
| 92 | L<Catmandu::Fix::Condition::any_match> | ||
| 93 | |||
| 94 | =cut | ||