marc 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/marc.rb +1 -0
- data/lib/marc/dublincore.rb +79 -0
- data/lib/marc/record.rb +10 -0
- data/test/tc_dublincore.rb +14 -0
- metadata +4 -2
data/lib/marc.rb
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
module MARC
|
2
|
+
|
3
|
+
# A class for mapping MARC records to Dublin Core
|
4
|
+
|
5
|
+
class DublinCore
|
6
|
+
|
7
|
+
def self.map(record)
|
8
|
+
dc_hash = Hash.new
|
9
|
+
dc_hash['title'] = get_field_value(record['245']['a'])
|
10
|
+
|
11
|
+
# Creator
|
12
|
+
[100, 110, 111, 700, 710, 711, 720].each do |field|
|
13
|
+
dc_hash['creator'] ||= []
|
14
|
+
dc_hash['creator'] << get_field_value(record[field.to_s])
|
15
|
+
end
|
16
|
+
|
17
|
+
# Subject
|
18
|
+
[600, 610, 611, 630, 650, 653].each do |field|
|
19
|
+
dc_hash['subject'] ||= []
|
20
|
+
dc_hash['subject'] << get_field_value(record[field.to_s])
|
21
|
+
end
|
22
|
+
|
23
|
+
# Description
|
24
|
+
[500..599].each do |field|
|
25
|
+
next if [506, 530, 540, 546].include?(field)
|
26
|
+
dc_hash['description'] ||= []
|
27
|
+
dc_hash['description'] << get_field_value(record[field.to_s])
|
28
|
+
end
|
29
|
+
|
30
|
+
dc_hash['publisher'] = get_field_value(record['260']['a']['b']) rescue nil
|
31
|
+
dc_hash['date'] = get_field_value(record['260']['c']) rescue nil
|
32
|
+
dc_hash['type'] = get_field_value(record['655'])
|
33
|
+
dc_hash['format'] = get_field_value(record['856']['q']) rescue nil
|
34
|
+
dc_hash['identifier'] = get_field_value(record['856']['u']) rescue nil
|
35
|
+
dc_hash['source'] = get_field_value(record['786']['o']['t']) rescue nil
|
36
|
+
dc_hash['language'] = get_field_value(record['546'])
|
37
|
+
|
38
|
+
dc_hash['relation'] = []
|
39
|
+
dc_hash['relation'] << get_field_value(record['530'])
|
40
|
+
[760..787].each do |field|
|
41
|
+
dc_hash['relation'] << get_field_value(record[field.to_s]['o']['t']) rescue nil
|
42
|
+
end
|
43
|
+
|
44
|
+
[651, 752].each do |field|
|
45
|
+
dc_hash['coverage'] ||= []
|
46
|
+
dc_hash['coverage'] << get_field_value(record[field.to_s])
|
47
|
+
end
|
48
|
+
|
49
|
+
[506, 540].each do |field|
|
50
|
+
dc_hash['rights'] ||= []
|
51
|
+
dc_hash['rights'] << get_field_value(record[field.to_s])
|
52
|
+
end
|
53
|
+
|
54
|
+
dc_hash.keys.each do |key|
|
55
|
+
dc_hash[key].flatten! if dc_hash[key].respond_to?(:flatten!)
|
56
|
+
dc_hash[key].compact! if dc_hash[key].respond_to?(:compact!)
|
57
|
+
end
|
58
|
+
|
59
|
+
dc_hash
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.get_field_value(field)
|
63
|
+
return if field.nil?
|
64
|
+
|
65
|
+
if !field.kind_of?(String) && field.respond_to?(:each)
|
66
|
+
values = []
|
67
|
+
field.each do |element|
|
68
|
+
values << get_field_value(element)
|
69
|
+
end
|
70
|
+
values
|
71
|
+
else
|
72
|
+
return field if field.kind_of?(String)
|
73
|
+
return field.value if field.respond_to?(:value)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
data/lib/marc/record.rb
CHANGED
@@ -91,6 +91,16 @@ module MARC
|
|
91
91
|
return MARC::XMLWriter.encode(self)
|
92
92
|
end
|
93
93
|
|
94
|
+
# Handy method for returning a hash mapping this records values
|
95
|
+
# to the Dublin Core.
|
96
|
+
#
|
97
|
+
# dc = record.to_dublin_core()
|
98
|
+
# print dc['title']
|
99
|
+
|
100
|
+
def to_dublin_core
|
101
|
+
return MARC::DublinCore.map(self)
|
102
|
+
end
|
103
|
+
|
94
104
|
# Returns a string version of the record, suitable for printing
|
95
105
|
|
96
106
|
def to_s
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'marc'
|
3
|
+
|
4
|
+
class DublinCoreTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_mapping
|
7
|
+
reader = MARC::Reader.new('test/batch.dat')
|
8
|
+
reader.each do |record|
|
9
|
+
dc = record.to_dublin_core
|
10
|
+
assert dc['title'] == record['245']['a']
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: marc
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date:
|
6
|
+
version: 0.1.3
|
7
|
+
date: 2007-01-02 00:00:00 -05:00
|
8
8
|
summary: A ruby library for working with Machine Readable Cataloging
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -34,6 +34,7 @@ files:
|
|
34
34
|
- lib/marc/constants.rb
|
35
35
|
- lib/marc/controlfield.rb
|
36
36
|
- lib/marc/datafield.rb
|
37
|
+
- lib/marc/dublincore.rb
|
37
38
|
- lib/marc/exception.rb
|
38
39
|
- lib/marc/reader.rb
|
39
40
|
- lib/marc/record.rb
|
@@ -46,6 +47,7 @@ files:
|
|
46
47
|
- test/one.dat
|
47
48
|
- test/tc_controlfield.rb
|
48
49
|
- test/tc_datafield.rb
|
50
|
+
- test/tc_dublincore.rb
|
49
51
|
- test/tc_reader.rb
|
50
52
|
- test/tc_record.rb
|
51
53
|
- test/tc_subfield.rb
|