File Coverage

blib/lib/CBI/Wrapper/Field.pm
Criterion Covered Total %
statement 6 49 12.2
branch 0 16 0.0
condition 0 3 0.0
subroutine 2 16 12.5
pod 0 14 0.0
total 8 98 8.1


line stmt bran cond sub pod time code
1             ##############################################################################
2             #
3             # Copyright (C) 2012 Agile Business Group sagl ()
4             # Copyright (C) 2012 Domsense srl ()
5             # Copyright (C) 2012 Associazione OpenERP Italia
6             # ().
7             # Copyright (C) 2022 Res Binaria Di Paolo Capaldo ()
8             # All Rights Reserved
9             #
10             # This program is free software: you can redistribute it and/or modify
11             # it under the terms of the GNU Affero General Public License as published
12             # by the Free Software Foundation, either version 3 of the License, or
13             # (at your option) any later version.
14             #
15             # This program is distributed in the hope that it will be useful,
16             # but WITHOUT ANY WARRANTY; without even the implied warranty of
17             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18             # GNU Affero General Public License for more details.
19             #
20             # You should have received a copy of the GNU Affero General Public License
21             # along with this program. If not, see .
22             #
23             ##############################################################################
24              
25             package CBI::Wrapper::Field;
26              
27 1     1   6 use warnings;
  1         1  
  1         28  
28 1     1   4 use strict;
  1         1  
  1         860  
29              
30             #ABSTRACT: Field of a Record.
31             # There are 120 chars in every Record;
32             # a Fields is a substring in the Record.
33             # Es. Testata IB
34             # IBT123405428040122DEMORIBA 1$05428 E
35             # ' ' -> primo campo
36             # 'IB' -> secondo campo
37             # 'T1234' -> terzo campo
38             # ecc..
39             # Each field has his own length (see RecordMapping.pm)
40              
41             sub new {
42 0     0 0   my $class = shift;
43 0           my $args = shift;
44             my $self = {
45             _from_position => $args->{from}, # Start column.
46             _to_position => $args->{to}, # End column.
47             _name => $args->{name}, # Field name.
48             _type => defined $args->{type} ? $args->{type} : 'an', # Data type ('n' numeric, 'an' text (default))
49 0 0         _truncate => defined $args->{truncate} ? $args->{truncate} : 'no', # 'yes' truncable, 'no' otherwise (default).
    0          
50             _content => shift, # Field content.
51             };
52              
53 0           bless $self, $class;
54              
55             # Content default value if not defined.
56 0 0         $self->set_content(' ' x $self->get_length()) unless defined $self->get_content();
57              
58 0           return $self;
59             }
60              
61             # Getters and Setters.
62             sub get_length {
63 0     0 0   my ($self) = @_;
64 0           return ($self->get_to_position() - $self->get_from_position()) + 1;
65             }
66              
67              
68             sub set_content {
69 0     0 0   my ($self, $content) = @_;
70              
71 0 0         if (length($content) > $self->get_length()) {
72 0 0         if ($self->get_truncate() eq 'yes') {
73 0           $content = substr($content, 0, $self->get_length());
74             } else {
75 0           croak('[BufferError] Specified field value ' . $self->get_name() . ' = ' . $content . ' passes field capacity of ' . $self->get_length());
76             }
77             }
78              
79 0 0         if ($self->get_type() eq 'n') {
80 0           my $no_spaces = $content;
81              
82             # Trim content.
83 0           $no_spaces =~ s/\ //g;
84              
85 0 0         if ($no_spaces eq '') {
86              
87             # Pad with ''.
88 0           $content = sprintf('%-' . $self->get_length() . 's', $no_spaces);
89             } else {
90              
91             # Pad with zeros.
92 0 0 0       $content = sprintf('%0' . $self->get_length() . 'd', (defined $content and $content ne '') ? $content : 0);
93             }
94             } else {
95              
96             # Pad with ''.
97 0           $content = sprintf('%-' . $self->get_length() . 's', $content);
98             }
99              
100 0           $self->{_content} = $content;
101             }
102              
103             sub get_content {
104 0     0 0   my ($self) = @_;
105 0           return $self->{_content};
106             }
107              
108             sub set_name {
109 0     0 0   my ($self, $name) = @_;
110              
111 0           $self->{_name} = $name;
112             }
113              
114             sub get_name {
115 0     0 0   my ($self) = @_;
116 0           return $self->{_name};
117             }
118              
119             sub set_type {
120 0     0 0   my ($self, $type) = @_;
121 0           $self->{_type} = $type;
122             }
123              
124             sub get_type {
125 0     0 0   my ($self) = @_;
126 0           return $self->{_type};
127             }
128              
129             sub set_truncate {
130 0     0 0   my ($self, $truncate) = @_;
131 0           $self->{_truncate} = $truncate;
132             }
133              
134             sub get_truncate {
135 0     0 0   my ($self) = @_;
136 0           return $self->{_truncate};
137             }
138              
139             sub set_from_position {
140 0     0 0   my ($self, $from_position) = @_;
141              
142 0           $self->{_from_position} = $from_position;
143             }
144              
145             sub get_from_position {
146 0     0 0   my ($self) = @_;
147 0           return $self->{_from_position};
148             }
149              
150             sub set_to_position {
151 0     0 0   my ($self, $to_position) = @_;
152              
153 0           $self->{_to_position} = $to_position;
154             }
155              
156             sub get_to_position {
157 0     0 0   my ($self) = @_;
158 0           return $self->{_to_position};
159             }
160              
161             1;
162