/
any_match.pm
48 lines (35 loc) · 1.04 KB
/
any_match.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package Catmandu::FixCondition::any_match;
use Catmandu::Sane;
use Catmandu::Util qw(:data);
use Moo;
with 'Catmandu::FixCondition';
has path => (is => 'ro', required => 1);
has key => (is => 'ro', required => 1);
has pattern => (is => 'ro', required => 1);
around BUILDARGS => sub {
my ($orig, $class, $path, $pattern) = @_;
my ($p, $key) = parse_data_path($path);
$orig->($class, path => $p, key => $key, pattern => $pattern);
};
sub is_fixable {
my ($self, $data) = @_;
my $key = $self->key;
my $pattern = $self->pattern;
for my $match (grep ref, data_at($self->path, $data)) {
for my $val (get_data($match, $key)) {
return 1 if $val =~ m{$pattern};
}
}
0;
}
=head1 NAME
Catmandu::FixCondition::any_match - only execute fixes if any path value matches the given regex
=head1 SYNOPSIS
# uppercase the value of field 'foo' if field 'oogly' has the value 'doogly'
if_any_match('oogly', 'doogly');
upcase('foo'); # foo => 'BAR'
end()
=head1 SEE ALSO
L<Catmandu::Fix>
=cut
1;