]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/MFHD/Caption.pm
Check for compressibility at parse/load time, not during formatting..
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / MFHD / Caption.pm
1 package MFHD::Caption;
2 use strict;
3 use integer;
4 use Carp;
5
6 use MARC::Record;
7
8 sub new
9 {
10     my $proto = shift;
11     my $class = ref($proto) || $proto;
12     my $caption = shift;
13     my $self = {};
14     my $last_enum = undef;
15
16     $self->{CAPTION} = $caption;
17     $self->{ENUMS} = {};
18     $self->{CHRONS} = {};
19     $self->{PATTERN} = {};
20     $self->{COPY} = undef;
21     $self->{UNIT} = undef;
22     $self->{COMPRESSIBLE} = 1;  # until proven otherwise
23
24     foreach my $subfield ($caption->subfields) {
25         my ($key, $val) = @$subfield;
26         if ($key eq '8') {
27             $self->{LINK} = $val;
28         } elsif ($key =~ /[a-h]/) {
29             # Enumeration Captions
30             $self->{ENUMS}->{$key} = {CAPTION => $val,
31                                       COUNT => undef,
32                                       RESTART => undef};
33             if ($key =~ /[ag]/) {
34                 $last_enum = undef;
35             } else {
36                 $last_enum = $key;
37             }
38         } elsif ($key =~ /[i-m]/) {
39             # Chronology captions
40             $self->{CHRONS}->{$key} = $val;
41         } elsif ($key eq 'u') {
42             # Bib units per next higher enumeration level
43             carp('$u specified for top-level enumeration')
44               unless defined($last_enum);
45             $self->{ENUMS}->{$last_enum}->{COUNT} = $val;
46         } elsif ($key eq 'v') {
47             carp '$v specified for top-level enumeration'
48               unless defined($last_enum);
49             $self->{ENUMS}->{$last_enum}->{RESTART} = ($val eq 'r');
50         } elsif ($key =~ /[npw-z]/) {
51             # Publication Pattern ('o' == type of unit, 'q'..'t' undefined)
52             $self->{PATTERN}->{$key} = $val;
53         } elsif ($key eq 'o') {
54             # Type of unit
55             $self->{UNIT} = $val;
56         } elsif ($key eq 't') {
57             $self->{COPY} = $val;
58         } else {
59             carp "Unknown caption subfield '$key'";
60         }
61     }
62
63     # subsequent levels of enumeration (primary and alternate)
64     # If an enumeration level doesn't document the number
65     # of "issues" per "volume", or whether numbering of issues
66     # restarts, then we can't compress.
67     foreach my $key ('b', 'c', 'd', 'e', 'f', 'h') {
68         if (exists $self->{ENUMS}->{$key}) {
69             my $pattern = $self->{ENUMS}->{$key};
70             if (!$pattern->{RESTART} || !$pattern->{COUNT}
71                 || ($pattern->{COUNT} eq 'var')
72                 || ($pattern->{COUNT} eq 'und')) {
73                 $self->{COMPRESSIBLE} = 0;
74                 last;
75             }
76         }
77     }
78
79     # If there's a $x subfield and a $j, then it's compressible
80     if (exists $self->{PATTERN}->{x} && exists $self->{CHRONS}->{'j'}) {
81         $self->{COMPRESSIBLE} = 1;
82     }
83
84     bless ($self, $class);
85     return $self;
86 }
87
88 sub compressible {
89     my $self = shift;
90
91     return $self->{COMPRESSIBLE};
92 }
93
94 sub caption {
95     my $self = shift;
96     my $key;
97
98     if (@_) {
99         $key = shift;
100         return $self->{ENUMS}->{$key}->{CAPTION};
101     } else {
102         return $self->{CAPTION};
103     }
104 }
105
106 1;