Skip to content

Commit

Permalink
reverse builder version
Browse files Browse the repository at this point in the history
  • Loading branch information
nics committed Jul 6, 2017
1 parent 446a7f4 commit e35b8d8
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/Catmandu/Fix/reverse.pm
Expand Up @@ -4,21 +4,28 @@ use Catmandu::Sane;

our $VERSION = '1.0602';

use Catmandu::Util qw(is_string is_array_ref);
use Moo;
use namespace::clean;
use Catmandu::Fix::Has;

has path => (fix_arg => 1);

with 'Catmandu::Fix::SimpleGetValue';

sub emit_value {
my ($self, $var, $fixer) = @_;

"if (is_array_ref(${var})) {"
. "${var} = [reverse(\@{${var}})];" . "}"
. "elsif (is_string(${var})) {"
. "${var} = scalar(reverse(${var}));" . "}";
with 'Catmandu::Fix::Base';

sub BUILD {
my ($self) = @_;
my $builder = $self->builder;
$builder->get($self->path)->update(sub {
my $val = $_[0];
if (is_array_ref($val)) {
[reverse(@$val)];
} elsif (is_string($val)) {
scalar(reverse($val));
} else {
$builder->cancel;
}
});
}

1;
Expand Down

0 comments on commit e35b8d8

Please sign in to comment.