| 1 | package DataFlow::Proc::DBF; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use MooseX::Aliases; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | extends 'DataFlow::Proc::Converter'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use XBase; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use File::Temp qw(:seekable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use File::Spec (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has '+converter' => ( | ||||||||||||||||||||||||||||
| 19 | lazy => 1, | ||||||||||||||||||||||||||||
| 20 | 4 | ▶ | default => sub { return XBase->new }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | handles => { | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | dbf => sub { shift->converter(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
dbf: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | dbf_opts => sub { shift->converter_opts(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Subroutine
dbf_opts: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | has_dbf_opts => sub { shift->has_converter_opts }, | ||||||||||||||||||||||||||
|
Subroutine
has_dbf_opts: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 25 | }, | ||||||||||||||||||||||||||||
| 26 | 2 | init_arg => 'dbf', | |||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | has '+converter_opts' => ( 'init_arg' => 'dbf_opts', ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | has 'header' => ( | ||||||||||||||||||||||||||||
| 32 | 'is' => 'rw', | ||||||||||||||||||||||||||||
| 33 | 'isa' => 'ArrayRef[Maybe[Str]]', | ||||||||||||||||||||||||||||
| 34 | 'predicate' => 'has_header', | ||||||||||||||||||||||||||||
| 35 | 'alias' => 'headers', | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | 'handles' => { 'has_headers' => sub { shift->has_header }, }, | ||||||||||||||||||||||||||
|
Subroutine
has_headers: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 37 | 2 | ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has 'header_wanted' => ( | ||||||||||||||||||||||||||||
| 42 | 'is' => 'rw', | ||||||||||||||||||||||||||||
| 43 | 'isa' => 'Bool', | ||||||||||||||||||||||||||||
| 44 | 'lazy' => 1, | ||||||||||||||||||||||||||||
| 45 | 3 | ▶ | 'default' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 3 | ▶ | return 0 if $self->direction eq 'CONVERT_FROM'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | return $self->has_header; | |||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||
| 50 | 2 | ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub _policy { | ||||||||||||||||||||||||||
|
Subroutine
_policy: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 4 | ▶ | return shift->direction eq 'CONVERT_TO' ? 'ArrayRef' : 'Scalar'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | ▶ | sub _build_subs { | ||||||||||||||||||||||||||
|
Subroutine
_build_subs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | return { | ||||||||||||||||||||||||||||
| 60 | 2 | ▶ | 'CONVERT_TO' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 61 | 2 | my $data = $_; | |||||||||||||||||||||||||||
| 62 | 2 | ▶ | my $options = $self->has_converter_opts | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | ? $self->converter_opts : {} | ||||||||||||||||||||||||||||
| 64 | ; | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | unless (exists $options->{'name'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 2 | $options->{'dir'} = File::Temp->newdir( CLEANUP => 1 ); | |||||||||||||||||||||||||||
| 68 | 2 | $options->{'name'} = File::Spec->catfile($options->{'dir'}, 'tmp.dbf'); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # header is mandatory, so we either | ||||||||||||||||||||||||||||
| 72 | # use one provided by the user, | ||||||||||||||||||||||||||||
| 73 | # or create our own "fake" version | ||||||||||||||||||||||||||||
| 74 | 2 | my $field_names; | |||||||||||||||||||||||||||
| 75 | 2 | ▶ | if ($self->header_wanted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | $self->header_wanted(0); | |||||||||||||||||||||||||||
| 77 | 1 | $field_names = $self->header; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||||||||||
| 80 | push @$field_names, "item$_" | ||||||||||||||||||||||||||||
| 81 | 1 | foreach ( 0 .. $#{ $data->[0] } ); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | my $table = $self->converter->create( | ||||||||||||||||||||||||||||
| 85 | 2 | ▶ | name => $options->{'name'}, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | field_names => $field_names, | ||||||||||||||||||||||||||||
| 87 | field_types => [], | ||||||||||||||||||||||||||||
| 88 | field_lengths => [], | ||||||||||||||||||||||||||||
| 89 | field_decimals => [], | ||||||||||||||||||||||||||||
| 90 | ) or die 'error creating DBF: ' . $self->converter->errstr; | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | foreach my $i ( 0 .. $#{$data} ) { | |||||||||||||||||||||||||||
| 93 | 4 | $table->set_record($i, @{ $data->[$i] } ); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | $table->close; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # temporary DBF file saved. Get the content back | ||||||||||||||||||||||||||||
| 99 | 2 | open my $fh, '<', $options->{'name'}; | |||||||||||||||||||||||||||
| 100 | 2 | binmode $fh; | |||||||||||||||||||||||||||
| 101 | 2 | my $content = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 102 | 2 | return $content; | |||||||||||||||||||||||||||
| 103 | }, | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 2 | ▶ | 'CONVERT_FROM' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.3
| |||||||||||||||||||||||||||||
| 106 | 2 | my $string = $_; | |||||||||||||||||||||||||||
| 107 | 2 | ▶ | my $options = $self->has_converter_opts | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | ? $self->converter_opts : {} | ||||||||||||||||||||||||||||
| 109 | ; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 2 | my $dbf; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # if the user passes a file name or handle | ||||||||||||||||||||||||||||
| 114 | # to read from, we use it. Otherwise, we | ||||||||||||||||||||||||||||
| 115 | # assume the DBF is in a binary string | ||||||||||||||||||||||||||||
| 116 | # (the "flow") and make our interface with | ||||||||||||||||||||||||||||
| 117 | # XBase using a temp file | ||||||||||||||||||||||||||||
| 118 | my $fh; | ||||||||||||||||||||||||||||
| 119 | 2 | ▶ | unless (exists $options->{'name'} or exists $options->{'fh'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $options->{"name"} or exists $options->{"fh"}
MC/DC0%0/2
exists $options->{"name"} or exists $options->{"fh"}
exists $options->{"name"}
exists $options->{"fh"}
Truth table33%1/3
A or B
A= exists $options->{"name"}B= exists $options->{"fh"}
| |||||||||||||||||||||||||||||
| 120 | 2 | $fh = File::Temp->new( UNLINK => 1 ); | |||||||||||||||||||||||||||
| 121 | 2 | binmode $fh; | |||||||||||||||||||||||||||
| 122 | 2 | print $fh $string; | |||||||||||||||||||||||||||
| 123 | 2 | close $fh; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | $options->{name} = $fh->filename; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2 | $dbf = $self->converter; | |||||||||||||||||||||||||||
| 129 | 2 | ▶ | $dbf->open( %$options ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | or die XBase->errstr; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | my $records = $dbf->get_all_records; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | if ($self->header_wanted) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 1 | $self->header_wanted(0); | |||||||||||||||||||||||||||
| 136 | 1 | $self->header( [$dbf->field_names] ); | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 2 | $dbf->close; | |||||||||||||||||||||||||||
| 140 | 2 | return $records; | |||||||||||||||||||||||||||
| 141 | }, | ||||||||||||||||||||||||||||
| 142 | 4 | }; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | 42; | |||||||||||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||||||||||||