Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
162 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package Catmandu::FixCondition; | ||
|
||
use Catmandu::Sane; | ||
use Moo::Role; | ||
|
||
requires 'is_fixable'; | ||
|
||
has fixes => (is => 'ro', default => sub { [] }); | ||
has invert => (is => 'rw'); | ||
|
||
sub fix { | ||
my ($self, $data) = @_; | ||
|
||
my $ok = $self->is_fixable($data); | ||
if ($self->invert) { | ||
$ok = !$ok; | ||
} | ||
|
||
if ($ok) { | ||
for my $fix (@{$self->fixes}) { | ||
$data = $fix->fix($data); | ||
} | ||
} | ||
|
||
$data | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package Catmandu::FixCondition::exists; | ||
|
||
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); | ||
|
||
around BUILDARGS => sub { | ||
my ($orig, $class, $path) = @_; | ||
my ($p, $key) = parse_data_path($path); | ||
$orig->($class, path => $p, key => $key); | ||
}; | ||
|
||
sub is_fixable { | ||
my ($self, $data) = @_; | ||
my $key = $self->key; | ||
for my $match (grep ref, data_at($self->path, $data)) { | ||
return 1 if get_data($match, $key); | ||
} | ||
0; | ||
} | ||
|
||
=head1 NAME | ||
Catmandu::FixCondition::exists - only execute fixes if the path exists | ||
=head1 SYNOPSIS | ||
# uppercase the value of field 'foo' if the field 'oogly' exists | ||
if_exists('oogly'); | ||
upcase('foo'); # foo => 'BAR' | ||
end() | ||
# inverted | ||
unless_exists('oogly'); | ||
upcase('foo'); # foo => 'bar' | ||
end() | ||
=head1 SEE ALSO | ||
L<Catmandu::Fix> | ||
=cut | ||
|
||
1; |