File Coverage

blib/lib/FlatFile/DataStore/Record.pm
Criterion Covered Total %
statement 55 62 88.7
branch 13 20 65.0
condition n/a
subroutine 26 27 96.3
pod 1 23 4.3
total 95 132 71.9


line stmt bran cond sub pod time code
1             #---------------------------------------------------------------------
2             package FlatFile::DataStore::Record;
3             #---------------------------------------------------------------------
4              
5             =head1 NAME
6              
7             FlatFile::DataStore::Record - Perl module that implements a flatfile
8             datastore record class.
9              
10             =head1 SYNOPSYS
11              
12             # first, create a preamble object
13              
14             use FlatFile::DataStore::Preamble;
15              
16             my $preamble = FlatFile::DataStore::Preamble->new( {
17             datastore => $ds, # FlatFile::DataStore object
18             indicator => $indicator, # single-character crud flag
19             transind => $transind, # single-character crud flag
20             date => $date, # pre-formatted date
21             transnum => $transint, # transaction number (integer)
22             keynum => $keynum, # record sequence number (integer)
23             reclen => $reclen, # record length (integer)
24             thisfnum => $fnum, # file number (in base format)
25             thisseek => $datapos, # seek position (integer)
26             prevfnum => $prevfnum, # ditto these ...
27             prevseek => $prevseek,
28             nextfnum => $nextfnum,
29             nextseek => $nextseek,
30             user => $user_data, # pre-formatted user-defined data
31             } );
32              
33             # then create a record object with the preamble contained in it
34              
35             use FlatFile::DataStore::Record;
36              
37             my $record = FlatFile::DataStore::Record->new( {
38             preamble => $preamble, # i.e., a preamble object
39             data => "This is a test record.", # actual record data
40             } );
41              
42             =head1 DESCRIPTION
43              
44             FlatFile::DataStore::Record is a Perl module that implements a flatfile
45             datastore record class. This class defines objects used by
46             FlatFile::DataStore. You will likely not ever call new() yourself,
47             (FlatFile::DataStore::create() would, e.g., do that) but you will
48             likely call the accessors.
49              
50             =head1 VERSION
51              
52             FlatFile::DataStore::Record version 1.03
53              
54             =cut
55              
56             our $VERSION = '1.03';
57              
58 23     23   51362 use 5.008003;
  23         88  
  23         1232  
59 23     23   138 use strict;
  23         49  
  23         974  
60 23     23   146 use warnings;
  23         49  
  23         758  
61              
62 23     23   131 use Carp;
  23         47  
  23         22877  
