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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d70f53b72ae5d965a51b002434716f68b7d6dd80
4
- data.tar.gz: a96da94d49a82ace8f74778aae5fe7b1e7ff2067
3
+ metadata.gz: fbab8ba0a044c67b908e367534220284eb66be82
4
+ data.tar.gz: fed3ba53ea5a6736e7ea66c7ec8165e619117e14
5
5
  SHA512:
6
- metadata.gz: 618645fdf96799bf57df46b97c1e0244a933137085f1162c72735822b79a38c7b8c4fe7e3e73c7aba95611f36bc84bb91199af3e294faa924c18edc5b7fe0228
7
- data.tar.gz: 7c0700b3c34bcd6812294cc67dc3571a35f4b4957a9aa2cef485aa15ab56292041ec1df056ebe861a5410cdcdb666041d85167aa00da43a45e92e0eb25bfbe6e
6
+ metadata.gz: 893aea24b7264ba1306b010f117fb06583e0bc9d70c2d4f6fa87ca955048276ae1f1cfbb1d237f9e06c3f391a799ded075048de6b5b0494efe3e5ae9e88b060b
7
+ data.tar.gz: 90348f313389a5a7d9d058624e81bc13b7833968cb94ca1442b21b29cdffa65ae438265f365da9623c4cc4f65664006fac7c4b6832486dffe85476d8b27078aa
@@ -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
@@ -81,6 +81,8 @@ end
81
81
  class Atom
82
82
  include SAXMachine
83
83
  # Use block to modify the returned value
84
+ # Blocks are working with pretty much everything,
85
+ # except for `elements` with `class` attribute
84
86
  element :title do |title|
85
87
  title.strip
86
88
  end
@@ -12,7 +12,7 @@ module SAXMachine
12
12
  end
13
13
  end
14
14
 
15
- def sax_parse(xml_text)
15
+ def sax_parse(xml_input)
16
16
  raise NotImplementedError
17
17
  end
18
18
 
@@ -5,9 +5,9 @@ module SAXMachine
5
5
  class SAXNokogiriHandler < Nokogiri::XML::SAX::Document
6
6
  include SAXAbstractHandler
7
7
 
8
- def sax_parse(xml_text)
8
+ def sax_parse(xml_input)
9
9
  parser = Nokogiri::XML::SAX::Parser.new(self)
10
- parser.parse(xml_text) do |ctx|
10
+ parser.parse(xml_input) do |ctx|
11
11
  ctx.replace_entities = true
12
12
  end
13
13
  end
@@ -9,8 +9,8 @@ module SAXMachine
9
9
  _initialize(*args)
10
10
  end
11
11
 
12
- def sax_parse(xml_text)
13
- Oga.sax_parse_xml(self, xml_text)
12
+ def sax_parse(xml_input)
13
+ Oga.sax_parse_xml(self, xml_input)
14
14
  end
15
15
 
16
16
  def on_element(namespace, name, attrs)
@@ -10,8 +10,11 @@ module SAXMachine
10
10
  _reset_element
11
11
  end
12
12
 
13
- def sax_parse(xml_text)
14
- Ox.sax_parse(self, StringIO.new(xml_text),
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 |xml_text|
18
- extended_clazz.parse(xml_text)
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(xml_text, on_error = nil, on_warning = nil)
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(xml_text)
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
- instance_variable_set("@#{real_name}", (yield value))
129
+ instance_variable_set("@#{real_name}", instance_exec(value, &block))
125
130
  end
126
131
  else
127
132
  attr_writer(real_name)
@@ -1,3 +1,3 @@
1
1
  module SAXMachine
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -221,36 +221,68 @@ describe "SAXMachine" do
221
221
 
222
222
  describe "the block" do
223
223
  before do
224
- @klass = Class.new do
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 :link, value: :foo do |link|
236
- link.downcase
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 = @klass.parse("<title>SAX</title>")
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 = @klass.parse("<title id='345'>SAX</title>")
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 attribute value" do
252
- document = @klass.parse("<link foo='tEst'>hello</link>")
253
- expect(document.link).to eq("test")
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.2.0
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: 2014-12-23 00:00:00.000000000 Z
14
+ date: 2015-01-13 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec