blib/lib/Log/Shiras/Report/CSVFile.pm untested

filter: Statementstmt 0.0% 0 / 251 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 379 CC 26 SCAR 31.3
CC
26
Cov
0%
_build_message_from_arrayref
56.0
_build_message_from_hashref
56.0
_test_headers
42.0
CRAP 22.8
1 package Log::Shiras::Report::CSVFile;
2 our $AUTHORITY = 'cpan:JANDREW';
3 use version; our $VERSION = version->declare("v0.48.0");
4 use strict;
5 use warnings;
6 use 5.010;
7 use utf8;
8 use Moose;
9 use namespace::autoclean;
10 use MooseX::StrictConstructor;
11 use MooseX::HasDefaults::RO;
12 use lib '../../../';
13 #~ use Log::Shiras::Unhide qw( :InternalReporTCSV );
14 ###InternalReporTCSV warn "You uncovered internal logging statements for Log::Shiras::Report::CSVFile-$VERSION" if !$ENV{hide_warn};
15 ###InternalReporTCSV use Log::Shiras::Switchboard;
16 ###InternalReporTCSV my $switchboard = Log::Shiras::Switchboard->instance;
17 use Text::CSV_XS 1.25;
18 use File::Copy qw( copy );
19 use File::Temp;
20 #~ $File::Temp::DEBUG = 1;
21 use Carp qw( confess cluck );
22 use Fcntl qw( :flock LOCK_EX LOCK_UN SEEK_END);#
23 use MooseX::Types::Moose qw(
24 FileHandle ArrayRef HashRef Str Bool
25 );
26 use Log::Shiras::Types qw( HeaderArray HeaderString CSVFile IOFileType );
27 with 'Log::Shiras::LogSpace';
28  
29 #########1 Public Attributes 3#########4#########5#########6#########7#########8#########9
30  
31 has file =>(
32 isa => CSVFile,
33 writer => 'set_file_name',
34 reader => 'get_file_name',
35 clearer => '_clear_file',
36 predicate => '_has_file',
37 required => 1,
38 coerce => 1,
39 );
40
41 has headers =>(
42 isa => HeaderArray,
43 traits =>['Array'],
44 writer => 'set_headers',
45 reader => 'get_headers',
46 predicate => 'has_headers',
47 clearer => '_clear_headers',
48 handles =>{
49 number_of_headers => 'count',
50 },
51 coerce => 1,
52 );
53
54 has reconcile_headers =>(
55 isa => Bool,
56 writer => 'set_reconcile_headers',
57 reader => 'should_reconcile_headers',
58 default => 1,
59 );
60  
61 has test_first_row =>(
62 isa => Bool,
63 writer => '_test_first_row',
64 reader => 'should_test_first_row',
65 default => 1,
66 );
67  
68 #########1 Public Methods 3#########4#########5#########6#########7#########8#########9
69  
70 sub add_line{
71  
72 my ( $self, $input_ref ) = @_;
73 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
74 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line' ),
75 ###InternalReporTCSV message =>[ 'Adding a line to the csv file -' . $self->get_file_name . '- :', $input_ref ], } );
76 my $message_ref;
77 my( $first_ref, @other_args ) = @{$input_ref->{message}};
78 if( !$first_ref ){
79 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
80 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_find_the_actual_message' ),
81 ###InternalReporTCSV message =>[ 'No data in the first position - adding an empty row' ], } );
82 $message_ref = $self->_build_message_from_arrayref( [] );
83 }elsif( @other_args ){
84 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
85 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_find_the_actual_message' ),
86 ###InternalReporTCSV message =>[ 'Multiple values passed - treating the inputs like a list' ], } );
87 $message_ref = $self->_build_message_from_arrayref( [ $first_ref, @other_args ] );
88 }elsif( is_HashRef( $first_ref ) ){
89 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
90 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_find_the_actual_message' ),
91 ###InternalReporTCSV message =>[ 'Using the ref as it stands:', $first_ref ], } );
92 $message_ref = $self->_build_message_from_hashref( $first_ref );
93 }else{
94 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
95 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_find_the_actual_message' ),
96 ###InternalReporTCSV message =>[ 'Treating the input as a one element string' ], } );
97 $message_ref = $self->_build_message_from_arrayref( [ $first_ref ] );
98 }
99 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
100 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line' ),
101 ###InternalReporTCSV message =>[ "committing the message:", $message_ref ], } );
102 $self->_send_array_ref( $self->_get_file_handle, $message_ref );
103
104 return 1;
105 }
106  
107 sub get_class_space{ 'CSVFile' }
108  
109 #########1 Private Attributes 3#########4#########5#########6#########7#########8#########9
110  
111 has _file_handle =>(
112 isa => IOFileType,
113 writer => '_set_file_handle',
114 reader => '_get_file_handle',
115 clearer => '_clear_file_handle',
116 predicate => '_has_file_handle',
117 init_arg => undef,
118 );
119  
120 has _file_headers =>(
121 isa => HeaderArray,
122 traits =>['Array'],
123 writer => '_set_file_headers',
124 reader => '_get_file_headers',
125 clearer => '_clear_file_headers',
126 predicate => '_has_file_headers',
127 handles =>{
128 _file_header_count => 'count',
129 },
130 init_arg => undef,
131 );
132  
133 has _expected_header_lookup =>(
134 isa => HashRef,
135 traits =>['Hash'],
136 writer => '_set_header_lookup',
137 reader => '_get_header_lookup',
138 clearer => '_clear_header_lookup',
139 predicate => '_has_header_lookup',
140 handles =>{
141 _get_header_position => 'get',
142 _has_header_named => 'exists',
143 },
144 init_arg => undef,
145 );
146  
147 has _csv_parser =>(
148 isa => 'Text::CSV_XS',
149 writer => '_set_csv_parser',
150 clearer => '_clear_csv_parser',
151 init_arg => undef,
152 handles =>{
153 _set_parsing_header => 'header',
154 _send_array_ref => 'say',
155 _send_hash_ref => 'print_hr',
156 _read_next_line => 'getline',
157 _separator_char => 'sep_char',
158 },
159 );
160  
161 #########1 Private Methods 3#########4#########5#########6#########7#########8#########9
162  
163 sub BUILD{
164 my( $self, ) = @_;
165 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
166 ###InternalReporTCSV name_space => $self->get_all_space( 'BUILD' ),
167 ###InternalReporTCSV message =>[ "Organizing the new file instance"], } );
168
169 # Open and collect the header if available
170 $self->_open_file( $self->get_file_name );
171 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
172 ###InternalReporTCSV name_space => $self->get_all_space( 'BUILD' ),
173 ###InternalReporTCSV message =>[ "Open file complete"], } );
174
175 # Check requested headers against an empty file
176 if( $self->has_headers ){
177 my $header_ref = $self->get_headers;
178 $self->_set_expected_header_lookup( $header_ref );
179 if( $self->should_reconcile_headers and !$self->_has_file_headers ){
180 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
181 ###InternalReporTCSV name_space => $self->get_all_space( 'BUILD' ),
182 ###InternalReporTCSV message =>[ "Ensuring the requested headers are in the file:", $header_ref ], } );
183 $self->_add_headers_to_file( $header_ref );
184 }
185 }
186 #~ confess "Died here";
187 return 1;
188 }
189  
190 after 'set_file_name' => sub{ my( $self, $file ) = @_; $self->_open_file( $file ) };
191  
192 sub _open_file{
193  
194 my ( $self, $file ) = @_;
195 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
196 ###InternalReporTCSV name_space => $self->get_all_space( '_open_file' ),
197 ###InternalReporTCSV message =>[ "Arrived at _open_file for:", $file ], } );
198 $self->_clear_file_handle;
199 $self->_clear_file_headers;
200 $self->_clear_csv_parser;
201
202 # Build the csv parser
203 $self->_set_csv_parser( Text::CSV_XS->new({ binary => 1, eol => $\, auto_diag => 1 }) );#
204
205 # Open the file handle and collect the header if available
206 open( my $fh, "+<:encoding(UTF-8)", $file ) or confess "Can't open $file: $!";
207 binmode( $fh );
208 flock( $fh, LOCK_EX );
209 $self->_set_file_handle( $fh );
210 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
211 ###InternalReporTCSV name_space => $self->get_all_space( '_open_file' ),
212 ###InternalReporTCSV message =>[ 'Read file handle built: ' . -s $fh ], } );
213
214 # Collect the header if available
215 if( -s $self->_get_file_handle ){
216 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
217 ###InternalReporTCSV name_space => $self->get_all_space( '_open_file' ),
218 ###InternalReporTCSV message =>[ "The file appears to have pre-existing content (headers)" ], } );
219 my $header_ref;
220 @$header_ref = $self->_set_parsing_header( $fh );
221 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
222 ###InternalReporTCSV name_space => $self->get_all_space( '_open_file' ),
223 ###InternalReporTCSV message =>[ "File headers are: " . join( '~|~', @$header_ref ) ], } );
224 $self->_set_file_headers( $header_ref );
225 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
226 ###InternalReporTCSV name_space => $self->get_all_space( '_open_file' ),
227 ###InternalReporTCSV message =>[ "File headers set" ], } );
228 }else{
229 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
230 ###InternalReporTCSV name_space => $self->get_all_space( '_open_file' ),
231 ###InternalReporTCSV message =>[ "The file is zero size" ], } );
232 }
233
234 # Get to the end for add_line (in case you weren't there before)
235 seek( $self->_get_file_handle, 0, SEEK_END) or confess "Can't seek (end) on $file: $!";
236
237 return 1;
238 }
239  
240 around '_set_file_headers' => sub{
241 my( $_set_file_headers, $self, $header_ref ) = @_;
242 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
243 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
244 ###InternalReporTCSV message =>[ 'Attempting to set the file headers to:', $header_ref ], } );
245 if( $self->should_reconcile_headers ){
246 my( $one_extra, $two_extra ) = $self->_test_headers( $header_ref, $self->get_headers );
247 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
248 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
249 ###InternalReporTCSV message =>[ 'Returned from the header test:', $one_extra, $two_extra ], } );
250 $self->set_reconcile_headers( 0 );
251 if( $two_extra ){
252 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
253 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
254 ###InternalReporTCSV message =>[ 'There are more expected headers than were found in the file:', $two_extra ], } );
255 push @$header_ref, @$two_extra;
256 $self->_add_headers_to_file( $header_ref );
257 }
258 if( $one_extra ){
259 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
260 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
261 ###InternalReporTCSV message =>[ 'There are more file headers than expected headers:', $one_extra ], } );
262 $self->set_headers( $header_ref );
263 }
264 $self->set_reconcile_headers( 1 );
265 }
266 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
267 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
268 ###InternalReporTCSV message =>[ "Setting file headers to: ", $header_ref ], } );
269 $self->$_set_file_headers( $header_ref );
270 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
271 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
272 ###InternalReporTCSV message =>[ 'Final file headers:', $self->_get_file_headers ], } );
273 };
274  
275 around 'set_headers' => sub{
276 my( $set_headers_method, $self, $header_ref ) = @_;
277 $self->_clear_header_lookup;
278 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
279 ###InternalReporTCSV name_space => $self->get_all_space( 'set_headers' ),
280 ###InternalReporTCSV message =>[ 'Received a request to set headers to:', $header_ref ], } );
281 $header_ref = $self->_scrub_header_array( $header_ref );
282 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
283 ###InternalReporTCSV name_space => $self->get_all_space( 'set_headers' ),
284 ###InternalReporTCSV message =>[ 'Attempting to set the requested headers with:', $header_ref ], } );
285 $self->_set_expected_header_lookup( $header_ref );
286 my( $one_extra, $two_extra, $translation );
287 if( $self->should_reconcile_headers ){
288 my $file_headers = $self->_get_file_headers;
289 ( $one_extra, $two_extra, $translation ) = $self->_test_headers( $file_headers, $header_ref, );
290 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
291 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
292 ###InternalReporTCSV message =>[ 'Returned from the header test:', $one_extra, $two_extra, $translation ], } );
293 $self->set_reconcile_headers( 0 );
294 if( $two_extra ){
295 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
296 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
297 ###InternalReporTCSV message =>[ 'There are more expected headers than were found in the file:', $two_extra ], } );
298 my $new_ref;
299 push @$new_ref, @$file_headers if $file_headers;
300 push @$new_ref, @$two_extra;
301 $self->_add_headers_to_file( $new_ref );
302 $header_ref = $new_ref;
303 }
304 $self->set_reconcile_headers( 1 );
305 }
306 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
307 ###InternalReporTCSV name_space => $self->get_all_space( '_set_file_headers' ),
308 ###InternalReporTCSV message =>[ "Setting requested headers to: ", $header_ref ], } );
309 $self->$set_headers_method( $header_ref );
310 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
311 ###InternalReporTCSV name_space => $self->get_all_space( 'set_headers' ),
312 ###InternalReporTCSV message =>[ 'Final requested headers resolved to:', $header_ref,
313 ###InternalReporTCSV '...with passing-to translation resolved as:', $translation ], } );
314 return $translation;
315 };
316  
317 sub _add_headers_to_file{
318  
319 my ( $self, $new_ref ) = @_;
320 #~ my $new_line = join( $self->_separator_char, @$new_ref ) . "\n";
321 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
322 ###InternalReporTCSV name_space => $self->get_all_space( '_add_headers_to_file' ),
323 ###InternalReporTCSV message =>[ "Arrived at _add_headers_to_file for:", $new_ref, ], } );
324
325 # Make a temp file to create new data
326 my $temp_dir = File::Temp->newdir( CLEANUP => 1 );
327 my $fh = File::Temp->new( UNLINK => 0, DIR => $temp_dir );
328 my $temp_parser = Text::CSV_XS->new({ binary => 1, sep_char => $self->_separator_char, eol => $\, auto_diag => 1 });#
329 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
330 ###InternalReporTCSV name_space => $self->get_all_space( '_add_headers_to_file' ),
331 ###InternalReporTCSV message =>[ "Tempfile open: " . $fh->filename, ], } );
332
333 # Add the new header
334 $temp_parser->say( $fh, $new_ref );
335 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
336 ###InternalReporTCSV name_space => $self->get_all_space( '_add_headers_to_file' ),
337 ###InternalReporTCSV message =>[ "Added headers to the tempfile: ", $new_ref, ], } );
338
339 # Write the rest of the lines (except the old header)
340 my $original_fh = $self->_get_file_handle;
341 $self->_clear_file_handle;
342 my $first_line = 1;
343 seek( $original_fh, 0, 0 );
344 while (my $row = $self->_read_next_line($original_fh)) {
345 if( $first_line ){
346 $first_line = 0;
347 next;
348 }
349 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
350 ###InternalReporTCSV name_space => $self->get_all_space( '_add_headers_to_file' ),
351 ###InternalReporTCSV message =>[ "Printing line to tempfile:", $row], } );
352 $temp_parser->say( $fh, $row );
353 }
354
355 # Close the original file
356 flock( $original_fh, LOCK_UN );
357 close( $original_fh ) or confess "Couldn't close file: $!";
358 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
359 ###InternalReporTCSV name_space => $self->get_all_space( '_add_headers_to_file' ),
360 ###InternalReporTCSV message =>[ "Closed the original file handle" ], } );
361
362 # Close the new tempfile
363 flock( $fh, LOCK_UN );
364 close( $fh );
365 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
366 ###InternalReporTCSV name_space => $self->get_all_space( '_add_headers_to_file' ),
367 ###InternalReporTCSV message =>[ "Closed the new temp file" ], } );
368
369 # Replace the original file with the tempfile
370 copy( $fh->filename, $self->get_file_name ) or confess "Couldn't copy file: $!";
371 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
372 ###InternalReporTCSV name_space => $self->get_all_space( '_add_headers_to_file' ),
373 ###InternalReporTCSV message =>[ "Original file replaced: " . $self->get_file_name,
374 ###InternalReporTCSV '..with file: ' . $fh->filename ], } );
375 $fh = undef;
376
377 # Re-run the file to get the headers registered with Text::CSV_XS;
378 $self->_open_file( $self->get_file_name );
379 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
380 ###InternalReporTCSV name_space => $self->get_all_space( '_add_headers_to_file' ),
381 ###InternalReporTCSV message =>[ "Updated file re-test complete" ], } );
382
383 return 1;
384 }
385  
386 sub _test_headers{
387  
388 my ( $self, $header_ref_1, $header_ref_2 ) = @_;
389 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
390 ###InternalReporTCSV name_space => $self->get_all_space( '_test_headers' ),
391 ###InternalReporTCSV message =>[ "Arrived at test headers with:", $header_ref_1, $header_ref_2 ], } );
392 my( $one_extra, $two_extra, $translation );
393 if( !$header_ref_2 ){
394 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
395 ###InternalReporTCSV name_space => $self->get_all_space( '_test_headers' ),
396 ###InternalReporTCSV message =>[ "No second header list passed for testing" ], } );
397 $one_extra = $header_ref_1;
398 }else{
399 my $x = 0;
400 for my $second_header ( @$header_ref_2 ){
401 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
402 ###InternalReporTCSV name_space => $self->get_all_space( '_test_headers' ),
403 ###InternalReporTCSV message =>[ "Testing second header: $second_header" ], } );
404 my $y = 0;
405 my $found_match = 0;
406 NEWHEADERTEST: for my $first_header ( @$header_ref_1 ){
407 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
408 ###InternalReporTCSV name_space => $self->get_all_space( '_test_headers' ),
409 ###InternalReporTCSV message =>[ "Testing first header -$first_header- for a match" ], } );
410 if( $second_header eq $first_header ){
411 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
412 ###InternalReporTCSV name_space => $self->get_all_space( '_test_headers' ),
413 ###InternalReporTCSV message =>[ "Second header list -$second_header- at position: $x",
414 ###InternalReporTCSV "matches first header list header -$first_header- at position: $y" ], } );
415 $translation->{$x} = $y;
416 $found_match = 1;
417 last NEWHEADERTEST;
418 }
419 $y++;
420 }
421 push @$two_extra, $second_header if !$found_match;
422 $x++;
423 }
424 for my $pos ( 0 .. $#$header_ref_1 ){
425 if( !exists $translation->{$pos} ){
426 push @$one_extra, $header_ref_1->[$pos];
427 }
428 }
429 my $next_pos = $#$header_ref_1 + 1;
430 for my $pos ( 0 .. $#$header_ref_2 ){
431 if( !exists $translation->{$pos} ){
432 $translation->{$pos} = $next_pos++;
433 }
434 }
435 }
436 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
437 ###InternalReporTCSV name_space => $self->get_all_space( '_test_headers' ),
438 ###InternalReporTCSV message =>[ "Finished with header list 1 extra:", $one_extra,
439 ###InternalReporTCSV "...and header list 2 extra:", $two_extra,
440 ###InternalReporTCSV "...and translation ref:", $translation ], } );
441 return( $one_extra, $two_extra, $translation );
442 }
443  
444 sub _build_message_from_arrayref{
445 my( $self, $array_ref )= @_;
446 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
447 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_arrayref' ),
448 ###InternalReporTCSV message =>[ 'Testing the message from an array ref: ' . ($self->should_test_first_row//0), $array_ref ], } );
449 my @expected_headers = $self->has_headers ? @{$self->get_headers} : ();
450 if( $self->should_test_first_row ){
451 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
452 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_arrayref' ),
453 ###InternalReporTCSV message =>[ 'First row - testing if the list matches the header count' ], } );
454
455 if( $#$array_ref != $#expected_headers ){
456 if( scalar( @expected_headers ) == 0 ){
457 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
458 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_arrayref' ),
459 ###InternalReporTCSV message =>[ 'Adding dummy file headers' ], } );
460 my $dummy_headers;
461 map{ $dummy_headers->[$_] = "header_" . $_ } ( 0 .. $#$array_ref );
462 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
463 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_arrayref' ),
464 ###InternalReporTCSV message =>[ 'New dummy headers:', $dummy_headers ], } );
465 cluck "Setting dummy headers ( " . join( ', ', @$dummy_headers ) . " )" if !$ENV{hide_warn};
466 $self->set_reconcile_headers( 1 );
467 $self->set_headers( $dummy_headers );
468 }else{
469 cluck "The first added row has -" . scalar( @$array_ref ) .
470 "- items - but the report expects -" .
471 scalar( @expected_headers ) . "- items" if !$ENV{hide_warn};
472 }
473 }
474 $self->_test_first_row ( 0 );
475 }
476 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
477 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_arrayref' ),
478 ###InternalReporTCSV message =>[ 'Returning message ref:', $array_ref ], } );
479 return $array_ref;
480 }
481  
482 sub _build_message_from_hashref{
483 my( $self, $hash_ref )= @_;
484 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
485 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_hashref' ),
486 ###InternalReporTCSV message =>[ 'Building the array ref from the hash ref: ' . ($self->should_test_first_row//0), $hash_ref ], } );
487
488 # Scrub the hash
489 my( $better_hash, @missing_list );
490 for my $key ( keys %$hash_ref ){
491 my $fixed_key = $self->_scrub_header_string( $key );
492 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
493 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_hashref' ),
494 ###InternalReporTCSV message =>[ "Managing key -$fixed_key- for key: $key" ], } );
495 push @missing_list, $fixed_key if $self->should_test_first_row and !$self->_has_header_named( $fixed_key );
496 $better_hash->{$fixed_key} = $hash_ref->{$key};
497 }
498 $self->_test_first_row( 0 );
499 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
500 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_hashref' ),
501 ###InternalReporTCSV message =>[ "Updated hash message:", $better_hash,
502 ###InternalReporTCSV "...with missing list:", @missing_list ], } );
503
504 # Handle first row errors
505 if( @missing_list ){
506 my @expected_headers = $self->has_headers ? @{$self->get_headers} : ();
507 push @expected_headers, @missing_list;
508 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 3,
509 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_hashref' ),
510 ###InternalReporTCSV message =>[ "Updating the expected headers with new data", [@expected_headers] ], } );
511 cluck "Adding headers from the first hashref ( " . join( ', ', @missing_list ) . " )" if !$ENV{hide_warn};
512 $self->set_reconcile_headers( 1 );
513 $self->set_headers( [@expected_headers] );
514 }
515
516 # Build the array_ref
517 my $array_ref = [];
518 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
519 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_hashref' ),
520 ###InternalReporTCSV message =>[ 'Building an array ref with loookup:', $self->_get_header_lookup ], } );
521 for my $header ( keys %$better_hash ){
522 if( $self->_has_header_named( $header ) ){
523 $array_ref->[$self->_get_header_position( $header )] = $better_hash->{$header};
524 }else{
525 cluck "found a hash key in the message that doesn't match the expected header ( $header )" if !$ENV{hide_warn};
526 }
527 }
528
529 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
530 ###InternalReporTCSV name_space => $self->get_all_space( 'add_line::_build_message_from_hashref' ),
531 ###InternalReporTCSV message =>[ 'Returning message array ref:', $array_ref ], } );
532 return $array_ref;
533 }
534  
535 sub _set_expected_header_lookup{
536 my ( $self, $hash_ref ) = @_;
537 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
538 ###InternalReporTCSV name_space => $self->get_all_space( '_set_expected_header_lookup' ),
539 ###InternalReporTCSV message =>[ "Arrived at _set_expected_header_lookup with:", $hash_ref ], } );
540 my( $i, $positions, ) = ( 0, {} );
541 map{ $positions->{$_} = $i++ } @$hash_ref;
542 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
543 ###InternalReporTCSV name_space => $self->get_all_space( '_set_expected_header_lookup' ),
544 ###InternalReporTCSV message =>[ "Header lookup hash is:", $positions ], } );
545 $self->_set_header_lookup( $positions );
546 }
547  
548 sub _scrub_header_array{
549 my ( $self, @args ) = @_;
550 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
551 ###InternalReporTCSV name_space => $self->get_all_space( '_scrub_header_array' ),
552 ###InternalReporTCSV message =>[ "Arrived at _scrub_header_array:", @args ], } );
553 my $new_ref = [];
554 for my $header ( @{$args[0]} ){
555 push @$new_ref, $self->_scrub_header_string( $header );
556 }
557 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
558 ###InternalReporTCSV name_space => $self->get_all_space( '_scrub_header_array' ),
559 ###InternalReporTCSV message =>[ "Updated header is:", $new_ref ], } );
560 return $new_ref;
561 }
562  
563 sub _scrub_header_string{
564 my ( $self, $string ) = @_;
565 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
566 ###InternalReporTCSV name_space => $self->get_all_space( '_scrub_header_string' ),
567 ###InternalReporTCSV message =>[ "Arrived at _scrub_header_string with: $string" ], } );
568 $string = lc( $string );
569 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
570 ###InternalReporTCSV name_space => $self->get_all_space( '_scrub_header_string' ),
571 ###InternalReporTCSV message =>[ "The updated string is: $string" ], } );
572 $string =~ s/\n/ /gsxm;
573 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
574 ###InternalReporTCSV name_space => $self->get_all_space( '_scrub_header_string' ),
575 ###InternalReporTCSV message =>[ "The updated string is: $string" ], } );
576 $string =~ s/\r/ /gsxm;
577 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
578 ###InternalReporTCSV name_space => $self->get_all_space( '_scrub_header_string' ),
579 ###InternalReporTCSV message =>[ "The updated string is: $string" ], } );
580 $string =~ s/\s/_/gsxm;
581 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 0,
582 ###InternalReporTCSV name_space => $self->get_all_space( '_scrub_header_string' ),
583 ###InternalReporTCSV message =>[ "The updated string is: $string" ], } );
584 chomp $string;
585 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
586 ###InternalReporTCSV name_space => $self->get_all_space( '_scrub_header_string' ),
587 ###InternalReporTCSV message =>[ "The final string is: $string" ], } );
588 return $string;
589 }
590  
591 sub DEMOLISH{
592 my ( $self ) = @_;
593 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 2,
594 ###InternalReporTCSV name_space => $self->get_all_space( 'DEMOLISH' ),
595 ###InternalReporTCSV message =>[ "Arrived at DEMOLISH" ], } ) if $switchboard;
596 if( $self->_has_file_handle ){
597 flock( $self->_get_file_handle, LOCK_UN );
598 close( $self->_get_file_handle ) or confess "Couldn't close the file handle";
599 $self->_clear_file_handle;
600 ###InternalReporTCSV $switchboard->master_talk( { report => 'log_file', level => 1,
601 ###InternalReporTCSV name_space => $self->get_all_space( 'DEMOLISH' ),
602 ###InternalReporTCSV message =>[ "Arrived at DEMOLISH" ], } ) if $switchboard;
603 }
604 }
605  
606 #########1 Phinish 3#########4#########5#########6#########7#########8#########9
607  
608 __PACKAGE__->meta->make_immutable;
609  
610 1;
611 # The preceding line will help the module return a true value
612  
613 #########1 main pod docs 3#########4#########5#########6#########7#########8#########9
614  
615 __END__