marc 0.1.0 → 0.1.2
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 +4 -4
- data/lib/marc/xmlreader.rb +22 -3
- data/test/tc_record.rb +7 -0
- data/test/tc_xml.rb +9 -1
- metadata +2 -2
data/lib/marc/record.rb
CHANGED
|
@@ -82,13 +82,13 @@ module MARC
|
|
|
82
82
|
end
|
|
83
83
|
|
|
84
84
|
# Handy method for returning the MARCXML serialization for a
|
|
85
|
-
# MARC::Record object.
|
|
86
|
-
# MARC::
|
|
85
|
+
# MARC::Record object. You'll get back a REXML::Document object.
|
|
86
|
+
# Really this is just a wrapper around MARC::XMLWriter::encode
|
|
87
87
|
#
|
|
88
|
-
#
|
|
88
|
+
# xml_doc = record.to_xml()
|
|
89
89
|
|
|
90
90
|
def to_xml
|
|
91
|
-
return MARC::
|
|
91
|
+
return MARC::XMLWriter.encode(self)
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
# Returns a string version of the record, suitable for printing
|
data/lib/marc/xmlreader.rb
CHANGED
|
@@ -6,9 +6,28 @@ module MARC
|
|
|
6
6
|
class XMLReader
|
|
7
7
|
include Enumerable
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
# the constructor which you can pass either a filename:
|
|
10
|
+
#
|
|
11
|
+
# reader = MARC::XMLReader.new('/Users/edsu/marc.dat')
|
|
12
|
+
#
|
|
13
|
+
# or a File object,
|
|
14
|
+
#
|
|
15
|
+
# reader = Marc::XMLReader.new(File.new('/Users/edsu/marc.dat'))
|
|
16
|
+
#
|
|
17
|
+
# or really any object that responds to read(n)
|
|
18
|
+
#
|
|
19
|
+
# reader = MARC::XMLReader.new(StringIO.new(xml))
|
|
20
|
+
|
|
21
|
+
def initialize(file)
|
|
22
|
+
if file.class == String:
|
|
23
|
+
handle = File.new(file)
|
|
24
|
+
elsif file.respond_to?("read", 5)
|
|
25
|
+
handle = file
|
|
26
|
+
else
|
|
27
|
+
throw "must pass in path or File"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
@parser = REXML::Parsers::PullParser.new(handle)
|
|
12
31
|
end
|
|
13
32
|
|
|
14
33
|
def each
|
data/test/tc_record.rb
CHANGED
|
@@ -8,6 +8,13 @@ class TestRecord < Test::Unit::TestCase
|
|
|
8
8
|
assert_equal(r.class, MARC::Record)
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
+
def test_xml
|
|
12
|
+
r = get_record()
|
|
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
|
|
16
|
+
end
|
|
17
|
+
|
|
11
18
|
def test_append_field
|
|
12
19
|
r = get_record()
|
|
13
20
|
assert_equal(r.fields.length(), 2)
|
data/test/tc_xml.rb
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
require 'test/unit'
|
|
2
2
|
require 'marc'
|
|
3
|
+
require 'stringio'
|
|
3
4
|
|
|
4
5
|
class XMLTest < Test::Unit::TestCase
|
|
5
6
|
|
|
6
|
-
def
|
|
7
|
+
def test_batch
|
|
7
8
|
reader = MARC::XMLReader.new('test/batch.xml')
|
|
8
9
|
count = 0
|
|
9
10
|
for record in reader
|
|
@@ -13,6 +14,12 @@ class XMLTest < Test::Unit::TestCase
|
|
|
13
14
|
assert_equal(count, 2)
|
|
14
15
|
end
|
|
15
16
|
|
|
17
|
+
def test_read_string
|
|
18
|
+
xml = File.new('test/batch.xml').read
|
|
19
|
+
reader = MARC::XMLReader.new(StringIO.new(xml))
|
|
20
|
+
assert_equal reader.entries.length, 2
|
|
21
|
+
end
|
|
22
|
+
|
|
16
23
|
def test_read_write
|
|
17
24
|
record1 = MARC::Record.new
|
|
18
25
|
record1.leader = '00925njm 22002777a 4500'
|
|
@@ -33,5 +40,6 @@ class XMLTest < Test::Unit::TestCase
|
|
|
33
40
|
|
|
34
41
|
File.unlink('test/test.xml')
|
|
35
42
|
end
|
|
43
|
+
|
|
36
44
|
end
|
|
37
45
|
|
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: 2006-12-
|
|
6
|
+
version: 0.1.2
|
|
7
|
+
date: 2006-12-21 00:00:00 -05:00
|
|
8
8
|
summary: A ruby library for working with Machine Readable Cataloging
|
|
9
9
|
require_paths:
|
|
10
10
|
- lib
|