sax-machine 1.3.0 → 1.3.1

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: fbab8ba0a044c67b908e367534220284eb66be82
4
- data.tar.gz: fed3ba53ea5a6736e7ea66c7ec8165e619117e14
3
+ metadata.gz: 0917a0f1c569089b182a6517a6cb6706ddb3c42a
4
+ data.tar.gz: 0828562df1e97d795f0cbc506b7258beed5f7d23
5
5
  SHA512:
6
- metadata.gz: 893aea24b7264ba1306b010f117fb06583e0bc9d70c2d4f6fa87ca955048276ae1f1cfbb1d237f9e06c3f391a799ded075048de6b5b0494efe3e5ae9e88b060b
7
- data.tar.gz: 90348f313389a5a7d9d058624e81bc13b7833968cb94ca1442b21b29cdffa65ae438265f365da9623c4cc4f65664006fac7c4b6832486dffe85476d8b27078aa
6
+ metadata.gz: 81eff065ec6ec812f7b8ff6c5e48ed4f48490e17a2a199a0f3980d1578863aab9a1bdba28be58b03338aaa7127e55632a7792354dcc57c9f980760b9ec402a51
7
+ data.tar.gz: b7e005d33a7765814a1188e10f6be96d5aee196df56a5913fbc94b0a5624b9d70f0f5a2270417ce1bcc8c736aec836c3227bdd0b96d569bdb8206e6c421408d8
data/HISTORY.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # HEAD
2
2
 
