File Coverage

blib/lib/MARC/SubjectMap/Field.pm
Criterion Covered Total %
statement 62 63 98.4
branch 21 32 65.6
condition 1 3 33.3
subroutine 14 14 100.0
pod 9 10 90.0
total 107 122 87.7


line stmt bran cond sub pod time code
1             package MARC::SubjectMap::Field;
2              
3 3     3   6084 use strict;
  3         9  
  3         121  
4 3     3   17 use warnings;
  3         4  
  3         103  
5 3     3   29 use Carp qw( croak );
  3         5  
  3         220  
6 3     3   692 use MARC::SubjectMap::XML qw( startTag endTag element emptyElement );
  3         7  
  3         2729  
7              
8             =head1 NAME
9              
10             MARC::SubjectMap::Field - represent field/subfield combinations to examine
11              
12             =head1 SYNOPSIS
13              
14             =head1 DESCRIPTION
15              
16             The MARC::SubjectMap configuration includes information about which
17             field/subfield combinations to examine. This is contained in the configuration
18             as a list of MARC::SubjectMap::Field objects which individually bundle up the
19             information.
20              
21             =head1 METHODS
22              
23             =head2 new()
24              
25             The constructor. Optionally you can supply tag, translate and copy during the
26             constructor call instead of using the setters.
27              
28             my $f = MARC::Subject::Field->new( { tag => '650', copy => ['a','b'] } )
29              
30             =cut
31              
32             sub new {
33 1     1 1 908 my ( $class, $args ) = @_;
34 1 50       6 $args = {} unless ref($args) eq 'HASH';
35 1   33     9 my $self = bless $args, ref($class) || $class;
36             # set up defaults
37 1 50       8 $self->{translate} = [] unless exists $self->{translate};
38 1 50       4 $self->{copy} = [] unless exists $self->{copy};
39 1 50       4 $self->{sourceSubfield} = 'a' unless exists $self->{sourceSubfield};
40 1 50       4 $self->{indicator1} = undef unless exists $self->{indicator1};
41 1 50       3 $self->{indicator2} = undef unless exists $self->{indicator2};
42 1         3 return $self;
43             }
44              
45             =head2 tag()
46              
47             Returns the tag for the field, for example: 600 or 650.
48              
49             =cut
50              
51             sub tag {
52 3     3 1 495 my ($self,$tag) = @_;
53 3 100       10 if ($tag) { $self->{tag} = $tag };
  1         2  
54 3         10 return $self->{tag};
55             }
56              
57             =head2 translate()
58              
59             Gets a list of subfields to translate in the field.
60              
61             =cut
62              
63             sub translate {
64 5     5 1 8 return @{ shift->{translate} };
  5         19  
65             }
66              
67             =head2 addTranslate()
68              
69             Adds a subfield to translate.
70              
71             =cut
72              
73             sub addTranslate {
74 3     3 1 44 my ($self,$subfield) = @_;
75 6         39 croak( "can't both translate and copy subfield $subfield" )
76 3 100       8 if grep { $subfield eq $_ } $self->copy();
77 2 50       5 push( @{ $self->{translate} }, $subfield ) if defined($subfield);
  2         7  
78             }
79              
80             =head2 copy()
81              
82             Gets a list of subfields to copy in the field.
83              
84             =cut
85              
86             sub copy {
87 5     5 1 7 return @{ shift->{copy} };
  5         27  
88             }
89              
90             =head2 addCopy()
91              
92             Adds a subfield to copy.
93              
94             =cut
95              
96             sub addCopy {
97 3     3 1 25 my ($self,$subfield) = @_;
98 2         48 croak( "can't both copy and translate subfield $subfield" )
99 3 100       6 if grep { $subfield eq $_ } $self->translate();
100 2 50       8 push( @{ $self->{copy} }, $subfield ) if defined($subfield);
  2         7  
101             }
102              
103             =head2 sourceSubfield()
104              
105             When a new subfield is constructed for this field the $2 or source for
106             the heading will be determined by the source for a particular subfield
107             rule that was used when building the new field. Since subfield components
108             could potentially have different sources sourceSubfield() lets you
109             specify which subfield to pull the source from. If unspecified sourceSubfield()
110             will always return 'a'.
111              
112             =cut
113              
114             sub sourceSubfield {
115 1     1 1 1 my ($self,$subfield) = @_;
116 1 50       4 if ($subfield) { $self->{sourceSubfield} = $subfield };
  0         0  
117 1         4 return $self->{sourceSubfield};
118             }
119              
120             =head2 indicator1()
121              
122             Specify a value to limit by for the 1st indicator. Using this will mean
123             that *only* fields with 1st indicator of this value will get processed.
124             By default records will not be limited if this value is unspecified.
125              
126             =head2 indicator2()
127              
128             Same as indicator1() but for the 2nd indicator.
129              
130             =cut
131              
132             sub indicator1 {
133 4     4 1 6 my ($self,$indicator) = @_;
134 4 100       8 if ( defined $indicator) { $self->{indicator1} = $indicator };
  1         2  
135 4         15 return $self->{indicator1};
136             }
137              
138             sub indicator2 {
139 4     4 1 8 my ($self,$indicator) = @_;
140 4 100       11 if ( defined $indicator) { $self->{indicator2} = $indicator };
  1         3  
141 4         18 return $self->{indicator2};
142             }
143              
144             sub toXML {
145 1     1 0 3 my $self = shift;
146              
147             # get the attrs into an array with defined order (instead of hash)
148 1         4 my @attrs = ( tag => $self->tag() );
149 1 50       3 push( @attrs, indicator1 => $self->indicator1() )
150             if defined $self->indicator1();
151 1 50       1144 push( @attrs, indicator2 => $self->indicator2() )
152             if defined $self->indicator2();
153              
154 1         6 my $xml = startTag( "field", @attrs )."\n";
155 1         3 map { $xml .= element("copy",$_)."\n" } $self->copy();
  2         7  
156 1         4 map { $xml .= element("translate",$_)."\n" } $self->translate();
  2         5  
157 1         4 $xml .= element("sourceSubfield", $self->sourceSubfield()) . "\n";
158 1         4 $xml .= endTag("field")."\n";
159 1         25 return $xml;
160             }
161              
162             1;