sax-machine 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/HISTORY.md +6 -0
- data/README.md +2 -0
- data/lib/sax-machine/handlers/sax_abstract_handler.rb +1 -1
- data/lib/sax-machine/handlers/sax_nokogiri_handler.rb +2 -2
- data/lib/sax-machine/handlers/sax_oga_handler.rb +2 -2
- data/lib/sax-machine/handlers/sax_ox_handler.rb +5 -2
- data/lib/sax-machine/sax_configure.rb +2 -2
- data/lib/sax-machine/sax_document.rb +37 -32
- data/lib/sax-machine/version.rb +1 -1
- data/spec/sax-machine/sax_document_spec.rb +57 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fbab8ba0a044c67b908e367534220284eb66be82
|
4
|
+
data.tar.gz: fed3ba53ea5a6736e7ea66c7ec8165e619117e14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 893aea24b7264ba1306b010f117fb06583e0bc9d70c2d4f6fa87ca955048276ae1f1cfbb1d237f9e06c3f391a799ded075048de6b5b0494efe3e5ae9e88b060b
|
7
|
+
data.tar.gz: 90348f313389a5a7d9d058624e81bc13b7833968cb94ca1442b21b29cdffa65ae438265f365da9623c4cc4f65664006fac7c4b6832486dffe85476d8b27078aa
|
data/.travis.yml
CHANGED
@@ -4,6 +4,7 @@ rvm:
|
|
4
4
|
- 1.9.3
|
5
5
|
- 2.0
|
6
6
|
- 2.1
|
7
|
+
- 2.2
|
7
8
|
- jruby-1.7
|
8
9
|
- rbx-2
|
9
10
|
- ruby-head
|
@@ -24,6 +25,8 @@ matrix:
|
|
24
25
|
- env: HANDLER="ox"
|
25
26
|
rvm: jruby-head
|
26
27
|
allow_failures:
|
28
|
+
- env: HANDLER="oga"
|
29
|
+
rvm: jruby-1.7
|
27
30
|
- rvm: rbx-2
|
28
31
|
- rvm: ruby-head
|
29
32
|
- rvm: jruby-head
|
data/HISTORY.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# HEAD
|
2
2
|
|
3
|
+
# 1.3.0
|
4
|
+
|
5
|
+
* Improve block modifiers to support all config options
|
6
|
+
* Make block modifiers run in instance context
|
7
|
+
* Make all handlers support IO as a input
|
8
|
+
|
3
9
|
# 1.2.0
|
4
10
|
|
5
11
|
* Add support for blocks as value modifiers [[#61](https://github.com/pauldix/sax-machine/pull/61)]
|
data/README.md
CHANGED
@@ -5,9 +5,9 @@ module SAXMachine
|
|
5
5
|
class SAXNokogiriHandler < Nokogiri::XML::SAX::Document
|
6
6
|
include SAXAbstractHandler
|
7
7
|
|
8
|
-
def sax_parse(
|
8
|
+
def sax_parse(xml_input)
|
9
9
|
parser = Nokogiri::XML::SAX::Parser.new(self)
|
10
|
-
parser.parse(
|
10
|
+
parser.parse(xml_input) do |ctx|
|
11
11
|
ctx.replace_entities = true
|
12
12
|
end
|
13
13
|
end
|
@@ -10,8 +10,11 @@ module SAXMachine
|
|
10
10
|
_reset_element
|
11
11
|
end
|
12
12
|
|
13
|
-
def sax_parse(
|
14
|
-
Ox
|
13
|
+
def sax_parse(xml_input)
|
14
|
+
# Ox requires input to be streamable
|
15
|
+
xml_input = StringIO.new(xml_input) if xml_input.is_a?(String)
|
16
|
+
|
17
|
+
Ox.sax_parse(self, xml_input,
|
15
18
|
symbolize: false,
|
16
19
|
convert_special: true,
|
17
20
|
skip: :skip_return,
|
@@ -14,8 +14,8 @@ module SAXMachine
|
|
14
14
|
clazz.extend LightWeightSaxMachine
|
15
15
|
clazz.sax_config = extended_clazz.sax_config
|
16
16
|
|
17
|
-
(class << clazz;self;end).send(:define_method, :parse) do |
|
18
|
-
extended_clazz.parse(
|
17
|
+
(class << clazz;self;end).send(:define_method, :parse) do |xml_input|
|
18
|
+
extended_clazz.parse(xml_input)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -4,11 +4,11 @@ module SAXMachine
|
|
4
4
|
base.extend(ClassMethods)
|
5
5
|
end
|
6
6
|
|
7
|
-
def parse(
|
7
|
+
def parse(xml_input, on_error = nil, on_warning = nil)
|
8
8
|
handler_klass = SAXMachine.const_get("SAX#{SAXMachine.handler.capitalize}Handler")
|
9
9
|
|
10
10
|
handler = handler_klass.new(self, on_error, on_warning)
|
11
|
-
handler.sax_parse(
|
11
|
+
handler.sax_parse(xml_input)
|
12
12
|
|
13
13
|
self
|
14
14
|
end
|
@@ -51,16 +51,46 @@ module SAXMachine
|
|
51
51
|
create_attr(real_name, &block)
|
52
52
|
end
|
53
53
|
|
54
|
-
def value(name, options = {})
|
54
|
+
def value(name, options = {}, &block)
|
55
55
|
real_name = (options[:as] ||= name).to_s
|
56
56
|
sax_config.add_top_level_element_value(self.class.to_s, options.merge(name: name))
|
57
|
-
create_attr(real_name)
|
57
|
+
create_attr(real_name, &block)
|
58
58
|
end
|
59
59
|
|
60
|
-
def ancestor(name, options = {})
|
60
|
+
def ancestor(name, options = {}, &block)
|
61
61
|
real_name = (options[:as] ||= name).to_s
|
62
62
|
sax_config.add_ancestor(name, options)
|
63
|
-
create_attr(real_name)
|
63
|
+
create_attr(real_name, &block)
|
64
|
+
end
|
65
|
+
|
66
|
+
def elements(name, options = {}, &block)
|
67
|
+
real_name = (options[:as] ||= name).to_s
|
68
|
+
|
69
|
+
if options[:class]
|
70
|
+
sax_config.add_collection_element(name, options)
|
71
|
+
else
|
72
|
+
if block_given?
|
73
|
+
define_method("add_#{real_name}") do |value|
|
74
|
+
send(real_name).send(:<<, instance_exec(value, &block))
|
75
|
+
end
|
76
|
+
else
|
77
|
+
define_method("add_#{real_name}") do |value|
|
78
|
+
send(real_name).send(:<<, value)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
sax_config.add_top_level_element(name, options.merge(collection: true))
|
83
|
+
end
|
84
|
+
|
85
|
+
if !method_defined?(real_name)
|
86
|
+
class_eval <<-SRC
|
87
|
+
def #{real_name}
|
88
|
+
@#{real_name} ||= []
|
89
|
+
end
|
90
|
+
SRC
|
91
|
+
end
|
92
|
+
|
93
|
+
attr_writer(options[:as]) unless method_defined?("#{options[:as]}=")
|
64
94
|
end
|
65
95
|
|
66
96
|
def columns
|
@@ -83,31 +113,6 @@ module SAXMachine
|
|
83
113
|
columns.map { |e| e.column }
|
84
114
|
end
|
85
115
|
|
86
|
-
def elements(name, options = {})
|
87
|
-
options[:as] ||= name
|
88
|
-
|
89
|
-
if options[:class]
|
90
|
-
sax_config.add_collection_element(name, options)
|
91
|
-
else
|
92
|
-
class_eval <<-SRC
|
93
|
-
def add_#{options[:as]}(value)
|
94
|
-
#{options[:as]} << value
|
95
|
-
end
|
96
|
-
SRC
|
97
|
-
sax_config.add_top_level_element(name, options.merge(collection: true))
|
98
|
-
end
|
99
|
-
|
100
|
-
if !method_defined?(options[:as].to_s)
|
101
|
-
class_eval <<-SRC
|
102
|
-
def #{options[:as]}
|
103
|
-
@#{options[:as]} ||= []
|
104
|
-
end
|
105
|
-
SRC
|
106
|
-
end
|
107
|
-
|
108
|
-
attr_writer(options[:as]) unless method_defined?("#{options[:as]}=")
|
109
|
-
end
|
110
|
-
|
111
116
|
def sax_config
|
112
117
|
@sax_config ||= SAXConfig.new
|
113
118
|
end
|
@@ -121,7 +126,7 @@ module SAXMachine
|
|
121
126
|
if !method_defined?("#{real_name}=")
|
122
127
|
if block_given?
|
123
128
|
define_method("#{real_name}=") do |value|
|
124
|
-
|
129
|
+
instance_variable_set("@#{real_name}", instance_exec(value, &block))
|
125
130
|
end
|
126
131
|
else
|
127
132
|
attr_writer(real_name)
|
data/lib/sax-machine/version.rb
CHANGED
@@ -221,36 +221,68 @@ describe "SAXMachine" do
|
|
221
221
|
|
222
222
|
describe "the block" do
|
223
223
|
before do
|
224
|
-
|
224
|
+
class ElementBlockParser
|
225
|
+
include SAXMachine
|
226
|
+
|
227
|
+
ancestor :parent do |parent|
|
228
|
+
parent.class.to_s
|
229
|
+
end
|
230
|
+
|
231
|
+
value :text do |text|
|
232
|
+
text.downcase
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
class BlockParser
|
225
237
|
include SAXMachine
|
226
238
|
|
227
239
|
element :title do |title|
|
228
240
|
"#{title}!!!"
|
229
241
|
end
|
230
242
|
|
243
|
+
element :scope do |scope|
|
244
|
+
"#{title} #{scope}"
|
245
|
+
end
|
246
|
+
|
231
247
|
attribute :id do |id|
|
232
248
|
id.to_i
|
233
249
|
end
|
234
250
|
|
235
|
-
element :
|
236
|
-
|
251
|
+
element :nested, class: ElementBlockParser
|
252
|
+
elements :message, as: :messages do |message|
|
253
|
+
"#{message}!"
|
237
254
|
end
|
238
255
|
end
|
239
256
|
end
|
240
257
|
|
258
|
+
it "has instance as a block context" do
|
259
|
+
document = BlockParser.parse("<root><title>SAX</title><scope>something</scope></root>")
|
260
|
+
expect(document.scope).to eq("SAX!!! something")
|
261
|
+
end
|
262
|
+
|
241
263
|
it "uses block for element" do
|
242
|
-
document =
|
264
|
+
document = BlockParser.parse("<title>SAX</title>")
|
243
265
|
expect(document.title).to eq("SAX!!!")
|
244
266
|
end
|
245
267
|
|
246
268
|
it 'uses block for attribute' do
|
247
|
-
document =
|
269
|
+
document = BlockParser.parse("<title id='345'>SAX</title>")
|
248
270
|
expect(document.id).to eq(345)
|
249
271
|
end
|
250
272
|
|
251
|
-
it "uses block for
|
252
|
-
document =
|
253
|
-
expect(document.
|
273
|
+
it "uses block for value" do
|
274
|
+
document = BlockParser.parse("<title><nested>tEst</nested></title>")
|
275
|
+
expect(document.nested.text).to eq("test")
|
276
|
+
end
|
277
|
+
|
278
|
+
it "uses block for ancestor" do
|
279
|
+
document = BlockParser.parse("<title><nested>SAX</nested></title>")
|
280
|
+
expect(document.nested.parent).to eq("BlockParser")
|
281
|
+
end
|
282
|
+
|
283
|
+
it "uses block for elements" do
|
284
|
+
document = BlockParser.parse("<title><message>hi</message><message>world</message></title>")
|
285
|
+
expect(document.messages).to eq(["hi!", "world!"])
|
254
286
|
end
|
255
287
|
end
|
256
288
|
end
|
@@ -1040,4 +1072,21 @@ describe "SAXMachine" do
|
|
1040
1072
|
expect(@warnings.uniq.size).to eq(0)
|
1041
1073
|
end
|
1042
1074
|
end
|
1075
|
+
|
1076
|
+
describe "with io as a input" do
|
1077
|
+
before do
|
1078
|
+
@io = StringIO.new('<item id="1"><title>sweet</title></item>')
|
1079
|
+
|
1080
|
+
class IoParser
|
1081
|
+
include SAXMachine
|
1082
|
+
element :title
|
1083
|
+
end
|
1084
|
+
|
1085
|
+
@item = ItemElement5.parse(@io)
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
it "parses" do
|
1089
|
+
expect(@item.title).to eq("sweet")
|
1090
|
+
end
|
1091
|
+
end
|
1043
1092
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sax-machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Dix
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2015-01-13 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rspec
|