File Coverage

blib/lib/ETL/Pipeline/Output/Storage/Hash.pm
Criterion Covered Total %
statement 20 21 95.2
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 32 34 94.1


line stmt bran cond sub pod time code
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 9     9   5107 use Moose::Role;
  9         14  
  9         69  
25              
26 9     9   30836 use 5.14.0;
  9         26  
27 9     9   32 use warnings;
  9         11  
  9         222  
28              
29 9     9   31 use Carp;
  9         13  
  9         616  
30 9     9   34 use String::Util qw/hascontent/;
  9         16  
  9         1577  
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 26     26 1 26 my $self = shift;
98              
99 26         57 my %copy = $self->default_fields;
100 26         654 $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 45     45 1 299 my ($self, $field, @values) = @_;
113              
114 45 50       83 if (hascontent( $field )) {
115 45 100       1628 $self->_set_value( $field, scalar( @values ) > 1 ? [@values] : $values[0] );
116             } else {
117 0           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;