marc 0.1.3 → 0.1.4
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/record.rb +1 -1
- data/lib/marc/xmlwriter.rb +10 -10
- data/test/tc_controlfield.rb +14 -14
- data/test/tc_record.rb +2 -2
- data/test/tc_xml.rb +1 -0
- metadata +1 -1
data/lib/marc/record.rb
CHANGED
data/lib/marc/xmlwriter.rb
CHANGED
@@ -70,9 +70,9 @@ module MARC
|
|
70
70
|
# a static method that accepts a MARC::Record object
|
71
71
|
# and returns a REXML::Document for the XML serialization.
|
72
72
|
|
73
|
-
def self.encode(record)
|
74
|
-
singleChar = Regexp.new(
|
75
|
-
ctrlFieldTag = Regexp.new(
|
73
|
+
def self.encode(record, opts={})
|
74
|
+
singleChar = Regexp.new('[\da-z ]{1}')
|
75
|
+
ctrlFieldTag = Regexp.new('00[1-9A-Za-z]{1}')
|
76
76
|
|
77
77
|
# Right now, this writer handles input from the strict and
|
78
78
|
# lenient MARC readers. Because it can get 'loose' MARC in, it
|
@@ -86,8 +86,8 @@ module MARC
|
|
86
86
|
# TODO: At the very least there should be some logging
|
87
87
|
# to record our attempts to account for less than perfect MARC.
|
88
88
|
|
89
|
-
|
90
|
-
|
89
|
+
e = REXML::Element.new('record')
|
90
|
+
e.add_namespace(MARC_NS) if opts[:include_namespace]
|
91
91
|
|
92
92
|
# MARCXML only allows alphanumerics or spaces in the leader
|
93
93
|
record.leader.gsub!(/[^\w|^\s]/, 'Z')
|
@@ -104,7 +104,7 @@ module MARC
|
|
104
104
|
|
105
105
|
leader = REXML::Element.new("leader")
|
106
106
|
leader.add_text(record.leader)
|
107
|
-
|
107
|
+
e.add_element(leader)
|
108
108
|
|
109
109
|
for field in record.fields
|
110
110
|
if field.class == MARC::DataField
|
@@ -143,24 +143,24 @@ module MARC
|
|
143
143
|
datafield_elem.add_element(subfield_element)
|
144
144
|
end
|
145
145
|
|
146
|
-
|
146
|
+
e.add_element datafield_elem
|
147
147
|
elsif field.class == MARC::ControlField
|
148
148
|
control_element = REXML::Element.new("controlfield")
|
149
149
|
|
150
150
|
# We need a marker for invalid tag values (we use 000)
|
151
|
-
|
151
|
+
unless field.tag.match(ctrlFieldTag)
|
152
152
|
field.tag = "00z"
|
153
153
|
end
|
154
154
|
|
155
155
|
control_element.add_attribute("tag", field.tag)
|
156
156
|
text = MARC::XMLWriter.convert_to_utf8(field.value)
|
157
157
|
control_element.add_text(text)
|
158
|
-
|
158
|
+
e.add_element(control_element)
|
159
159
|
end
|
160
160
|
end
|
161
161
|
|
162
162
|
# return xml
|
163
|
-
return
|
163
|
+
return e
|
164
164
|
end
|
165
165
|
end
|
166
166
|
end
|
data/test/tc_controlfield.rb
CHANGED
@@ -3,23 +3,23 @@ require 'marc'
|
|
3
3
|
|
4
4
|
class TestField < Test::Unit::TestCase
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def test_control
|
7
|
+
control = MARC::ControlField.new('005', 'foobarbaz')
|
8
|
+
assert_equal(control.to_s, '005 foobarbaz')
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
11
|
+
def test_field_as_control
|
12
|
+
assert_raise(MARC::Exception) do
|
13
|
+
# can't have a field with a tag < 010
|
14
|
+
field = MARC::DataField.new('007')
|
16
15
|
end
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
18
|
+
def test_control_as_field
|
19
|
+
assert_raise(MARC::Exception) do
|
20
|
+
# can't have a control with a tag > 009
|
21
|
+
f = MARC::ControlField.new('245')
|
23
22
|
end
|
23
|
+
end
|
24
24
|
end
|
25
25
|
|
data/test/tc_record.rb
CHANGED
@@ -11,8 +11,8 @@ class TestRecord < Test::Unit::TestCase
|
|
11
11
|
def test_xml
|
12
12
|
r = get_record()
|
13
13
|
doc = r.to_xml
|
14
|
-
assert_kind_of REXML::
|
15
|
-
assert_equal "<record><leader> Z 22 4500</leader><datafield tag='100' ind1='2' ind2='0'><subfield code='a'>Thomas, Dave</subfield></datafield><datafield tag='245' ind1='0' ind2='4'><subfield code='The Pragmatic Programmer'></subfield></datafield></record>", doc.to_s
|
14
|
+
assert_kind_of REXML::Element, doc
|
15
|
+
assert_equal "<record xmlns='http://www.loc.gov/MARC21/slim'><leader> Z 22 4500</leader><datafield tag='100' ind1='2' ind2='0'><subfield code='a'>Thomas, Dave</subfield></datafield><datafield tag='245' ind1='0' ind2='4'><subfield code='The Pragmatic Programmer'></subfield></datafield></record>", doc.to_s
|
16
16
|
end
|
17
17
|
|
18
18
|
def test_append_field
|
data/test/tc_xml.rb
CHANGED
@@ -32,6 +32,7 @@ class XMLTest < Test::Unit::TestCase
|
|
32
32
|
writer.close
|
33
33
|
|
34
34
|
xml = File.read('test/test.xml')
|
35
|
+
assert_match /<controlfield tag='007'>sdubumennmplu<\/controlfield>/, xml
|
35
36
|
assert_match /<\?xml-stylesheet type="text\/xsl" href="style.xsl"\?>/, xml
|
36
37
|
|
37
38
|
reader = MARC::XMLReader.new('test/test.xml')
|
metadata
CHANGED