3
+ # 1.3.1
4
+
5
+ * Allow default value to be `false` [[#66](https://github.com/pauldix/sax-machine/pull/66)]
6
+ * Support adding class to an attribute [[#68](https://github.com/pauldix/sax-machine/pull/68)]
7
+ * Adjust Ox handler to skip empty text/cdata values
8
+
3
9
  # 1.3.0
4
10
 
5
11
  * Improve block modifiers to support all config options
@@ -1,13 +1,14 @@
1
1
  module SAXMachine
2
2
  class SAXConfig
3
3
  class ElementValueConfig
4
- attr_reader :name, :setter
4
+ attr_reader :name, :setter, :data_class
5
5
 
6
6
  def initialize(name, options)
7
7
  @name = name.to_s
8
8
  @as = options[:as]
9
9
  @setter = "#{@as}="
10
10
  @required = options[:required]
11
+ @data_class = options[:class]
11
12
  end
12
13
 
13
14
  def column
@@ -107,26 +107,8 @@ module SAXMachine
107
107
 
108
108
  object.send(config.accessor) << element
109
109
  else
110
- value =
111
- case config.data_class.to_s
112
- when "String" then value != NO_BUFFER ? value.to_s : value
113
- when "Integer" then value != NO_BUFFER ? value.to_i : value
114
- when "Float" then value != NO_BUFFER ? value.to_f : value
115
- when "Symbol" then
116
- if value != NO_BUFFER
117
- value.to_s.empty? ? nil : value.to_s.downcase.to_sym
118
- else
119
- value
120
- end
121
- # Assumes that time elements will be string-based and are not
122
- # something else, e.g. seconds since epoch
123
- when "Time" then value != NO_BUFFER ? Time.parse(value.to_s) : value
124
- when "" then value
125
- else
126
- element
127
- end
128
-
129
- object.send(config.setter, value) unless value == NO_BUFFER
110
+ value = data_class_value(config.data_class, value) || element
111
+ object.send(config.setter, value) if value != NO_BUFFER
130
112
  mark_as_parsed(object, config)
131
113
  end
132
114
 
@@ -186,8 +168,27 @@ module SAXMachine
186
168
 
187
169
  if config
188
170
  config.attribute_configs_for_element(attributes).each do |ac|
189
- object.send(ac.setter, ac.value_from_attrs(attributes))
171
+ value = data_class_value(ac.data_class, ac.value_from_attrs(attributes))
172
+ object.send(ac.setter, value)
173
+ end
174
+ end
175
+ end
176
+
177
+ def data_class_value(data_class, value)
178
+ case data_class.to_s
179
+ when "String" then value != NO_BUFFER ? value.to_s : value
180
+ when "Integer" then value != NO_BUFFER ? value.to_i : value
181
+ when "Float" then value != NO_BUFFER ? value.to_s.gsub(",",".").to_f : value
182
+ when "Symbol" then
183
+ if value != NO_BUFFER
184
+ value.to_s.empty? ? nil : value.to_s.downcase.to_sym
185
+ else
186
+ value
190
187
  end
188
+ # Assumes that time elements will be string-based and are not
189
+ # something else, e.g. seconds since epoch
190
+ when "Time" then value != NO_BUFFER ? Time.parse(value.to_s) : value
191
+ when "" then value
191
192
  end
192
193
  end
193
194
 
@@ -34,12 +34,16 @@ module SAXMachine
34
34
  @element = name
35
35
  end
36
36
 
37
+ def text(value)
38
+ _characters(value) if value && !value.empty?
39
+ end
40
+
41
+ alias_method :cdata, :text
42
+
37
43
  def error(message, line, column)
38
44
  _error("#{message} on line #{line} column #{column}")
39
45
  end
40
46
 
41
- alias_method :text, :_characters
42
- alias_method :cdata, :_characters
43
47
  alias_method :end_element, :_end_element
44
48
 
45
49
  private
@@ -21,7 +21,7 @@ module SAXMachine
21
21
 
22
22
  self.class.sax_config.top_level_elements.each do |_, configs|
23
23
  configs.each do |config|
24
- next unless config.default
24
+ next if config.default.nil?
25
25
  next unless send(config.as).nil?
26
26
 
27
27
  send(config.setter, config.default)
@@ -1,3 +1,3 @@
1
1
  module SAXMachine
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
@@ -143,54 +143,154 @@ describe "SAXMachine" do
143
143
  expect(@klass.data_class(:date)).to eq(DateTime)
144
144
  end
145
145
 
146
- it "handles an integer class" do
147
- @klass = Class.new do
148
- include SAXMachine
149
- element :number, class: Integer
146
+ describe "string" do
147
+ before do
148
+ class TestString
149
+ include SAXMachine
150
+ element :number, class: String
151
+ end
152
+
153
+ class TestStringAttribute
154
+ include SAXMachine
155
+ attribute :sub_number, class: String
156
+ end
157
+
158
+ class TestStringWithAttribute
159
+ include SAXMachine
160
+ element :number, class: TestStringAttribute
161
+ end
162
+ end
163
+
164
+ it "is handled in an element" do
165
+ document = TestString.parse("<number>5.5</number>")
166
+ expect(document.number).to eq("5.5")
150
167
  end
151
168
 
152
- document = @klass.parse("<number>5</number>")
153
- expect(document.number).to eq(5)
169
+ it "is handled in an attribute" do
170
+ document = TestStringWithAttribute.parse("<number sub_number='5.5'></number>")
171
+ expect(document.number.sub_number).to eq("5.5")
172
+ end
154
173
  end
155
174
 
156
- it "handles an float class" do
157
- @klass = Class.new do
158
- include SAXMachine
159
- element :number, class: Float
175
+ describe "integer" do
176
+ before do
177
+ class TestInteger
178
+ include SAXMachine
179
+ element :number, class: Integer
180
+ end
181
+
182
+ class TestIntegerAttribute
183
+ include SAXMachine
184
+ attribute :sub_number, class: Integer
185
+ end
186
+
187
+ class TestIntegerWithAttribute
188
+ include SAXMachine
189
+ element :number, class: TestIntegerAttribute
190
+ end
160
191
  end
161
192
 
162
- document = @klass.parse("<number>5.5</number>")
163
- expect(document.number).to eq(5.5)
193
+ it "is handled in an element" do
194
+ document = TestInteger.parse("<number>5</number>")
195
+ expect(document.number).to eq(5)
196
+ end
197
+
198
+ it "is handled in an attribute" do
199
+ document = TestIntegerWithAttribute.parse("<number sub_number='5'></number>")
200
+ expect(document.number.sub_number).to eq(5)
201
+ end
164
202
  end
165
203
 
166
- it "handles an string class" do
167
- @klass = Class.new do
168
- include SAXMachine
169
- element :number, class: String
204
+ describe "float" do
205
+ before do
206
+ class TestFloat
207
+ include SAXMachine
208
+ element :number, class: Float
209
+ end
210
+
211
+ class TestFloatAttribute
212
+ include SAXMachine
213
+ attribute :sub_number, class: Float
214
+ end
215
+
216
+ class TestFloatWithAttribute
217
+ include SAXMachine
218
+ element :number, class: TestFloatAttribute
219
+ end
220
+ end
221
+
222
+ it "is handled in an element with '.' delimiter" do
223
+ document = TestFloat.parse("<number>5.5</number>")
224
+ expect(document.number).to eq(5.5)
225
+ end
226
+
227
+ it "is handled in an element with ',' delimiter" do
228
+ document = TestFloat.parse("<number>5,5</number>")
229
+ expect(document.number).to eq(5.5)
170
230
  end
171
231
 
172
- document = @klass.parse("<number>5.5</number>")
173
- expect(document.number).to eq("5.5")
232
+ it "is handled in an attribute" do
233
+ document = TestFloatWithAttribute.parse("<number sub_number='5.5'>5.5</number>")
234
+ expect(document.number.sub_number).to eq(5.5)
235
+ end
174
236
  end
175
237
 
176
- it "handles a time class" do
177
- @klass = Class.new do
178
- include SAXMachine
179
- element :time, class: Time
238
+ describe "symbol" do
239
+ before do
240
+ class TestSymbol
241
+ include SAXMachine
242
+ element :symbol, class: Symbol
243
+ end
244
+
245
+ class TestSymbolAttribute
246
+ include SAXMachine
247
+ attribute :sub_symbol, class: Symbol
248
+ end
249
+
250
+ class TestSymbolWithAttribute
251
+ include SAXMachine
252
+ element :symbol, class: TestSymbolAttribute
253
+ end
180
254
  end
181
255
 
182
- document = @klass.parse("<time>1994-02-04T06:20:00Z</time>")
183
- expect(document.time).to eq(Time.utc(1994, 2, 4, 6, 20, 0, 0))
256
+ it "is handled in an element" do
257
+ document = TestSymbol.parse("<symbol>MY_SYMBOL_VALUE</symbol>")
258
+ expect(document.symbol).to eq(:my_symbol_value)
259
+ end
260
+
261
+ it "is handled in an attribute" do
262
+ document = TestSymbolWithAttribute.parse("<symbol sub_symbol='MY_SYMBOL_VALUE'></symbol>")
263
+ expect(document.symbol.sub_symbol).to eq(:my_symbol_value)
264
+ end
184
265
  end
185
266
 
186
- it "handles a Symbol class" do
187
- @klass = Class.new do
188
- include SAXMachine
189
- element :symbol, class: Symbol
267
+ describe "time" do
268
+ before do
269
+ class TestTime
270
+ include SAXMachine
271
+ element :time, class: Time
272
+ end
273
+
274
+ class TestTimeAttribute
275
+ include SAXMachine
276
+ attribute :sub_time, class: Time
277
+ end
278
+
279
+ class TestTimeWithAttribute
280
+ include SAXMachine
281
+ element :time, class: TestTimeAttribute
282
+ end
283
+ end
284
+
285
+ it "is handled in an element" do
286
+ document = TestTime.parse("<time>1994-02-04T06:20:00Z</time>")
287
+ expect(document.time).to eq(Time.utc(1994, 2, 4, 6, 20, 0, 0))
190
288
  end
191
289
 
192
- document = @klass.parse("<symbol>MY_SYMBOL_VALUE</symbol>")
193
- expect(document.symbol).to eq(:my_symbol_value)
290
+ it "is handled in an attribute" do
291
+ document = TestTimeWithAttribute.parse("<time sub_time='1994-02-04T06:20:00Z'>1994-02-04T06:20:00Z</time>")
292
+ expect(document.time.sub_time).to eq(Time.utc(1994, 2, 4, 6, 20, 0, 0))
293
+ end
194
294
  end
195
295
  end
196
296
 
@@ -207,6 +307,22 @@ describe "SAXMachine" do
207
307
  document = @klass.parse("<number></number>")
208
308
  expect(document.number).to eq(0)
209
309
  end
310
+
311
+ it "can be a Boolean" do
312
+ @klass = Class.new do
313
+ include SAXMachine
314
+ element(:bool, default: false) { |v| !!v }
315
+ end
316
+
317
+ document = @klass.parse("<no>bool</no>")
318
+ expect(document.bool).to be false
319
+
320
+ document = @klass.parse("<bool></bool>")
321
+ expect(document.bool).to be false
322
+
323
+ document = @klass.parse("<bool>1</bool>")
324
+ expect(document.bool).to be true
325
+ end
210
326
  end
211
327
 
212
328
  describe "the required attribute" do
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.3.0
4
+ version: 1.3.1
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: 2015-01-13 00:00:00.000000000 Z
14
+ date: 2015-03-22 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rspec