| 1 | package PICA::Schema::Builder; | ||||||||||
| 2 | 1 | ▶ | use v5.14.1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 1 | our $VERSION = '1.18'; | |||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use PICA::Schema qw(field_identifier); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Scalar::Util qw(reftype); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | sub new { | ||||||||||
| 10 | 1 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 11 | 1 | bless {fields => {}, counter => 0}, $class; | |||||||||
| 12 | } | ||||||||||
| 13 | |||||||||||
| 14 | sub add { | ||||||||||
| 15 | 4 | ▶ | my ($self, $record) = @_; | ||||||||
|
Subroutine
add: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||
| 16 | |||||||||||
| 17 | 4 | ▶ | $record = $record->{record} if reftype $record eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 4 | my $fields = $self->{fields}; | |||||||||
| 19 | |||||||||||
| 20 | 4 | my %field_identifiers; | |||||||||
| 21 | 4 | foreach (@$record) { | |||||||||
| 22 | 6 | my ($tag, $occ, @content) = @$_; | |||||||||
| 23 | |||||||||||
| 24 | 6 | my $id = field_identifier($_); | |||||||||
| 25 | |||||||||||
| 26 | # check whether field is repeated within thin record | ||||||||||
| 27 | 6 | ▶ | if ($field_identifiers{$id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | $fields->{$id}{repeatable} = \1; | |||||||||
| 29 | } | ||||||||||
| 30 | else { | ||||||||||
| 31 | 6 | $field_identifiers{$id} = 1; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | # field has not been inspected yet | ||||||||||
| 35 | 6 | ▶ | if (!$fields->{$id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 3 | $fields->{$id} = {counter => 0, tag => $tag, subfields => {},}; | |||||||||
| 37 | 3 | ▶ | $fields->{$id}{occurrence} = $occ if length $id gt 4; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 3 | ▶ | $fields->{$id}{required} = \1 unless $self->{counter}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | } | ||||||||||
| 40 | else { | ||||||||||
| 41 | 3 | $fields->{$id}{counter}++; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 6 | my $subfields = $fields->{$id}{subfields}; | |||||||||
| 45 | 6 | my %subfield_codes; | |||||||||
| 46 | 6 | ▶ | while (@content) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 8 | my ($code, $value) = splice(@content, 0, 2); | |||||||||
| 48 | |||||||||||
| 49 | # check whether subfield is repeated within this field | ||||||||||
| 50 | 8 | ▶ | if ($subfield_codes{$code}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 1 | $subfields->{$code}{repeatable} = \1; | |||||||||
| 52 | } | ||||||||||
| 53 | else { | ||||||||||
| 54 | 7 | $subfield_codes{$code} = 1; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 8 | ▶ | if (!$subfields->{$code}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 5 | $subfields->{$code} = {code => $code}; | |||||||||
| 59 | $subfields->{$code}{required} = \1 | ||||||||||
| 60 | 5 | ▶ | unless $fields->{$id}{counter}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | # subfields not given in this field are not required | ||||||||||
| 65 | 6 | for (grep {!$subfield_codes{$_}} keys %$subfields) { | |||||||||
| 66 | 1 | delete $subfields->{$_}{required}; | |||||||||
| 67 | } | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | # fields not given in this record are not required | ||||||||||
| 71 | 4 | for (grep {!$field_identifiers{$_}} keys %$fields) { | |||||||||
| 72 | 2 | delete $fields->{$_}{required}; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 4 | $self->{counter}++; | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub schema { | ||||||||||
| 79 | 4 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
schema: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 80 | PICA::Schema->new( | ||||||||||
| 81 | { | ||||||||||
| 82 | fields => { | ||||||||||
| 83 | map { | ||||||||||
| 84 | 8 | my %field = %{$self->{fields}{$_}}; | |||||||||
| 85 | 8 | delete $field{counter}; | |||||||||
| 86 | 8 | $_ => \%field; | |||||||||
| 87 | 4 | } keys %{$self->{fields}} | |||||||||
| 88 | } | ||||||||||
| 89 | } | ||||||||||
| 90 | ); | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 1 | 1; | |||||||||
| 94 | __END__ | ||||||||||