| 1 | package Catmandu::TabularExporter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use Catmandu::Sane; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 5 | our $VERSION = '1.0201_02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use Catmandu::Util qw(:is :check); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub _coerce_array { | ||||||||||||||||||||||||||||
| 11 | 14 | ▶ | my $fields = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
_coerce_array: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 12 | 14 | ▶ | if (ref $fields eq 'ARRAY') {return $fields} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | if (ref $fields eq 'HASH') {return [sort keys %$fields]} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 4 | [split ',', $fields]; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | with 'Catmandu::Exporter'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | has fields => (is => 'rwp', coerce => \&_coerce_array,); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 5 | has columns => (is => 'rwp', coerce => \&_coerce_array,); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 5 | has collect_fields => (is => 'ro',); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 14 | ▶ | has header => (is => 'ro', default => sub {1}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | around add => sub { | ||||||||||||||||||||||||||||
| 30 | 37 | ▶ | my ($orig, $self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | 37 | ▶ | $self->_set_fields($data) unless $self->fields; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 37 | $orig->($self, $data); | |||||||||||||||||||||||||||
| 33 | 5 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | around add_many => sub { | ||||||||||||||||||||||||||||
| 36 | 8 | ▶ | my ($orig, $self, $many) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=29.2
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 8 | ▶ | if ($self->collect_fields && !$self->fields) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->collect_fields and not $self->fields
MC/DC50%1/2
$self->collect_fields and not $self->fields
$self->collect_fields
not $self->fields
Truth table66%2/3
A and not(B)
A= $self->collect_fieldsB= not $self->fields
| |||||||||||||||||||||||||||||
| 39 | 2 | my $coll; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | ▶ | if (is_array_ref($many)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 42 | 2 | $coll = $many; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | elsif (is_hash_ref($many)) { | ||||||||||||||||||||||||||||
| 45 | 0 | $coll = [$many]; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if (is_invocant($many)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $many = check_able($many, 'generator')->generator; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | check_code_ref($many); | |||||||||||||||||||||||||||
| 52 | 0 | $coll = []; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | while (defined(my $data = $many->())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | push @$coll, $data; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 2 | my $keys = {}; | |||||||||||||||||||||||||||
| 59 | 2 | for my $data (@$coll) { | |||||||||||||||||||||||||||
| 60 | 6 | for my $key (keys %$data) { | |||||||||||||||||||||||||||
| 61 | 10 | ▶ | $keys->{$key} ||= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$keys->{$key} ||= 1
MC/DC100%1/1
$keys->{$key} ||= 1
$keys->{$key}
Truth table100%2/2
A
A= $keys->{$key}
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 2 | $self->_set_fields($keys); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | $many = $coll; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 8 | $orig->($self, $many); | |||||||||||||||||||||||||||
| 70 | 5 | }; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 5 | 1; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | __END__ | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =pod | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 NAME | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Catmandu::TabularExporter - base role for tabular exporters like CSV | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | See L<Catmandu::Exporter> for the base functionality of this role. This role | ||||||||||||||||||||||||||||
| 85 | adds some functionality tailored to tabular or columnar exporters. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =over | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item fields | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | The fields to be mapped. Can be an arrayref, example hashref or comma | ||||||||||||||||||||||||||||
| 94 | separated string. If missing, the fields of the first record encountered will | ||||||||||||||||||||||||||||
| 95 | be used. If C<collect_fields> is true, all fields names in the record stream | ||||||||||||||||||||||||||||
| 96 | will be collected first. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =item columns | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Optional custom column labels. Can be an arrayref, example hashref or comma | ||||||||||||||||||||||||||||
| 101 | separated string. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =item collect_fields | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | See C<fields> for a description. Note that this option will cause all records | ||||||||||||||||||||||||||||
| 106 | in the stream to be buffered in memory. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item header | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Include a header with column names. Enabled by default. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =back | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||