Pipeline/Output/Storage/Hash.pm untested

filter: Statementstmt 0.0% 0 / 27 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 40 SCAR 24.8
CC
3
Cov
0%
set
12.0
new_record
2.0
BEGIN
2.0
CRAP 12.0
1 =pod
2  
3 =head1 NAME
4  
5 ETL::Pipeline::Output::Storage::Hash - Holds the current record in a hash
6  
7 =head1 SYNOPSIS
8  
9 with 'ETL::Pipeline::Output::Storage::Hash';
10  
11 =head1 DESCRIPTION
12  
13 This role stores the current record in a Perl hash.
14 L<ETL::Pipeline::Output/write_record> copies the information out of the hash
15 and into permanent storage.
16  
17 L<ETL::Pipeline/process> wipes out the hash at the start of every input record.
18 B<ETL::Pipeline::Output::Storage::Hash> creates an entirely new copy. That way
19 L<ETL::Pipeline::Output/write_record> can safely store the hash reference.
20  
21 =cut
22  
23 package ETL::Pipeline::Output::Storage::Hash;
24 use Moose::Role;
25  
26 use 5.14.0;
27 use warnings;
28  
29 use Carp;
30 use String::Util qw/hascontent/;
31  
32  
33 our $VERSION = '1.00';
34  
35  
36 =head1 METHODS & ATTRIBUTES
37  
38 =head3 default_fields
39  
40 This subroutine returns a hash of default fields. L</new_record> initializes
41 every new record with these values. The consuming class must define the
42 B<default_fields> method.
43  
44 B<NOTE:> Do not return a hash reference. Return the hash. B<default_fields> is
45 called in list context.
46  
47 =cut
48  
49 requires 'default_fields';
50  
51  
52 =head3 current
53  
54 B<current> is a hash reference of the current record. Fields are added using
55 the L</set> method.
56  
57 =head3 field_names
58  
59 B<field_names> returns a list of the current field names. It can be used for
60 traversing the hash.
61  
62 =head3 get_value
63  
64 B<get_value> returns the value of a single field. Pass the field name (a.k.a.
65 the hash key) as the only parameter.
66  
67 =head3 this_record
68  
69 B<this_record> returns the complete record as a hash instead of a hash
70 reference.
71  
72 =cut
73  
74 has 'current' => (
75 handles => {
76 field_names => 'keys',
77 get_value => 'get',
78 _set_value => 'set',
79 this_record => 'elements',
80 },
81 is => 'rw',
82 isa => 'HashRef[Any]',
83 traits => [qw/Hash/],
84 );
85  
86  
87 =head2 Called from L<ETL::Pipeline/process>
88  
89 =head3 new_record
90  
91 B<new_record> creates a new hash reference for L</current>. Every record begins
92 empty. Fields are created through L</set>.
93  
94 =cut
95  
96 sub new_record {
97 my $self = shift;
98  
99 my %copy = $self->default_fields;
100 $self->current( \%copy );
101 }
102  
103  
104 =head3 set
105  
106 B<set> adds a single field to the record. The parameters are a field name
107 followed by one or more values. Multiple values are stored as a list reference.
108  
109 =cut
110  
111 sub set {
112 my ($self, $field, @values) = @_;
113  
114 if (hascontent( $field )) {
115 $self->_set_value( $field, scalar( @values ) > 1 ? [@values] : $values[0] );
116 } else {
117 croak 'No field name for "set"';
118 }
119 }
120  
121  
122 =head1 SEE ALSO
123  
124 L<ETL::Pipeline>, L<ETL::Pipeline::Output>, L<ETL::Pipeline::Output::UnitTest>
125  
126 =head1 AUTHOR
127  
128 Robert Wohlfarth <robert.j.wohlfarth@vanderbilt.edu>
129  
130 =head1 LICENSE
131  
132 Copyright 2016 (c) Vanderbilt University
133  
134 This program is free software; you can redistribute it and/or modify it under
135 the same terms as Perl itself.
136  
137 =cut
138  
139 # Required by Perl to load the module.
140 1;