| 1 | ############################################################################## | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (C) 2012 Agile Business Group sagl (<http://www.agilebg.com>) | ||||||||||||||||||||||||||||
| 4 | # Copyright (C) 2012 Domsense srl (<http://www.domsense.com>) | ||||||||||||||||||||||||||||
| 5 | # Copyright (C) 2012 Associazione OpenERP Italia | ||||||||||||||||||||||||||||
| 6 | # (<http://www.openerp-italia.org>). | ||||||||||||||||||||||||||||
| 7 | # Copyright (C) 2022 Res Binaria Di Paolo Capaldo (<https://www.resbinaria.com/>) | ||||||||||||||||||||||||||||
| 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 <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||
| 23 | ############################################################################## | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | package CBI::Wrapper::Field; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 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 | ▶ | _truncate => defined $args->{truncate} ? $args->{truncate} : 'no', # 'yes' truncable, 'no' otherwise (default). | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 50 | _content => shift, # Field content. | ||||||||||||||||||||||||||||
| 51 | }; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Content default value if not defined. | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $self->set_content(' ' x $self->get_length()) unless defined $self->get_content(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | return $self; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Getters and Setters. | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub get_length { | ||||||||||||||||||||||||||
|
Subroutine
get_length: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 64 | 0 | return ($self->get_to_position() - $self->get_from_position()) + 1; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub set_content { | ||||||||||||||||||||||||||
|
Subroutine
set_content: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 0 | my ($self, $content) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (length($content) > $self->get_length()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($self->get_truncate() eq 'yes') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ▶ | if ($self->get_type() eq 'n') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | my $no_spaces = $content; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Trim content. | ||||||||||||||||||||||||||||
| 83 | 0 | $no_spaces =~ s/\ //g; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ($no_spaces eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # Pad with ''. | ||||||||||||||||||||||||||||
| 88 | 0 | $content = sprintf('%-' . $self->get_length() . 's', $no_spaces); | |||||||||||||||||||||||||||
| 89 | } else { | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Pad with zeros. | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $content = sprintf('%0' . $self->get_length() . 'd', (defined $content and $content ne '') ? $content : 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $content and $content ne ""
MC/DC0%0/2
defined $content and $content ne ""
defined $content
$content ne ""
Truth table0%0/3
A and B
A= defined $contentB= $content ne ""
| |||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub get_content { | ||||||||||||||||||||||||||
|
Subroutine
get_content: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 104 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 105 | 0 | return $self->{_content}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub set_name { | ||||||||||||||||||||||||||
|
Subroutine
set_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | $self->{_name} = $name; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | sub get_name { | ||||||||||||||||||||||||||
|
Subroutine
get_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 115 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 116 | 0 | return $self->{_name}; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub set_type { | ||||||||||||||||||||||||||
|
Subroutine
set_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 0 | my ($self, $type) = @_; | |||||||||||||||||||||||||||
| 121 | 0 | $self->{_type} = $type; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub get_type { | ||||||||||||||||||||||||||
|
Subroutine
get_type: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 126 | 0 | return $self->{_type}; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub set_truncate { | ||||||||||||||||||||||||||
|
Subroutine
set_truncate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($self, $truncate) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | $self->{_truncate} = $truncate; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub get_truncate { | ||||||||||||||||||||||||||
|
Subroutine
get_truncate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 136 | 0 | return $self->{_truncate}; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub set_from_position { | ||||||||||||||||||||||||||
|
Subroutine
set_from_position: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 0 | my ($self, $from_position) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | $self->{_from_position} = $from_position; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub get_from_position { | ||||||||||||||||||||||||||
|
Subroutine
get_from_position: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 146 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 147 | 0 | return $self->{_from_position}; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub set_to_position { | ||||||||||||||||||||||||||
|
Subroutine
set_to_position: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 151 | 0 | my ($self, $to_position) = @_; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | $self->{_to_position} = $to_position; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub get_to_position { | ||||||||||||||||||||||||||
|
Subroutine
get_to_position: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 158 | 0 | return $self->{_to_position}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||