All/IO/Base.pm

filter: Statementstmt 92.0% 58 / 63 Branchbran 68.7% 11 / 16 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 6 totaltotal 79.8% 83 / 104 CC 10 SCAR 8.4
CC
10
Cov
85%
_add_field
6.0
putrecords
3.0
getrecords
3.0
CRAP 2.3
« All/IO/File.pm ↑ Summary All/Format/Fixed.pm »
1 package Data::All::IO::Base;
2  
3 # Base package for all format modules
4  
5 # $Id: Base.pm,v 1.1.1.1 2005/05/10 23:56:20 dmandelbaum Exp $
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 1 ▶use Data::All::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::All::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = 0.13;
15  
16 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 our @EXPORT = qw(new internal attribute populate error init _load_format
18 getrecords putrecords array_to_hash hash_to_array getrecord_hash
19 _add_field
20 );
21  
22 # Interface
23 sub count();
24 sub array_to_hash(\@);
25  
26  
27  
28  
29 sub _add_field()
30 0 ▶{
Subroutine _add_field: not called CC=2 SCAR=17.9
31 0 my ($self, $name) = @_;
32
33 0 ▶ return if (defined($self->__added_fields()->{'_ORGINAL'}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->__added_fields->{"_ORGINAL"} 0 0 0
34
35 0 unshift(@{ $self->fields() }, '_ORIGINAL');
36 0 $self->__added_fields()->{'_ORGINAL'}++;
37 }
38  
39 sub array_to_hash(\@)
40 6 ▶{
Subroutine array_to_hash: called CC=2 SCAR=7.0
Pod undocumented
41 6 my ($self, $record) = @_;
42 6 my %hash;
43
44 6 ▶ $self->_add_field('_ORIGINAL') if ($self->ioconf->{'with_original'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->ioconf->{"with_original"} 0 6 6
45
46 6 my @fields = @{ $self->fields() };
47 6 @hash{ @fields } = @{ $record };
48
49 6 return \%hash;
50 }
51  
52 sub hash_to_array()
53 3 ▶{
Subroutine hash_to_array: called CC=1 SCAR=0.0
Pod undocumented
54 3 my ($self, $hash) = @_;
55 3 return [@{ $hash }{@{ $self->fields() }}];
56 }
57  
58  
59 sub getrecord_hash()
60 8 ▶{
Subroutine getrecord_hash: called CC=2 SCAR=6.9
Pod undocumented
61 8 my $self = shift;
62 8 my $rec = $self->getrecord_array($self->ioconf->{'with_original'});
63  
64 8 ▶ return ($rec)
Branch: 1 branch
BranchTrueFalseTotal
$rec ? : 6 2 8
65 ? $self->array_to_hash($rec)
66 : undef;
67 }
68  
69 sub getrecords(;$$)
70 2 ▶{
Subroutine getrecords: called CC=3 SCAR=11.0
Pod undocumented
71 2 my $self = shift;
72 # TODO: Enable running COUNT records only
73  
74 2 my (@records);
75
76 #warn ' -> using fields:', join(',', @{ $self->fields });
77
78 2 ▶ while (my $record = $self->getrecord_hash())
Branch: 1 branch
BranchTrueFalseTotal
if (my $record = $self->getrecord_hash) 6 2 8
79 {
80 6 push(@records, $record);
81 }
82
83 2 ▶ return wantarray ? @records : \@records;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
84 }
85  
86  
87  
88 sub putrecords()
89 1 ▶{
Subroutine putrecords: called CC=3 SCAR=11.0
Pod undocumented
90 1 my $self = shift;
91 1 my ($records, $options) = @_;
92  
93 1 my $start = 0;
94 1 my $count = $#{ $records }+1;
95  
96 1 ▶ die("$self->putrecords() needs records") unless ($#{ $records }+1);
Branch: 1 branch
BranchTrueFalseTotal
unless $#{$records;} + 1 0 1 1
97
98 #warn "Writing $count records from $start";
99
100 1 my $record;
101 1 ▶ while ($count--)
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 3 1 4
102 {
103 3 $self->putrecord($records->[ $start++ ], $options);
104 }
105 }
106  
107  
108 sub _load_format()
109 4 ▶{
Subroutine _load_format: called CC=2 SCAR=7.6
110 4 my $self = shift;
111 4 ▶ my $format = shift || $self->format();
Condition33%1/3
shift() || $self->format
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() || $self->format
shift() $self->format
Truth table33%1/3
A || B
A= shift()B= $self->format
ABresult
1X1
011
000
112
113 4 return Data::All::Format->new($format->{'type'}, $format);
114 }
115  
116  
117 sub init()
118 # Called in Data::All::IO::new
119 # TODO: Create Format::Hash
120 4 ▶{
Subroutine init: called CC=2 SCAR=7.0
Pod undocumented
121 4 my ($self, $args) = @_;
122 1 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
123
124 4 populate $self => $args;
125
126 $self->__FORMAT($self->_load_format())
127 # Override the loading of a Format reader for Hash types
128 4 ▶ unless ($self->ioconf()->{'type'} eq 'db');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ioconf->{"type"} eq "db" 4 0 4
129
130 4 return $self;
131 }
132  
133  
134 1 1;
135  
« All/IO/File.pm ↑ Summary All/Format/Fixed.pm »