pump 0.5.0 → 0.5.1
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.
- checksums.yaml +4 -4
- data/lib/pump/version.rb +1 -1
- data/lib/pump/xml/tag.rb +8 -1
- data/lib/pump/xml/tag_array.rb +10 -1
- data/spec/pump/xml_spec.rb +68 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9047d695a9bb5dbbec84d04fc9a77bebb43c35f8
|
|
4
|
+
data.tar.gz: eeed27e8e5f98a30fa22271af1e6182cc3178bcd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6c696f69ee33b842a90e2bd2790a802415497a29aed0d274331bdefc7d54bc5c975b9e6cb6cfd53e161f95a37447777ceefb51c58b1a65df83e64829e755e0ab
|
|
7
|
+
data.tar.gz: f020baf37e32686173e0334193fbd555c93966a3b8924f3ecaa0dd0127a3dbc8c484dba40217ba11a7d682b6bdb4cd27a238bff04cd460dab16708e18443223c
|
data/lib/pump/version.rb
CHANGED
data/lib/pump/xml/tag.rb
CHANGED
|
@@ -13,7 +13,9 @@ module Pump
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def to_s
|
|
16
|
-
if
|
|
16
|
+
if options.has_key?(:static_value)
|
|
17
|
+
"#{condition_start}#{open_tag}#{static_value_and_close_tag}#{condition_end}"
|
|
18
|
+
elsif !value_nodes? || options[:never_nil]
|
|
17
19
|
"#{condition_start}#{open_tag}#{value_and_close_tag}#{condition_end}"
|
|
18
20
|
else
|
|
19
21
|
"#{condition_start}#{open_tag}\#{v = #{nodes.first.plain};''}\#{#{value_and_close_tag_with_nil_check}}#{condition_end}"
|
|
@@ -47,6 +49,11 @@ module Pump
|
|
|
47
49
|
"v.nil? ? \" nil=\\\"true\\\"/>\n\" : \"#{value_and_close_tag('v')}\""
|
|
48
50
|
end
|
|
49
51
|
|
|
52
|
+
def static_value_and_close_tag
|
|
53
|
+
return " nil=\\\"true\\\"/>\n" if options[:static_value].nil?
|
|
54
|
+
">#{options[:static_value]}</#{name}>\n"
|
|
55
|
+
end
|
|
56
|
+
|
|
50
57
|
def attributes_string
|
|
51
58
|
return "" if !attributes || attributes.size == 0
|
|
52
59
|
attributes.inject('') do |str, (key, value)|
|
data/lib/pump/xml/tag_array.rb
CHANGED
|
@@ -11,7 +11,11 @@ module Pump
|
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def to_s
|
|
14
|
-
|
|
14
|
+
if options.has_key?(:static_value)
|
|
15
|
+
"#{prefix}<#{name} type=\\\"array\\\"#{static_value_and_close_tag}"
|
|
16
|
+
else
|
|
17
|
+
"#{prefix}<#{name} type=\\\"array\\\"#{loop_and_close_tag}"
|
|
18
|
+
end
|
|
15
19
|
end
|
|
16
20
|
|
|
17
21
|
private
|
|
@@ -24,6 +28,11 @@ module Pump
|
|
|
24
28
|
"\#{ #{objects_path}.empty? ? \"/>\n\" : \">\n#{tag_loop}#{tabs}</#{name}>\n\" }"
|
|
25
29
|
end
|
|
26
30
|
|
|
31
|
+
def static_value_and_close_tag
|
|
32
|
+
return "/>\n" if options[:static_value].nil?
|
|
33
|
+
">#{options[:static_value]}</#{name}>\n"
|
|
34
|
+
end
|
|
35
|
+
|
|
27
36
|
def objects_path
|
|
28
37
|
options[:array_method] ? "object.#{options[:array_method]}" : "objects"
|
|
29
38
|
end
|
data/spec/pump/xml_spec.rb
CHANGED
|
@@ -203,12 +203,64 @@ describe Pump::Xml do
|
|
|
203
203
|
end
|
|
204
204
|
end
|
|
205
205
|
|
|
206
|
+
context "with static_value set" do
|
|
207
|
+
let(:person) { Struct.new(:name, :age, :is_yount).new('Gorbatschow', 82, false) }
|
|
208
|
+
|
|
209
|
+
context "replace with other value" do
|
|
210
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :static_value => 12}]) }
|
|
211
|
+
|
|
212
|
+
it "returns given static_value" do
|
|
213
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n <age>12</age>\n</person>\n")
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
context "replace with nil value" do
|
|
218
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :static_value => nil}]) }
|
|
219
|
+
|
|
220
|
+
it "returns given static_value" do
|
|
221
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n <age nil=\"true\"/>\n</person>\n")
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
context "replace with other value but with failed condition" do
|
|
226
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :static_value => 12, :if => :is_yount}]) }
|
|
227
|
+
|
|
228
|
+
it "returns given static_value" do
|
|
229
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n</person>\n")
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
context "replace with other value but with succssful condition" do
|
|
234
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :static_value => 12, :unless => :is_yount}]) }
|
|
235
|
+
|
|
236
|
+
it "returns given static_value" do
|
|
237
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n <age>12</age>\n</person>\n")
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
206
242
|
context "deep hash-like nesting" do
|
|
207
243
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}]}], :instruct => false) }
|
|
208
244
|
|
|
209
245
|
it "returns xml string" do
|
|
210
246
|
xml.encode(person).should eql("<person>\n <name>Benny</name>\n <parent>\n <name>Benny</name>\n <age>9</age>\n </parent>\n</person>\n")
|
|
211
247
|
end
|
|
248
|
+
|
|
249
|
+
context "with static_value = nil" do
|
|
250
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}], :static_value => nil}], :instruct => false) }
|
|
251
|
+
|
|
252
|
+
it "uses static value" do
|
|
253
|
+
xml.encode(person).should eql("<person>\n <name>Benny</name>\n <parent nil=\"true\"/>\n</person>\n")
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
context "with static_value = ''" do
|
|
258
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}], :static_value => ""}], :instruct => false) }
|
|
259
|
+
|
|
260
|
+
it "uses static value" do
|
|
261
|
+
xml.encode(person).should eql("<person>\n <name>Benny</name>\n <parent></parent>\n</person>\n")
|
|
262
|
+
end
|
|
263
|
+
end
|
|
212
264
|
end
|
|
213
265
|
|
|
214
266
|
context "deep array-like nesting" do
|
|
@@ -233,6 +285,22 @@ describe Pump::Xml do
|
|
|
233
285
|
xml.encode(person).should eql("<person>\n <name>Gustav</name>\n <children type=\"array\">\n <kid>\n <name>Lilly</name>\n </kid>\n <kid>\n <name>Lena</name>\n </kid>\n </children>\n</person>\n")
|
|
234
286
|
end
|
|
235
287
|
end
|
|
288
|
+
|
|
289
|
+
context "with static_value = nil" do
|
|
290
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:children => :children,
|
|
291
|
+
:array => [{:name => :name}], :static_value => nil}], :instruct => false) }
|
|
292
|
+
it "uses static value" do
|
|
293
|
+
xml.encode(person).should eql("<person>\n <name>Gustav</name>\n <children type=\"array\"/>\n</person>\n")
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
context "with static_value = ''" do
|
|
298
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:children => :children,
|
|
299
|
+
:array => [{:name => :name}], :static_value => ''}], :instruct => false) }
|
|
300
|
+
it "uses static value" do
|
|
301
|
+
xml.encode(person).should eql("<person>\n <name>Gustav</name>\n <children type=\"array\"></children>\n</person>\n")
|
|
302
|
+
end
|
|
303
|
+
end
|
|
236
304
|
end
|
|
237
305
|
end
|
|
238
306
|
end
|