File Coverage

blib/lib/Business/NAB/BPAY/Payments/TrailerRecord.pm
Criterion Covered Total %
statement 35 35 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 49 49 100.0


line stmt bran cond sub pod time code
1             package Business::NAB::BPAY::Payments::TrailerRecord;
2             $Business::NAB::BPAY::Payments::TrailerRecord::VERSION = '0.03';
3             =head1 NAME
4              
5             Business::NAB::BPAY::Payments::TrailerRecord
6              
7             =head1 SYNOPSIS
8              
9             use Business::NAB::BPAY::Payments::TrailerRecord;
10              
11             # parse
12             my $Trailer = Business::NAB::BPAY::Payments::TrailerRecord
13             ->new_from_record( $line );
14              
15             # create
16             my $Trailer = Business::NAB::BPAY::Payments::TrailerRecord->new(
17             total_number_of_payments => 10,
18             total_value_of_payments => 11111,
19             );
20              
21             my $line = $Trailer->to_record;
22              
23             =head1 DESCRIPTION
24              
25             Class for trailer record in the "BPAY Batch User Guide"
26              
27             =cut;
28              
29 5     5   1283255 use strict;
  5         21  
  5         288  
30 5     5   36 use warnings;
  5         48  
  5         387  
31 5     5   58 use feature qw/ signatures /;
  5         12  
  5         771  
32              
33 5     5   37 use Carp qw/ croak /;
  5         12  
  5         348  
34 5     5   712 use Moose;
  5         632067  
  5         72  
35 5         589 use Business::NAB::Types qw/
36             add_max_string_attribute
37 5     5   50789 /;
  5         28  
38              
39 5     5   85 no warnings qw/ experimental::signatures /;
  5         15  
  5         2642  
40              
41             =head1 ATTRIBUTES
42              
43             =over
44              
45             =item total_number_of_payments (NAB::Type::PositiveIntOrZero)
46              
47             =item total_value_of_payments (NAB::Type::PositiveIntOrZero)
48              
49             =back
50              
51             =cut
52              
53             foreach my $attr (
54             qw/
55             total_number_of_payments
56             total_value_of_payments
57             /
58             ) {
59             has $attr => (
60             is => 'ro',
61             isa => 'NAB::Type::PositiveIntOrZero',
62             required => 1,
63             trigger => sub {
64             my ( $self, $value, $old_value ) = @_;
65             $self->{ $attr } = int( $value );
66             },
67             );
68             }
69              
70             sub _pack_template {
71 7     7   286 return "A1 A10 A13 A120";
72             }
73              
74             =head1 METHODS
75              
76             =head2 new_from_record
77              
78             Returns a new instance of the class with attributes populated from
79             the result of parsing the passed line:
80              
81             my $Trailer = Business::NAB::BPAY::Payments::TrailerRecord
82             ->new_from_record( $line );
83              
84             =cut
85              
86 3     3 1 24965 sub new_from_record ( $class, $line ) {
  3         9  
  3         8  
  3         6  
87              
88             # undef being "this space intentionally left blank"
89             my (
90 3         16 $record_type,
91             $total_number,
92             $total_value,
93             undef,
94             ) = unpack( $class->_pack_template(), $line );
95              
96 3 100       18 if ( $record_type ne '9' ) {
97 1         20 croak( "unsupported record type ($record_type)" );
98             }
99              
100 2         147 return $class->new(
101             total_value_of_payments => $total_value,
102             total_number_of_payments => $total_number,
103             );
104             }
105              
106             =head2 to_record
107              
108             Returns a string constructed from the object's attributes, representing
109             the record for use in a batch file:
110              
111             my $line = $Trailer->to_record;
112              
113             =cut
114              
115 4     4 1 13 sub to_record ( $self ) {
  4         9  
  4         9  
116              
117 4         18 my $record = pack(
118             $self->_pack_template(),
119             "9",
120             sprintf( "%010s", $self->total_number_of_payments ),
121             sprintf( "%013s", $self->total_value_of_payments ),
122             "",
123             );
124              
125 4         133 return $record;
126             }
127              
128             =head1 SEE ALSO
129              
130             L<Business::NAB::Types>
131              
132             =cut
133              
134             __PACKAGE__->meta->make_immutable;