File Coverage

blib/lib/Data/All/IO/Base.pm
Criterion Covered Total %
statement 55 60 91.6
branch 7 12 58.3
condition 1 3 33.3
subroutine 13 14 92.8
pod 0 6 0.0
total 76 95 80.0


line stmt bran cond sub pod time code
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     1   6 use strict;
  1         3  
  1         37  
8 1     1   5 use warnings;
  1         2  
  1         26  
9              
10              
11 1     1   5 use Data::All::Base;
  1         2  
  1         86  
12 1     1   618 use Data::All::Format;
  1         4  
  1         41  
13              
14             our $VERSION = 0.13;
15              
16 1     1   7 use base 'Exporter';
  1         3  
  1         707  
17             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             {
31 0     0   0 my ($self, $name) = @_;
32            
33 0 0       0 return if (defined($self->__added_fields()->{'_ORGINAL'}));
34            
35 0         0 unshift(@{ $self->fields() }, '_ORIGINAL');
  0         0  
36 0         0 $self->__added_fields()->{'_ORGINAL'}++;
37             }
38              
39             sub array_to_hash(\@)
40             {
41 6     6 0 11 my ($self, $record) = @_;
42 6         98 my %hash;
43            
44 6 50       16 $self->_add_field('_ORIGINAL') if ($self->ioconf->{'with_original'});
45            
46 6         10 my @fields = @{ $self->fields() };
  6         17  
47 6         10 @hash{ @fields } = @{ $record };
  6         47  
48            
49 6         33 return \%hash;
50             }
51              
52             sub hash_to_array()
53             {
54 3     3 0 5 my ($self, $hash) = @_;
55 3         4 return [@{ $hash }{@{ $self->fields() }}];
  3         19  
  3         9  
56             }
57              
58              
59             sub getrecord_hash()
60             {
61 8     8 0 11 my $self = shift;
62 8         28 my $rec = $self->getrecord_array($self->ioconf->{'with_original'});
63              
64 8 100       31 return ($rec)
65             ? $self->array_to_hash($rec)
66             : undef;
67             }
68              
69             sub getrecords(;$$)
70             {
71 2     2 0 7 my $self = shift;
72             # TODO: Enable running COUNT records only
73              
74 2         5 my (@records);
75            
76             #warn ' -> using fields:', join(',', @{ $self->fields });
77            
78 2         7 while (my $record = $self->getrecord_hash())
79             {
80 6         15 push(@records, $record);
81             }
82            
83 2 100       13 return wantarray ? @records : \@records;
84             }
85              
86              
87              
88             sub putrecords()
89             {
90 1     1 0 2 my $self = shift;
91 1         2 my ($records, $options) = @_;
92              
93 1         3 my $start = 0;
94 1         2 my $count = $#{ $records }+1;
  1         12  
95              
96 1 50       2 die("$self->putrecords() needs records") unless ($#{ $records }+1);
  1         6  
97            
98             #warn "Writing $count records from $start";
99            
100 1         2 my $record;
101 1         8 while ($count--)
102             {
103 3         12 $self->putrecord($records->[ $start++ ], $options);
104             }
105             }
106              
107              
108             sub _load_format()
109             {
110 4     4   6 my $self = shift;
111 4   33     20 my $format = shift || $self->format();
112            
113 4         31 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             {
121 4     4 0 20 my ($self, $args) = @_;
122 1     1   7 use Data::Dumper;
  1         10  
  1         139  
123            
124 4         15 populate $self => $args;
125            
126 4 50       15 $self->__FORMAT($self->_load_format())
127             # Override the loading of a Format reader for Hash types
128             unless ($self->ioconf()->{'type'} eq 'db');
129            
130 4         21 return $self;
131             }
132              
133              
134             1;
135