63              
64             my %Attrs = qw(
65             preamble 1
66             data 1
67             );
68              
69             #---------------------------------------------------------------------
70              
71             =head1 CLASS METHODS
72              
73             =head2 FlatFile::DataStore::Record->new( $parms )
74              
75             Constructs a new FlatFile::DataStore::Record object.
76              
77             The parm C<$parms> is a hash reference containing key/value pairs to
78             populate the record string. Two keys are recognized:
79              
80             - preamble, i.e., a FlatFile::DataStore::Preamble object
81             - data, the actual record data
82              
83             The record data is stored in the object as a scalar reference, and
84             new() will accept a scalar ref, e.g.,
85              
86             my $record_data = "This is a test record.";
87             my $record = FlatFile::DataStore::Record->new( {
88             preamble => $preamble_obj,
89             data => \$record_data,
90             } );
91              
92             =cut
93              
94             sub new {
95 147     147 1 781 my( $class, $parms ) = @_;
96              
97 147         451 my $self = bless {}, $class;
98              
99 147 50       687 $self->init( $parms ) if $parms;
100 147         438 return $self;
101             }
102              
103              
104             #---------------------------------------------------------------------
105             # init(), called by new() to parse the parms
106              
107             sub init {
108 147     147 0 247 my( $self, $parms ) = @_;
109              
110             # want to store record data as a scalar reference
111 147         346 for( $parms->{'data'} ) {
112 147 50       335 if( defined ) {
113 147 100       431 if( ref eq 'SCALAR' ) { $self->data( $_ ) }
  146         410  
114 1         4 else { $self->data( \$_ ) }
115             }
116 0         0 else { my $s = ''; $self->data( \$s ) }
  0         0  
117             }
118              
119 147 50       491 if( my $preamble = $parms->{'preamble'} ) {
120 147         450 $self->preamble( $preamble );
121             }
122            
123 147         332 return $self;
124             }
125              
126             #---------------------------------------------------------------------
127              
128             =head1 OBJECT METHODS: Accessors
129              
130             The following read/write methods set and return their respective
131             attribute values if C<$value> is given. Otherwise, they just return
132             the value.
133              
134             The value parameter to data() and dataref() may be a scalar or scalar ref.
135             The return value of data() is always the scalar value.
136             The return value of dataref() is always the scalar ref.
137              
138             The value given to dataref() should be a scalar ref, and dataref() will
139             always return a scalar ref.
140              
141             $record->data( $value );
142             $record->dataref( $value );
143              
144             $record->preamble( $value ); # FlatFile::DataStore::Preamble object
145              
146             =cut
147              
148             sub data {
149 208     208 0 3624 my( $self, $data ) = @_;
150 208 100       1076 return ${$self->{data}} unless $data;
  55         338  
151 153         279 for( $data ) {
152 153 100       408 if( ref eq 'SCALAR' ) { $self->{data} = $_ }
  147         795  
153 6         33 else { $self->{data} = \$_ }
154             }
155             }
156              
157             sub dataref {
158 22     22 0 53 my( $self, $data ) = @_;
159 22 50       113 return $self->{data} unless $data;
160 0         0 for( $data ) {
161 0 0       0 if( ref eq 'SCALAR' ) { $self->{data} = $_ }
  0         0  
162 0         0 else { $self->{data} = \$_ }
163             }
164             }
165              
166 439 100   439 0 1756 sub preamble {for($_[0]->{preamble}){$_=$_[1]if@_>1;return$_}}
  439         1046  
  439         1374  
167              
168             =pod
169              
170             The following read-only methods just return their respective values.
171             The values all come from the record's contained preamble object.
172              
173             $record->user()
174             $record->preamble_string() # the 'string' attr of the preamble
175             $record->indicator()
176             $record->transind()
177             $record->date()
178             $record->transnum()
179             $record->keynum()
180             $record->reclen()
181             $record->thisfnum()
182             $record->thisseek()
183             $record->prevfnum()
184             $record->prevseek()
185             $record->nextfnum()
186             $record->nextseek()
187              
188             $record->is_created()
189             $record->is_updated()
190             $record->is_deleted()
191              
192             =cut
193              
194 40 50   40 0 2699 sub user {for($_[0]->preamble()){defined&&return$_->user()}}
  40         327  
195              
196 150     150 0 1663 sub preamble_string {$_[0]->preamble()->string()}
197              
198 0     0 0 0 sub datastore {$_[0]->preamble()->datastore()}
199 3     3 0 2448 sub indicator {$_[0]->preamble()->indicator()}
200 2     2 0 1550 sub transind {$_[0]->preamble()->transind() }
201 2     2 0 1779 sub date {$_[0]->preamble()->date() }
202 2     2 0 1172 sub transnum {$_[0]->preamble()->transnum() }
203 31     31 0 1932 sub keynum {$_[0]->preamble()->keynum() }
204 2     2 0 1682 sub reclen {$_[0]->preamble()->reclen() }
205 3     3 0 1287 sub thisfnum {$_[0]->preamble()->thisfnum() }
206 3     3 0 1819 sub thisseek {$_[0]->preamble()->thisseek() }
207 2     2 0 589 sub prevfnum {$_[0]->preamble()->prevfnum() }
208 2     2 0 461 sub prevseek {$_[0]->preamble()->prevseek() }
209 1     1 0 396 sub nextfnum {$_[0]->preamble()->nextfnum() }
210 1     1 0 391 sub nextseek {$_[0]->preamble()->nextseek() }
211              
212 4     4 0 1190 sub is_created {$_[0]->preamble()->is_created() }
213 3     3 0 205 sub is_updated {$_[0]->preamble()->is_updated() }
214 6     6 0 34 sub is_deleted {$_[0]->preamble()->is_deleted() }
215              
216             __END__