| 1 | package Catmandu::Fix::add_to_exporter; | ||
| 2 | |||
| 3 | 3 | ▶ | use Catmandu::Sane; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 3 | our $VERSION = '1.0201_02'; | |
| 6 | |||
| 7 | 3 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 3 | ▶ | use namespace::clean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 3 | ▶ | use Catmandu::Fix::Has; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 3 | has path => (fix_arg => 1); | |
| 12 | 3 | has exporter_name => (fix_arg => 1); | |
| 13 | 3 | has exporter_args => (fix_opt => 'collect'); | |
| 14 | 3 | has exporter => (is => 'lazy', init_arg => undef); | |
| 15 | |||
| 16 | 3 | with 'Catmandu::Fix::SimpleGetValue'; | |
| 17 | |||
| 18 | sub _build_exporter { | ||
| 19 | 3 | ▶ | my ($self) = @_; |
|
Subroutine
_build_exporter: called CC=1 SCAR=0.0
| |||
| 20 | Catmandu->exporter( | ||
| 21 | $self->exporter_name, | ||
| 22 | 3 | %{$self->exporter_args}, | |
| 23 | autocommit => 1 | ||
| 24 | ); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub emit_value { | ||
| 28 | 4 | ▶ | my ($self, $var, $fixer) = @_; |
|
Subroutine
emit_value: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 29 | 4 | my $exporter_var = $fixer->capture($self->exporter); | |
| 30 | |||
| 31 | 4 | "${exporter_var}->add(${var});" | |
| 32 | } | ||
| 33 | |||
| 34 | 3 | 1; | |
| 35 | |||
| 36 | __END__ | ||
| 37 | |||
| 38 | =pod | ||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | Catmandu::Fix::add_to_exporter - Export a record as side effect | ||
| 43 | |||
| 44 | =head1 SYNOPSIS | ||
| 45 | |||
| 46 | # Export the data field values to a CSV file | ||
| 47 | add_to_exporter(data,CSV,file => /tmp/test.txt, header => 1) | ||
| 48 | |||
| 49 | # Export the complete record into a JSON file | ||
| 50 | add_to_exporter(data,JSON,file => /tmp/test.json, pretty => 1) | ||
| 51 | |||
| 52 | # In general, export a PATH to an EXPORTER with one ore more OPT0s | ||
| 53 | add_to_exporter(PATH,EXPORTER, OPT1 => ... , OPT2 => ... , OPT3 => ... , ...) | ||
| 54 | |||
| 55 | # Use the add_to_exporter to explode an ARRAY into many records | ||
| 56 | # E.g. | ||
| 57 | # books: | ||
| 58 | # - title: Graphic Design Rules | ||
| 59 | # year: 2003 | ||
| 60 | # - title: Urban Sketching | ||
| 61 | # year: 2013 | ||
| 62 | # - title: Findus flyttar ut | ||
| 63 | # year: 2012 | ||
| 64 | # And a fix file: exporter.fix | ||
| 65 | do with(path => books) | ||
| 66 | add_to_exporter(.,JSON) | ||
| 67 | end | ||
| 68 | # You can get an output with 3 records using the command line function | ||
| 69 | catmandu convert JSON to Null --fix exporter.fix < book.json | ||
| 70 | |||
| 71 | =head1 SEE ALSO | ||
| 72 | |||
| 73 | L<Catmandu::Fix> , L<Catmandu::Exporter> | ||
| 74 | |||
| 75 | =cut | ||