blib/lib/MARC/Schema.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 93.7% 30 / 32 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 11 / 11 Podpod 66.6% 2 / 3 totaltotal 94.4% 137 / 145 CC 22 SCAR 23.9
CC
22
Cov
96%
check_field
18.1
new
2.0
_initialize
2.0
CRAP 10.9
↑ Summary script/marcvalidate »
1 package MARC::Schema;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.16';
7  
8 5 ▶use Cpanel::JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use File::Share ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use File::Slurper 'read_binary';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 6 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
14 6 my ($class, $arg_ref) = @_;
15 6 ▶ my $self = $arg_ref // {};
Condition100%2/2
$arg_ref // {}
l!l
42
MC/DC100%1/1
$arg_ref // {}
$arg_ref
Truth table100%2/2
A
A= $arg_ref
Aresult
11
00
16 6 bless $self, $class;
17 6 $self->_initialize();
18 6 return $self;
19 }
20  
21 6 ▶sub _initialize {
Subroutine _initialize: called CC=2 SCAR=6.9
22 6 my ($self) = shift;
23 6 ▶ if (!$self->{fields}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fields"}) 5 1 6
24 5 $self->{fields} = $self->_load_schema();
25 }
26 6 return;
27 }
28  
29 5 ▶sub _load_schema {
Subroutine _load_schema: called CC=2 SCAR=6.9
30 5 my ($self) = shift;
31 5 my $json;
32 5 ▶ if ($self->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'file'}) { } 1 4 5
33 1 $json = read_binary($self->{file});
34 }
35 else {
36 4 $self->{file} = dist_file('MARC-Schema', 'marc-schema.json');
37 4 $json = read_binary($self->{file});
38 }
39 5 my $schema = decode_json($json);
40  
41 5 return $schema->{fields};
42 }
43  
44 31 ▶sub check {
Subroutine check: called CC=2 SCAR=6.9
Pod documented
45 31 my ($self, $record, %options) = @_;
46  
47 31 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 1 30 31
48  
49 31 $options{counter} = {};
50 31 return map {$self->check_field($_, %options)} @$record;
51 }
52  
53 558 ▶sub check_field {
Subroutine check_field: called CC=18 SCAR=29.0
Pod documented
54 558 my ($self, $field, %options) = @_;
55  
56 558 my $tag = $field->[0];
57  
58 558 my $spec = $self->{fields}->{$tag};
59  
60 558 ▶ if (!$spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($spec) 1 557 558
61 1 ▶ if (!$options{ignore_unknown_fields}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $options{'ignore_unknown_fields'}) { } 1 0 1
62 1 return ({tag => $tag, error => 'unknown field', type => 'field'});
63 }
64 else {
65 0 return ();
66 }
67 }
68  
69 557 ▶ if ($options{counter} && !$spec->{repeatable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"counter"} and not $spec->{"repeatable"}) 324 233 557
Condition66%2/3
$options{"counter"} and not $spec->{"repeatable"}
!ll&&!rl&&r
0233324
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
70 324 ▶ if ($options{counter}{$field->[0]}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"counter"}{$field->[0]}++) 1 323 324
71 return (
72 {
73 1 tag => $tag,
74 error => 'field is not repeatable',
75 type => 'field',
76 }
77 );
78 }
79 }
80  
81 556 my @errors;
82 556 ▶ if ($spec->{subfields}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"subfields"}) 404 152 556
83 404 my %sfcounter;
84 404 my (undef, undef, undef, @subfields) = @$field;
85 404 ▶ while (@subfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subfields) 649 404 1053
86 649 my ($code, undef) = splice @subfields, 0, 2;
87 649 my $sfspec = $spec->{subfields}->{$code};
88  
89 649 ▶ if ($sfspec) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sfspec) { } 648 1 649
elsif (not $options{'ignore_unknown_subfields'}) { } 1 0 1
90 648 ▶ if (!$sfspec->{repeatable} && $sfcounter{$code}++) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sfspec->{"repeatable"} and $sfcounter{$code}++) 1 647 648
Condition100%3/3
not $sfspec->{"repeatable"} and $sfcounter{$code}++
!ll&&!rl&&r
2454021
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
91 1 push @errors,
92 {
93 tag => $tag,
94 error => 'subfield is not repeatable',
95 type => 'subfield',
96 value => $code,
97 };
98 }
99 }
100 elsif (!$options{ignore_unknown_subfields}) {
101 1 push @errors,
102 {
103 tag => $tag,
104 error => "unknown subfield",
105 type => 'subfield',
106 value => $code,
107 };
108 }
109 }
110 }
111  
112 556 ▶ if ($spec->{indicator1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"indicator1"}) 221 335 556
113 221 my (undef, $code, @other) = @$field;
114 221 ▶ $code //= ' ';
Condition100%2/2
$code //= " "
l!l
2192
MC/DC100%1/1
$code //= " "
$code
Truth table100%2/2
A
A= $code
Aresult
11
00
115 my (@matches)
116 221 = grep {$code =~ /^[$_]/} keys %{$spec->{indicator1}->{codes}};
117  
118 221 ▶ if (@matches > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches > 0) { } 217 4 221
119  
120 # everything is ok
121 }
122 else {
123 4 push @errors,
124 {
125 tag => $tag,
126 error => 'unknown first indicator',
127 type => 'indicator',
128 value => $code,
129 };
130 }
131 }
132  
133 556 ▶ if ($spec->{indicator2}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"indicator2"}) 158 398 556
134 158 my (undef, undef, $code, @other) = @$field;
135 158 ▶ $code //= ' ';
Condition50%1/2
$code //= " "
l!l
1580
MC/DC0%0/1
$code //= " "
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
136 my (@matches)
137 158 = grep {$code =~ /^[$_]/} keys %{$spec->{indicator2}->{codes}};
138  
139 158 ▶ if (@matches > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@matches > 0) { } 157 1 158
140  
141 # everything is ok
142 }
143 else {
144 1 push @errors,
145 {
146 tag => $tag,
147 error => 'unknown second indicator',
148 type => 'indicator',
149 value => $code,
150 };
151 }
152 }
153  
154 556 return @errors;
155 }
156  
157 5 1;
158 __END__
↑ Summary script/marcvalidate »