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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
package Catmandu::Fix::add_to_exporter;
use Catmandu::Sane;
our $VERSION = '1.2024';
use Moo;
use Catmandu::Util::Path qw(as_path);
use namespace::clean;
use Catmandu::Fix::Has;
has path => (fix_arg => 1);
has exporter_name => (fix_arg => 1);
has exporter_args => (fix_opt => 'collect');
has exporter => (is => 'lazy', init_arg => undef);
with 'Catmandu::Fix::Builder';
sub _build_exporter {
my ($self) = @_;
Catmandu->exporter(
$self->exporter_name,
%{$self->exporter_args},
autocommit => 1
);
}
sub _build_fixer {
my ($self) = @_;
my $exporter = $self->exporter;
my $getter = as_path($self->path)->getter;
sub {
my $data = $_[0];
my $vals = $getter->($data);
$exporter->add($_) for @$vals;
$data;
};
}
1;
__END__
=pod
=head1 NAME
Catmandu::Fix::add_to_exporter - Export a record as side effect
=head1 SYNOPSIS
# Export the data field values to a CSV file
add_to_exporter(data,CSV, file:/tmp/test.txt, header: 1)
# Export the complete record into a JSON file
add_to_exporter(data,JSON, file:/tmp/test.json, pretty:1)
# In general, export a PATH to an EXPORTER with one ore more OPT0s
add_to_exporter(PATH,EXPORTER, OPT1:... , OPT2:... , OPT3:... , ...)
# Use the add_to_exporter to explode an ARRAY into many records
# E.g.
# books:
# - title: Graphic Design Rules
# year: 2003
# - title: Urban Sketching
# year: 2013
# - title: Findus flyttar ut
# year: 2012
# And a fix file: exporter.fix
do with(path => books)
add_to_exporter(.,JSON)
end
# You can get an output with 3 records using the command line function
catmandu convert JSON to Null --fix exporter.fix < book.json
=head1 SEE ALSO
L<Catmandu::Fix> , L<Catmandu::Exporter>
=cut
|