marc 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/marc/record.rb CHANGED
@@ -88,7 +88,7 @@ module MARC
88
88
  # xml_doc = record.to_xml()
89
89
 
90
90
  def to_xml
91
- return MARC::XMLWriter.encode(self)
91
+ return MARC::XMLWriter.encode(self, :include_namespace => true)
92
92
  end
93
93
 
94
94
  # Handy method for returning a hash mapping this records values
@@ -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(/[\da-z ]{1}/)
75
- ctrlFieldTag = Regexp.new(/00[1-9A-Za-z]{1}s/)
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
- root = "<record/>"
90
- doc = REXML::Document.new(root)
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
- doc.root.add_element(leader)
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
- doc.root.add_element datafield_elem
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
- if (field.tag.match(ctrlFieldTag) == nil)
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
- doc.root.add_element(control_element)
158
+ e.add_element(control_element)
159
159
  end
160
160
  end
161
161
 
162
162
  # return xml
163
- return doc
163
+ return e
164
164
  end
165
165
  end
166
166
  end
@@ -3,23 +3,23 @@ require 'marc'
3
3
 
4
4
  class TestField < Test::Unit::TestCase
5
5
 
6
- def test_control
7
- control = MARC::ControlField.new('005', 'foobarbaz')
8
- assert_equal(control.to_s, '005 foobarbaz')
9
- end
6
+ def test_control
7
+ control = MARC::ControlField.new('005', 'foobarbaz')
8
+ assert_equal(control.to_s, '005 foobarbaz')
9
+ end
10
10
 
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')
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
- 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')
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::Document, doc
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
@@ -3,7 +3,7 @@ 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.3
6
+ version: 0.1.4
7
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: