File Coverage

blib/lib/Business/Westpac/Role/CSV.pm
Criterion Covered Total %
statement 31 31 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 41 41 100.0


line stmt bran cond sub pod time code
1             package Business::Westpac::Role::CSV;
2              
3             =head1 NAME
4              
5             Business::Westpac::Role::CSV
6              
7             =head1 SYNOPSIS
8              
9             use Moose;
10             with 'Business::Westpac::Role::CSV';
11              
12             =head1 DESCRIPTION
13              
14             Role for CSV related attributes and functions used in the
15             Business::Westpac namespace
16              
17             =cut
18              
19 12     12   10470 use strict;
  12         31  
  12         565  
20 12     12   73 use warnings;
  12         22  
  12         1042  
21 12     12   97 use feature qw/ signatures /;
  12         28  
  12         1631  
22              
23 12     12   6831 use Moose::Role;
  12         75230  
  12         56  
24 12     12   86489 no warnings qw/ experimental::signatures /;
  12         53  
  12         762  
25 12     12   11552 use Text::CSV;
  12         259413  
  12         3623  
26              
27             =head1 ATTRIBUTES
28              
29             None yet
30              
31             =cut
32              
33             has _csv_encoder => (
34             is => 'ro',
35             isa => 'Text::CSV',
36             lazy => 1,
37             default => sub {
38             Text::CSV->new({
39             binary => 1,
40             quote => '"',
41             always_quote => 1,
42             });
43             }
44             );
45              
46             =head1 METHODS
47              
48             =head2 attributes_to_csv
49              
50             Convert the list of attributes to a CSV line:
51              
52             my @csv = $self->attributes_to_csv( qw/
53             record_type
54             payment_amount
55             ...
56             / );
57              
58             =cut
59              
60             sub attributes_to_csv (
61 28         56 $self,
62 28         171 @attributes,
63 28     28 1 100 ) {
  28         112  
64             return $self->values_to_csv(
65 28         98 map { $self->$_ } @attributes
  313         8016  
66             );
67             }
68              
69             =head1 METHODS
70              
71             =head2 values_to_csv
72              
73             Convert the list of values to a CSV line:
74              
75             my @csv = $self->values_to_csv(
76             $self->record_type,
77             $self->payment_amount,
78             ...
79             );
80              
81             =cut
82              
83             sub values_to_csv (
84 31         71 $self,
85 31         132 @values,
86 31     31 1 322 ) {
  31         57  
87 31         1935 my $csv = $self->_csv_encoder;
88 31         244 $csv->combine( @values );
89 31         1426 return $csv->string;
90             }
91              
92             1;