| 1 | #!perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ########################################################################## # | ||||||||||||||||||||||||||||
| 4 | # Title: Write data stream to tab separated stream | ||||||||||||||||||||||||||||
| 5 | # Creation date: 2007-03-05 | ||||||||||||||||||||||||||||
| 6 | # Author: Michael Zedeler | ||||||||||||||||||||||||||||
| 7 | # Description: Writes data stream to a handle (IO::Handle) | ||||||||||||||||||||||||||||
| 8 | # Data Stream class | ||||||||||||||||||||||||||||
| 9 | # Transformer | ||||||||||||||||||||||||||||
| 10 | # File: $Source: /data/cvs/lib/DSlib/lib/DS/Transformer/TabStreamWriter.pm,v $ | ||||||||||||||||||||||||||||
| 11 | # Repository: kronhjorten | ||||||||||||||||||||||||||||
| 12 | # State: $State: Exp $ | ||||||||||||||||||||||||||||
| 13 | # Documentation: inline | ||||||||||||||||||||||||||||
| 14 | # Recepient: - | ||||||||||||||||||||||||||||
| 15 | # #TODO Must store field order internally. Ask not where to get it from. | ||||||||||||||||||||||||||||
| 16 | # ########################################################################## # | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | package DS::Transformer::TabStreamWriter; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use base qw{ DS::Transformer::TypePassthrough }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | use Carp::Assert; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | our ($VERSION) = $DS::VERSION; | |||||||||||||||||||||||||||
| 27 | 1 | our ($REVISION) = '$Revision: 1.2 $' =~ /(\d+\.\d+)/; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 2 | my( $class, $fh, $field_order, $source, $target ) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | my $self = $class->SUPER::new( $source, $target ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | assert($fh->isa('IO::Handle')); | |||||||||||||||||||||||||||
| 35 | 2 | $self->{fh} = $fh; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | $self->{field_order} = $field_order; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | return $self; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 24 | ▶ | sub receive_row { | ||||||||||||||||||||||||||
|
Subroutine
receive_row: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 24 | my( $self, $row ) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # TODO (V3) This needs to be rewritten once a new typing system is in place | ||||||||||||||||||||||||||||
| 46 | # Trying to figure out field order here is just plain ugly and only | ||||||||||||||||||||||||||||
| 47 | # something we do to allow TabFileWriter to work (it needs field order to be | ||||||||||||||||||||||||||||
| 48 | # set before process is called. | ||||||||||||||||||||||||||||
| 49 | 24 | ▶ | unless( $self->{field_order} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | $self->{field_order} = [sort keys %$row]; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 24 | return $self->SUPER::receive_row( $row ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub include_header { | ||||||||||||||||||||||||||
|
Subroutine
include_header: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 0 | my( $self ) = @_; | |||||||||||||||||||||||||||
| 58 | 0 | $self->{include_header} = 1; | |||||||||||||||||||||||||||
| 59 | 0 | return; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub exclude_header { | ||||||||||||||||||||||||||
|
Subroutine
exclude_header: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | my( $self ) = @_; | |||||||||||||||||||||||||||
| 64 | 0 | $self->{include_header} = 0; | |||||||||||||||||||||||||||
| 65 | 0 | return; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 24 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=4 SCAR=15.2
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 24 | my( $self, $row) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 24 | ▶ | if( $row ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 22 | ▶ | if( $self->{include_header} and not $self->{header_included} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"include_header"} and not $self->{"header_included"}
MC/DC0%0/2
$self->{"include_header"} and not $self->{"header_included"}
$self->{"include_header"}
not $self->{"header_included"}
Truth table33%1/3
A and not(B)
A= $self->{"include_header"}B= not $self->{"header_included"}
| |||||||||||||||||||||||||||||
| 73 | 0 | $self->write_header; | |||||||||||||||||||||||||||
| 74 | 0 | $self->{header_included} = 1; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 22 | $self->{fh}->print(join("\t", @$row{@{$self->{field_order}}}), "\r\n"); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 24 | return $row; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | sub write_header { | ||||||||||||||||||||||||||
|
Subroutine
write_header: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 2 | my( $self ) = @_; | |||||||||||||||||||||||||||
| 84 | 2 | assert( $self->{field_order} ); | |||||||||||||||||||||||||||
| 85 | 2 | $self->{fh}->print(join("\t", @{$self->{field_order}}), "\r\n"); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | 1; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||