blib/lib/MARC/Schema.pm

filter: Statementstmt 96.9% 63 / 65 Branchbran 88.4% 23 / 26 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 12 / 12 Podpod 66.6% 2 / 3 totaltotal 93.2% 111 / 119 CC 17 SCAR 19.3
CC
17
Cov
94%
check_field
13.1
_load_schema
2.0
new
2.0
CRAP 6.9
« script/marcvalidate ↑ Summary Build »
1 package MARC::Schema;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.05';
7  
8 3 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Share ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Slurper 'read_binary';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new {
14 4 ▶ my ($class, $arg_ref) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
15 4 ▶ my $self = $arg_ref // {};
Condition100%2/2
$arg_ref // {}
l!l
13
MC/DC100%1/1
$arg_ref // {}
$arg_ref
Truth table100%2/2
A
A= $arg_ref
Aresult
11
00
16 4 bless $self, $class;
17 4 $self->_initialize();
18 4 return $self;
19 }
20  
21 sub _initialize {
22 4 ▶ my ($self) = shift;
Subroutine _initialize: called CC=2 SCAR=6.9
23 4 ▶ if (!$self->{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fields"}) 3 1 4
24 3 $self->{fields} = $self->_load_schema();
25 }
26 4 return;
27 }
28  
29 sub _load_schema {
30 3 ▶ my ($self) = shift;
Subroutine _load_schema: called CC=2 SCAR=7.1
31 3 my $json;
32 3 ▶ if ($self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'file'}) { } 0 3 3
33 0 $json = read_binary($self->{file});
34 }
35 else {
36 3 $self->{file} = dist_file('MARC-Schema', 'marc-schema.json');
37 3 $json = read_binary($self->{file});
38 }
39 3 my $schema = decode_json($json);
40  
41 3 return $schema->{fields};
42 }
43  
44 sub check {
45 11 ▶ my ($self, $record, %options) = @_;
Subroutine check: called CC=2 SCAR=6.9
Pod documented
46  
47 11 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 1 10 11
48  
49 11 $options{counter} = {};
50 11 return map {$self->check_field($_, %options)} @$record;
51 }
52  
53 sub _error {
54 3 ▶ my $field = shift;
Subroutine _error: called CC=1 SCAR=0.0
55 3 return {tag => $field->[0], @_};
56 }
57  
58 sub check_field {
59 190 ▶ my ($self, $field, %options) = @_;
Subroutine check_field: called CC=13 SCAR=25.7
Pod documented
60  
61 190 my $spec = $self->{fields}->{$field->[0]};
62  
63 190 ▶ if (!$spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec) 1 189 190
64 1 ▶ if (!$options{ignore_unknown_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $options{'ignore_unknown_fields'}) { } 1 0 1
65 1 return _error($field, message => 'unknown field');
66 }
67 else {
68 0 return ();
69 }
70 }
71  
72 189 ▶ if ($options{counter} && !$spec->{repeatable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"counter"} and not $spec->{"repeatable"}) 110 79 189
Condition66%2/3
$options{"counter"} and not $spec->{"repeatable"}
!ll&&!rl&&r
079110
MC/DC50%1/2
$options{"counter"} and not $spec->{"repeatable"}
$options{"counter"} not $spec->{"repeatable"}
Truth table66%2/3
A and not(B)
A= $options{"counter"}B= not $spec->{"repeatable"}
ABresult
0X0
100
111
73 110 ▶ if ($options{counter}{$field->[0]}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"counter"}{$field->[0]}++) 1 109 110
74 1 return _error(
75 $field,
76 repeatable => 'false',
77 message => 'field is not repeatable'
78 );
79 }
80 }
81  
82 188 my %errors;
83 188 ▶ if ($spec->{subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"subfields"}) 136 52 188
84 136 my %sfcounter;
85 136 my (undef, undef, undef, @subfields) = @$field;
86 136 ▶ while (@subfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subfields) 221 136 357
87 221 my ($code, undef) = splice @subfields, 0, 2;
88 221 my $sfspec = $spec->{subfields}->{$code};
89  
90 221 ▶ if ($sfspec) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sfspec) { } 220 1 221
elsif (not $options{'ignore_unknown_subfields'}) { } 1 0 1
91 220 ▶ if (!$sfspec->{repeatable} && $sfcounter{$code}++) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sfspec->{"repeatable"} and $sfcounter{$code}++) 1 219 220
Condition100%3/3
not $sfspec->{"repeatable"} and $sfcounter{$code}++
!ll&&!rl&&r
831361
MC/DC100%2/2
not $sfspec->{"repeatable"} and $sfcounter{$code}++
not $sfspec->{"repeatable"} $sfcounter{$code}++
Truth table100%3/3
not(A) and B
A= not $sfspec->{"repeatable"}B= $sfcounter{$code}++
ABresult
0X0
100
111
92 $errors{$code} = {
93 message => 'subfield is not repeatable',
94 label => $sfspec->{label},
95 1 repeatable => 'false'
96 };
97 }
98 }
99 elsif (!$options{ignore_unknown_subfields}) {
100 1 $errors{$code} = {message => 'unknown subfield'};
101 }
102 }
103 }
104  
105 188 ▶ return %errors ? _error($field, subfields => \%errors) : ();
Branch: 1 branch
BranchTrueFalseTotal
%errors ? : 1 187 188
106 }
107  
108 3 1;
109 __END__
« script/marcvalidate ↑ Summary Build »