File Coverage

blib/lib/MARC/SubjectMap/Rules.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package MARC::SubjectMap::Rules;
2              
3 7     7   1052 use strict;
  7         13  
  7         261  
4 7     7   46 use warnings;
  7         12  
  7         187  
5 7     7   4378 use BerkeleyDB;
  0            
  0            
6             use File::Temp qw( tempfile );
7             use Storable qw( freeze thaw );
8             use MARC::SubjectMap::XML qw( startTag endTag comment );
9             use Carp qw( croak );
10              
11             =head1 NAME
12              
13             MARC::SubjectMap::Rules - storage for rules
14              
15             =head1 SYNOPSIS
16              
17             my $rules = MARC::SubjectMap->new();
18             $rules->addRule( $rule );
19              
20             =head1 DESCRIPTION
21              
22             Since there may be a very large set of translation rules in a given
23             configuration the MARC::SubjectMap::Rules class allows the rules and lookup
24             tables to stored on disk rather than memory.
25              
26             =head1 METHODS
27              
28             =head2 new()
29              
30             Create rule storage.
31              
32             =cut
33              
34             sub new {
35             my ($class) = @_;
36             my ($fh,$filename) = tempfile();
37             #tie my %storage, 'DB_File', $filename, O_RDWR|O_CREAT, 0666, $DB_BTREE;
38             tie my %storage, 'BerkeleyDB::Btree';
39             return bless { rules => \%storage }, ref($class) || $class;
40             }
41              
42             =head2 addRule()
43              
44             Add a rule to the rules storage. A rule must be a MARC::SubjectMap::Rule
45             object.
46              
47             $rules->addRule( $rule );
48              
49             =cut
50              
51             sub addRule {
52             my ($self,$rule) = @_;
53             croak( "must supply MARC::SubjectMap::Rule object" )
54             if ref($rule) ne 'MARC::SubjectMap::Rule';
55             croak( "MARC::SubjectMap::Rule lacks field attribute" )
56             if ! $rule->field();
57             croak( "MARC::SubjectMap::Rule lacks subfield attribute" )
58             if ! $rule->subfield();
59             croak( "MARC::SubjectMap::Rule lacks original attribute" )
60             if ! $rule->original();
61            
62             # make key for storage
63             my $key = join ('-',$rule->original(),$rule->field(),$rule->subfield());
64              
65             # add the rule
66             $self->{rules}{$key} = freeze($rule);
67             }
68              
69             =head2 getRule()
70              
71             Look up a rule in storage using the field, subfield and original text.
72             If no rule is found you will be returned undef.
73            
74             my $rule = $rules->getRule( field => '600', subfield => 'a',
75             original => 'Africa' );
76              
77             =cut
78              
79             sub getRule {
80             my ($self,%args) = @_;
81             croak( "must supply field parameter" ) if ! exists $args{field};
82             croak( "must supply subfield parameter" ) if ! exists $args{subfield};
83             croak( "must supply original parameter" ) if ! exists $args{original};
84             my $key = join('-',$args{original},$args{field},$args{subfield});
85             return unless exists( $self->{rules}{$key} );
86             return thaw($self->{rules}{$key});
87             }
88              
89             ## there can be lots of rules so this takes a filehandle
90              
91             sub toXML {
92             my ($self,$fh) = @_;
93             print $fh comment( "the rule mappings themselves" ), "\n";
94             print $fh startTag( "rules" ), "\n\n";
95             while ( my($k,$v) = each(%{$self->{rules}}) ) {
96             my $rule = thaw($v);
97             print $fh $rule->toXML(), "\n";
98             }
99             print $fh endTag( "rules" ), "\n";
100             }
101              
102             1;