oxm 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.markdown +3 -0
- data/VERSION +1 -1
- data/lib/oxm.rb +27 -5
- data/lib/oxm/object.rb +1 -1
- data/lib/oxm/sax_handler.rb +14 -1
- data/test/test_oxm.rb +58 -8
- metadata +13 -13
data/CHANGELOG.markdown
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/oxm.rb
CHANGED
@@ -3,14 +3,36 @@ require 'builder'
|
|
3
3
|
require 'oxm/sax_handler'
|
4
4
|
require 'oxm/object'
|
5
5
|
|
6
|
-
|
6
|
+
class OXM
|
7
7
|
# @param [IO/String] xml XML data
|
8
8
|
# @param [String] loop_element
|
9
9
|
# @return [Array] Array of OXM::Objects
|
10
10
|
def self.from_xml xml, loop_element, &block
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
oxm = OXM.new xml
|
12
|
+
oxm.from_xml loop_element, &block
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param [IO/String] xml XML data
|
16
|
+
def initialize xml
|
17
|
+
@xml = xml
|
18
|
+
end
|
19
|
+
|
20
|
+
# @param [String] loop_element
|
21
|
+
def from_xml loop_element, &block
|
22
|
+
@handler = OXM::SaxHandler.new(loop_element, &block)
|
23
|
+
parser = Nokogiri::XML::SAX::Parser.new(@handler)
|
24
|
+
parser.parse @xml
|
25
|
+
@handler.outputs unless block_given?
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return [Array] Error messages during the last from_xml call
|
29
|
+
def errors
|
30
|
+
@handler ? @handler.errors : []
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Array] Warning messages during the last from_xml call
|
34
|
+
def warnings
|
35
|
+
@handler ? @handler.warnings : []
|
15
36
|
end
|
16
37
|
end
|
38
|
+
|
data/lib/oxm/object.rb
CHANGED
data/lib/oxm/sax_handler.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
class OXM
|
2
2
|
class SaxHandler < Nokogiri::XML::SAX::Document
|
3
3
|
attr_reader :outputs
|
4
|
+
attr_reader :warnings
|
5
|
+
attr_reader :errors
|
4
6
|
|
5
7
|
def initialize loop_node, &block
|
6
8
|
@loop_node = loop_node
|
@@ -8,6 +10,17 @@ module OXM
|
|
8
10
|
@tags = []
|
9
11
|
@objects = []
|
10
12
|
@outputs = []
|
13
|
+
|
14
|
+
@warnings = []
|
15
|
+
@errors = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def warning str
|
19
|
+
@warnings << str
|
20
|
+
end
|
21
|
+
|
22
|
+
def error str
|
23
|
+
@errors << str
|
11
24
|
end
|
12
25
|
|
13
26
|
def start_element tag, attributes
|
data/test/test_oxm.rb
CHANGED
@@ -1,8 +1,56 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestOxm < Test::Unit::TestCase
|
4
|
+
require 'tempfile'
|
5
|
+
def test_errors
|
6
|
+
str = '
|
7
|
+
<?xml version="1.0"?>
|
8
|
+
<container attr1="1" attr2="2">
|
9
|
+
<item dept="A">
|
10
|
+
<volume unit="ml"><![CDATA[ 100 ]]></volume>
|
11
|
+
<volume unit="ml"><![CDATA[200]]></volume>
|
12
|
+
</item>
|
13
|
+
<item dept="B">
|
14
|
+
<volume unit="l">200</volume>
|
15
|
+
<volume unit="l">300</volume>
|
16
|
+
</item>
|
17
|
+
</container>
|
18
|
+
'
|
19
|
+
|
20
|
+
# Test errors (XML declaration allowed only at the start of the document)
|
21
|
+
oxm = OXM.new str
|
22
|
+
result = oxm.from_xml('container')
|
23
|
+
assert_equal 0, result.length
|
24
|
+
assert_equal 1, oxm.errors.length
|
25
|
+
assert_equal 0, oxm.warnings.length
|
26
|
+
|
27
|
+
# Test no-errors
|
28
|
+
oxm = OXM.new str.strip
|
29
|
+
result = oxm.from_xml('container')
|
30
|
+
assert_equal 1, result.length
|
31
|
+
assert_equal 0, oxm.errors.length
|
32
|
+
assert_equal 0, oxm.warnings.length
|
33
|
+
|
34
|
+
# Test empty IO
|
35
|
+
tf = Tempfile.new('oxm')
|
36
|
+
tf << str.strip
|
37
|
+
tf.flush
|
38
|
+
|
39
|
+
oxm = OXM.new File.open(tf.path)
|
40
|
+
result = oxm.from_xml('container')
|
41
|
+
assert_equal 1, result.length
|
42
|
+
assert_equal 0, oxm.errors.length
|
43
|
+
assert_equal 0, oxm.warnings.length
|
44
|
+
|
45
|
+
result = oxm.from_xml('container')
|
46
|
+
assert_equal 0, result.length
|
47
|
+
assert_equal 2, oxm.errors.length
|
48
|
+
assert_equal 0, oxm.warnings.length
|
49
|
+
end
|
50
|
+
|
4
51
|
def test_from_xml_string
|
5
|
-
str =
|
52
|
+
str = '
|
53
|
+
<?xml version="1.0"?>
|
6
54
|
<container attr1="1" attr2="2">
|
7
55
|
<item dept="A">
|
8
56
|
<volume unit="ml"><![CDATA[ 100 ]]></volume>
|
@@ -21,15 +69,17 @@ class TestOxm < Test::Unit::TestCase
|
|
21
69
|
<volume></volume>
|
22
70
|
</item>
|
23
71
|
</container>
|
24
|
-
|
72
|
+
'
|
73
|
+
|
74
|
+
# STRIP
|
75
|
+
str = str.strip
|
25
76
|
|
26
|
-
require 'tempfile'
|
27
77
|
tf = Tempfile.new('oxm')
|
28
78
|
tf << str
|
29
79
|
tf.flush
|
30
80
|
|
31
|
-
[str, File.open(tf.path)].each do |input|
|
32
|
-
result = OXM.from_xml(input, 'container')
|
81
|
+
[lambda { str }, lambda { File.open(tf.path) }].each do |input|
|
82
|
+
result = OXM.from_xml(input.call, 'container')
|
33
83
|
assert result.is_a?(Array)
|
34
84
|
assert_equal 1, result.length
|
35
85
|
assert_equal "", result.first.to_s
|
@@ -51,7 +101,7 @@ class TestOxm < Test::Unit::TestCase
|
|
51
101
|
assert container.compact!.equal?(container) # Object identity
|
52
102
|
assert container.item.is_a?(Array)
|
53
103
|
|
54
|
-
result = OXM.from_xml(
|
104
|
+
result = OXM.from_xml(input.call, 'container/item')
|
55
105
|
assert_equal 4, result.length
|
56
106
|
|
57
107
|
item = result.last
|
@@ -60,7 +110,7 @@ class TestOxm < Test::Unit::TestCase
|
|
60
110
|
assert_equal '500', item.volume.to_s
|
61
111
|
assert_equal 'L', item.volume['unit']
|
62
112
|
|
63
|
-
result = OXM.from_xml(
|
113
|
+
result = OXM.from_xml(input.call, 'container/item/volume')
|
64
114
|
assert_equal 8, result.length
|
65
115
|
assert_equal %w[100 200 200 300 300 400 500] + [""], result.map(&:to_s)
|
66
116
|
assert_equal %w[ml ml l l l ml L] + [nil], result.map { |e| e['unit'] }
|
@@ -99,7 +149,7 @@ class TestOxm < Test::Unit::TestCase
|
|
99
149
|
assert_equal '<volume unit="L"></volume>', vol.to_xml
|
100
150
|
|
101
151
|
cnt = 0
|
102
|
-
result = OXM.from_xml(
|
152
|
+
result = OXM.from_xml(input.call, 'container/item/volume') do |obj|
|
103
153
|
assert_equal 'volume', obj.tag
|
104
154
|
assert obj.is_a?(OXM::Object)
|
105
155
|
cnt += 1
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oxm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-28 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: builder
|
16
|
-
requirement: &
|
16
|
+
requirement: &2158741640 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2158741640
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: nokogiri
|
27
|
-
requirement: &
|
27
|
+
requirement: &2158740520 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.5.0
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2158740520
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: bundler
|
38
|
-
requirement: &
|
38
|
+
requirement: &2158738800 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2158738800
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: jeweler
|
49
|
-
requirement: &
|
49
|
+
requirement: &2158736100 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.6.4
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2158736100
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rcov
|
60
|
-
requirement: &
|
60
|
+
requirement: &2158735100 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2158735100
|
69
69
|
description: A tiny Object-XML-Mapper using Nokogiri SAX parser
|
70
70
|
email: junegunn.c@gmail.com
|
71
71
|
executables: []
|
@@ -102,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: '0'
|
103
103
|
segments:
|
104
104
|
- 0
|
105
|
-
hash:
|
105
|
+
hash: -1465942368572815623
|
106
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
107
|
none: false
|
108
108
|
requirements:
|