App-NDTools
view release on metacpan or search on metacpan
lib/App/NDTools/NDProc/Module/Insert.pm view on Meta::CPAN
package App::NDTools::NDProc::Module::Insert;
use strict;
use warnings FATAL => 'all';
use parent 'App::NDTools::NDProc::Module';
use Log::Log4Cli;
use Scalar::Util qw(looks_like_number);
use Struct::Path 0.80 qw(path);
use App::NDTools::Util qw(chomp_evaled_error);
use App::NDTools::Slurp qw(s_decode);
our $VERSION = '0.20';
sub MODINFO { "Insert value into structure" }
sub arg_opts {
my $self = shift;
return (
$self->SUPER::arg_opts(),
'boolean=s' => sub {
if ($_[1] eq '1' or $_[1] =~ /^(T|t)rue$/) {
$self->{OPTS}->{value} = JSON::true;
} elsif ($_[1] eq '0' or $_[1] =~ /^(F|f)alse$/) {
$self->{OPTS}->{value} = JSON::false;
} else {
$self->{ARG_ERROR} = "Unsuitable value for --boolean";
die "!FINISH";
}
},
'file|f=s' => \$self->{OPTS}->{file},
'file-fmt=s' => \$self->{OPTS}->{'file-fmt'},
'null|undef' => sub { $self->{OPTS}->{value} = undef },
'number=s' => sub {
if (looks_like_number($_[1])) {
$self->{OPTS}->{value} = 0 + $_[1];
} else {
$self->{ARG_ERROR} = "Unsuitable value for --number";
die "!FINISH";
}
},
'string|value=s' => sub { $self->{OPTS}->{value} = $_[1] },
'structure=s' => \$self->{OPTS}->{structure},
)
}
sub check_rule {
my ($self, $rule) = @_;
my $out = $self;
unless (exists $rule->{value}) {
log_error { "Value to insert should be defined" };
$out = undef;
}
push @{$rule->{path}}, '' unless (@{$rule->{path}});
return $out;
}
sub configure {
my $self = shift;
$self->{OPTS}->{value} =
s_decode(delete $self->{OPTS}->{structure}, 'JSON')
if (defined $self->{OPTS}->{structure});
$self->{OPTS}->{value} =
$self->load_struct($self->{OPTS}->{file}, $self->{OPTS}->{'file-fmt'})
if (defined $self->{OPTS}->{file});
}
sub process_path {
my ($self, $data, $path, $spath, $opts) = @_;
log_info { 'Updating path "' . $path . '"' };
eval { path(${$data}, $spath, assign => $opts->{value}, expand => 1) };
die_fatal "Failed to lookup path '$path' (" .
chomp_evaled_error($@) . ")", 4 if ($@);
}
1; # End of App::NDTools::NDProc::Module::Insert
__END__
=head1 NAME
Insert - Insert value into structure
=head1 OPTIONS
=over 4
=item B<--[no]blame>
( run in 0.769 second using v1.01-cache-2.11-cpan-39bf76dae61 )