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