PICA/Schema/Builder.pm

filter: Statementstmt 100% 57 / 57 Branchbran 90.9% 20 / 22 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 66.6% 2 / 3 totaltotal 94.6% 89 / 94 CC 18 SCAR 25.2
CC
18
Cov
96%
add
17.0
schema
2.0
BEGIN
1.0
CRAP 12.4
« PICA/Path.pm ↑ Summary PICA/Writer/Base.pm »
1 package PICA::Schema::Builder;
2 2 ▶use v5.14.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 our $VERSION = '2.12';
5  
6 2 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use parent 'PICA::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 2 my $class = shift;
13 2 bless {fields => {}, records => 0, @_}, $class;
14 }
15  
16 5 ▶sub add {
Subroutine add: called CC=17 SCAR=28.4
Pod documented
17 5 my ($self, $record) = @_;
18  
19 5 ▶ $record = $record->{record} if reftype $record eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($record) eq "HASH" 0 5 5
20 5 my $fields = $self->{fields};
21  
22 5 my %field_identifiers;
23 5 foreach (@$record) {
24 8 my ($tag, $occ, @content) = @$_;
25  
26 8 my $id = $self->field_identifier($_);
27  
28 # check whether field is repeated within this record
29 8 ▶ if ($field_identifiers{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_identifiers{$id}) { } 1 7 8
30 1 $fields->{$id}{repeatable} = \1;
31 }
32 else {
33 7 $field_identifiers{$id} = 1;
34 }
35  
36 # field has not been seen in any record
37 8 ▶ if (!$fields->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields->{$id}) 3 5 8
38 3 ▶ next if $self->{ignore_unknown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ignore_unknown"} 0 3 3
39 3 $fields->{$id}
40 = {total => 0, records => 0, tag => $tag, subfields => {},};
41 3 ▶ $fields->{$id}{occurrence} = $occ if $occ > 0 && length $id gt 4;
Branch: 1 branch
BranchTrueFalseTotal
if $occ > 0 and length $id gt 4 1 2 3
Condition66%2/3
$occ > 0 and length $id gt 4
!ll&&!rl&&r
201
MC/DC50%1/2
$occ > 0 and length $id gt 4
$occ > 0 length $id gt 4
Truth table66%2/3
A and B
A= $occ > 0B= length $id gt 4
ABresult
0X0
100
111
42 3 ▶ $fields->{$id}{required} = \1 unless $self->{records};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"records"} 1 2 3
43 }
44  
45 8 my $subfields = $fields->{$id}{subfields};
46 8 my %subfield_codes;
47 8 ▶ pop @content if @content % 2; # remove annotation
Branch: 1 branch
BranchTrueFalseTotal
if @content % 2 1 7 8
48 8 ▶ while (@content) {
Branch: 1 branch
BranchTrueFalseTotal
if (@content) 10 8 18
49 10 my ($code, $value) = splice(@content, 0, 2);
50  
51 # check whether subfield is repeated within this field
52 10 ▶ if ($subfield_codes{$code}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subfield_codes{$code}) { } 1 9 10
53 1 $subfields->{$code}{repeatable} = \1;
54 }
55 else {
56 9 $subfield_codes{$code} = 1;
57 }
58  
59 10 ▶ if (!$subfields->{$code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subfields->{$code}) 6 4 10
60 6 $subfields->{$code} = {code => $code};
61 $subfields->{$code}{required} = \1
62 6 ▶ unless $fields->{$id}{total};
Branch: 1 branch
BranchTrueFalseTotal
unless $fields->{$id}{"total"} 4 2 6
63 }
64 }
65  
66 # subfields not given in this field are not required
67 8 for (grep {!$subfield_codes{$_}} keys %$subfields) {
68 2 delete $subfields->{$_}{required};
69 }
70  
71 # count total number of this field
72 8 $fields->{$id}{total}++;
73 }
74  
75 5 for (grep {exists $fields->{$_}} keys %field_identifiers) {
76 7 $fields->{$_}{records}++;
77 }
78  
79 # fields not given in this record are not required
80 5 for (grep {!$field_identifiers{$_}} keys %$fields) {
81 2 delete $fields->{$_}{required};
82 }
83  
84 5 $self->{records}++;
85 }
86  
87 3 ▶sub schema {
Subroutine schema: called CC=2 SCAR=6.9
Pod documented
88 3 my $schema = dclone($_[0]->TO_JSON);
89 3 my $fields = $schema->{fields};
90  
91 3 delete $fields->{$_} for grep {!$fields->{$_}{total}} keys %$fields;
92  
93 3 return PICA::Schema->new($schema);
94 }
95  
96 2 1;
97 __END__
« PICA/Path.pm ↑ Summary PICA/Writer/Base.pm »