File Coverage

blib/lib/CEFACT/Unit.pm
Criterion Covered Total %
statement 61 61 100.0
branch 7 8 87.5
condition n/a
subroutine 13 13 100.0
pod 2 2 100.0
total 83 84 98.8


line stmt bran cond sub pod time code
1             package CEFACT::Unit;
2              
3 4     4   123356 use strict;
  4         8  
  4         148  
4 4     4   38 use warnings;
  4         7  
  4         221  
5              
6 4     4   2135 use Class::Utils qw(set_params);
  4         55740  
  4         94  
7 4     4   2388 use Data::CEFACT::Unit;
  4         34347  
  4         165  
8 4     4   1618 use File::Share ':all';
  4         148056  
  4         871  
9 4     4   2232 use IO::File;
  4         44354  
  4         685  
10 4     4   40 use List::Util 1.33 qw(any);
  4         89  
  4         290  
11 4     4   2368 use Mo::utils::Array qw(check_array_object);
  4         11698  
  4         100  
12 4     4   4558 use Text::CSV_XS;
  4         76008  
  4         2276  
13              
14             our $VERSION = 0.02;
15              
16             sub new {
17 5     5 1 684232 my ($class, @params) = @_;
18              
19             # Create object.
20 5         14 my $self = bless {}, $class;
21              
22             # Units list.
23 5         20 $self->{'units'} = [];
24              
25             # Process parameters.
26 5         37 set_params($self, @params);
27              
28             # Init all units.
29 4         59 check_array_object($self, 'units', 'Data::CEFACT::Unit');
30 2 50       48 if (! @{$self->{'units'}}) {
  2         13  
31 2         13 $self->_init;
32             }
33              
34 2         15 return $self;
35             }
36              
37             sub check_common_code {
38 2     2 1 1315 my ($self, $common_code) = @_;
39              
40 2         7 my $ret;
41 2 100   3493   12 if (any { $_->common_code eq $common_code } @{$self->{'units'}}) {
  3493         23530  
  2         189  
42 1         17 $ret = 1;
43             } else {
44 1         15 $ret = 0;
45             }
46              
47 2         23 return $ret;
48             }
49              
50             sub _init {
51 2     2   5 my $self = shift;
52              
53             # Object.
54 2         34 my $csv = Text::CSV_XS->new({
55             'binary' => 1,
56             'escape_char' => '"',
57             'quote_char' => '"',
58             'sep_char' => ',',
59             });
60              
61             # Parse file.
62 2         509 my $fh = IO::File->new;
63 2         139 my $csv_file = dist_file('CEFACT-Unit', 'code-list.csv');
64 2         527 $fh->open($csv_file, 'r');
65 2         225 my $i = 0;
66 2         251 while (my $columns_ar = $csv->getline($fh)) {
67 4274         347463 $i++;
68              
69             # Header.
70 4274 100       7872 if ($i == 1) {
71 2         28 next;
72             }
73              
74 4272         6289 for (my $i = 0; $i < @{$columns_ar}; $i++) {
  34176         56276  
75 29904 100       51598 $columns_ar->[$i] = $columns_ar->[$i] ne '' ? $columns_ar->[$i] : undef;
76             }
77              
78 4272         5529 push @{$self->{'units'}}, Data::CEFACT::Unit->new(
  4272         15193  
79             'common_code' => $columns_ar->[1],
80             'conversion_factor' => $columns_ar->[6],
81             'description' => $columns_ar->[3],
82             'level_category' => $columns_ar->[4],
83             'symbol' => $columns_ar->[5],
84             'name' => $columns_ar->[2],
85             'status' => $columns_ar->[0],
86             );
87             }
88 2         302 $fh->close;
89              
90 2         116 return;
91             }
92              
93             1;
94              
95             __END__
96              
97             =pod
98              
99             =encoding utf8
100              
101             =head1 NAME
102              
103             CEFACT::Unit - CEFACT unit handling.
104              
105             =head1 SYNOPSIS
106              
107             use CEFACT::Unit;
108              
109             my $obj = CEFACT::Unit->new(%params);
110             my $bool = $obj->check_common_code($unit_common_code);
111              
112             =head1 METHODS
113              
114             =head2 C<new>
115              
116             my $obj = CEFACT::Unit->new(%params);
117              
118             Constructor.
119              
120             =over 8
121              
122             =item * C<units>
123              
124             List of units in L<Data::CEFACT::Unit> instances.
125              
126             Default value is [].
127              
128             =back
129              
130             Returns instance of object.
131              
132             =head2 C<check_common_code>
133              
134             my $bool = $obj->check_common_code($unit_common_code);
135              
136             Check UN/CEFACT unit common code.
137              
138             Returns bool (0/1).
139              
140             =head1 ERRORS
141              
142             new():
143             From Class::Utils::set_params():
144             Unknown parameter '%s'.
145              
146             From Mo::utils::Array::check_array_object():
147             Parameter 'units' must be a array.
148             Value: %s
149             Reference: %s
150             Parameter 'units' with array must contain 'Data::CEFACT::Unit' objects.
151             Value: %s
152             Reference: %s
153              
154             =head1 EXAMPLE
155              
156             =for comment filename=check_unit_common_code.pl
157              
158             use strict;
159             use warnings;
160              
161             use CEFACT::Unit;
162              
163             if (@ARGV < 1) {
164             print STDERR "Usage: $0 unit_common_code\n";
165             exit 1;
166             }
167             my $unit_common_code = $ARGV[0];
168              
169             # Object.
170             my $obj = CEFACT::Unit->new;
171              
172             # Check unit common code.
173             my $bool = $obj->check_common_code($unit_common_code);
174              
175             # Print out.
176             print "Unit '$unit_common_code' is ".($bool ? 'valid' : 'invalid')."\n";
177              
178             # Output for 'KGM':
179             # Unit 'KGM' is valid
180              
181             # Output for 'XXX':
182             # Unit 'XXX' is invalid
183              
184             =head1 DEPENDENCIES
185              
186             L<Class::Utils>,
187             L<Data::CEFACT::Unit>,
188             L<File::Share>,
189             L<IO::File>,
190             L<List::Util>,
191             L<Mo::utils::Array>,
192             L<Text::CSV_XS>.
193              
194             =head1 REPOSITORY
195              
196             L<https://github.com/michal-josef-spacek/CEFACT-Unit>
197              
198             =head1 AUTHOR
199              
200             Michal Josef Špaček L<mailto:skim@cpan.org>
201              
202             L<http://skim.cz>
203              
204             =head1 LICENSE AND COPYRIGHT
205              
206             © 2024-2025 Michal Josef Špaček
207              
208             BSD 2-Clause License
209              
210             =head1 VERSION
211              
212             0.02
213              
214             =cut