pump 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pump/version.rb +1 -1
- data/lib/pump/xml/tag.rb +4 -4
- data/lib/pump/xml/tag_array.rb +7 -3
- data/lib/pump/xml.rb +2 -0
- data/spec/pump/xml/tag_spec.rb +7 -7
- data/spec/pump/xml_spec.rb +35 -23
- metadata +1 -1
data/lib/pump/version.rb
CHANGED
data/lib/pump/xml/tag.rb
CHANGED
@@ -34,13 +34,13 @@ module Pump
|
|
34
34
|
if level == 0
|
35
35
|
options[:instruct] ? INSTRUCT : (tabs)
|
36
36
|
else
|
37
|
-
"
|
37
|
+
"#{tabs}"
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
def value_and_close_tag(path=nil)
|
42
|
-
value = value_nodes? ? nodes.first.to_s(path) : (nodes.map(&:to_s).join
|
43
|
-
">#{value}#{tabs unless value_nodes?}</#{name}
|
42
|
+
value = value_nodes? ? nodes.first.to_s(path) : ("\n" << nodes.map(&:to_s).join)
|
43
|
+
">#{value}#{tabs unless value_nodes?}</#{name}>\n"
|
44
44
|
end
|
45
45
|
|
46
46
|
def value_and_close_tag_with_blank_check
|
@@ -67,7 +67,7 @@ module Pump
|
|
67
67
|
end
|
68
68
|
|
69
69
|
def close_blank_tag
|
70
|
-
"\"/>\""
|
70
|
+
"\"/>\n\""
|
71
71
|
end
|
72
72
|
|
73
73
|
def condition_start
|
data/lib/pump/xml/tag_array.rb
CHANGED
@@ -6,7 +6,7 @@ module Pump
|
|
6
6
|
class TagArray < Node
|
7
7
|
def initialize(name, attributes={}, nodes=[], options={})
|
8
8
|
tag = Tag.new(name, attributes, nodes, {:level => 1, :extra_indent => options[:extra_indent]})
|
9
|
-
array_root = options[:array_root] || name.pluralize
|
9
|
+
array_root = options[:array_root] || name.to_s.pluralize
|
10
10
|
super(array_root, {}, [tag], options)
|
11
11
|
end
|
12
12
|
|
@@ -21,11 +21,15 @@ module Pump
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def loop_and_close_tag
|
24
|
-
"\#{
|
24
|
+
"\#{ #{objects_path}.empty? ? \" />\n\" : \">\n#{tag_loop}#{tabs}</#{name}>\n\" }"
|
25
|
+
end
|
26
|
+
|
27
|
+
def objects_path
|
28
|
+
options[:array_method] ? "object.#{options[:array_method]}" : "objects"
|
25
29
|
end
|
26
30
|
|
27
31
|
def tag_loop
|
28
|
-
"\#{
|
32
|
+
"\#{#{objects_path}.map{|object| \"#{nodes.first}\" }.join('')}"
|
29
33
|
end
|
30
34
|
end
|
31
35
|
end
|
data/lib/pump/xml.rb
CHANGED
@@ -46,6 +46,8 @@ module Pump
|
|
46
46
|
tag_name, method_name = config.keys.first, config.values.first
|
47
47
|
if method_name.is_a?(Array)
|
48
48
|
Tag.new(tag_name, config[:attributes], method_name.map{|conf| build_tag(conf) }, config)
|
49
|
+
elsif config[:array].is_a?(Array)
|
50
|
+
TagArray.new(tag_name, config[:attributes], config[:array].map{|conf| build_tag(conf) }, config.merge({:array_method => method_name}))
|
49
51
|
else
|
50
52
|
Tag.new(tag_name, config[:attributes], Value.new(method_name), config)
|
51
53
|
end
|
data/spec/pump/xml/tag_spec.rb
CHANGED
@@ -15,14 +15,14 @@ describe Pump::Xml::Tag do
|
|
15
15
|
subject{ Pump::Xml::Tag.new("tag", attributes, Pump::Xml::Value.new('method'), options).to_s }
|
16
16
|
|
17
17
|
it {should eql(
|
18
|
-
"<tag\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\"}"
|
18
|
+
"<tag\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\n\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\n\"}"
|
19
19
|
)}
|
20
20
|
|
21
21
|
context "with :nil_check => true" do
|
22
22
|
let(:options) { {:nil_check => true} }
|
23
23
|
|
24
24
|
it {should eql(
|
25
|
-
"<tag\#{v = object.method;''}\#{\" nil=\\\"true\\\"\" if v.nil?}\#{v.nil? || v == '' ? \"/>\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\"}"
|
25
|
+
"<tag\#{v = object.method;''}\#{\" nil=\\\"true\\\"\" if v.nil?}\#{v.nil? || v == '' ? \"/>\n\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\n\"}"
|
26
26
|
)}
|
27
27
|
end
|
28
28
|
|
@@ -30,14 +30,14 @@ describe Pump::Xml::Tag do
|
|
30
30
|
let(:options) { {:never_blank => true} }
|
31
31
|
|
32
32
|
it {should eql(
|
33
|
-
"<tag>\#{object.method.to_s.encode(:xml => :text)}</tag
|
33
|
+
"<tag>\#{object.method.to_s.encode(:xml => :text)}</tag>\n"
|
34
34
|
)}
|
35
35
|
|
36
36
|
context "with attributes" do
|
37
37
|
let(:attributes) { {:foo => "bar"} }
|
38
38
|
|
39
39
|
it {should eql(
|
40
|
-
"<tag foo=\\\"bar\\\">\#{object.method.to_s.encode(:xml => :text)}</tag
|
40
|
+
"<tag foo=\\\"bar\\\">\#{object.method.to_s.encode(:xml => :text)}</tag>\n"
|
41
41
|
)}
|
42
42
|
end
|
43
43
|
end
|
@@ -46,7 +46,7 @@ describe Pump::Xml::Tag do
|
|
46
46
|
let(:options) { {:skip_encoding => true} }
|
47
47
|
|
48
48
|
it {should eql(
|
49
|
-
"<tag\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\" : \">\#{v}</tag>\"}"
|
49
|
+
"<tag\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\n\" : \">\#{v}</tag>\n\"}"
|
50
50
|
)}
|
51
51
|
end
|
52
52
|
|
@@ -54,7 +54,7 @@ describe Pump::Xml::Tag do
|
|
54
54
|
let(:attributes) { {:foo => "bar"} }
|
55
55
|
|
56
56
|
it {should eql(
|
57
|
-
"<tag foo=\\\"bar\\\"\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\"}"
|
57
|
+
"<tag foo=\\\"bar\\\"\#{v = object.method;''}\#{v.nil? || v == '' ? \"/>\n\" : \">\#{v.to_s.encode(:xml => :text)}</tag>\n\"}"
|
58
58
|
)}
|
59
59
|
end
|
60
60
|
end
|
@@ -69,7 +69,7 @@ describe Pump::Xml::Tag do
|
|
69
69
|
subject{ tag1.to_s }
|
70
70
|
|
71
71
|
it {should eql(
|
72
|
-
"<root>\n <child>\#{object.method}</child>\n</root
|
72
|
+
"<root>\n <child>\#{object.method}</child>\n</root>\n"
|
73
73
|
)}
|
74
74
|
end
|
75
75
|
end
|
data/spec/pump/xml_spec.rb
CHANGED
@@ -19,7 +19,7 @@ describe Pump::Xml do
|
|
19
19
|
end
|
20
20
|
|
21
21
|
it "returns xml string" do
|
22
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Benny</name>\n</person
|
22
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Benny</name>\n</person>\n")
|
23
23
|
end
|
24
24
|
|
25
25
|
context "with array" do
|
@@ -28,7 +28,7 @@ describe Pump::Xml do
|
|
28
28
|
let(:people) { [person] }
|
29
29
|
|
30
30
|
it "returns xml string" do
|
31
|
-
xml.encode(people).should eql("#{XML_INSTRUCT}<people type=\"array\">\n <person>\n <name>Benny</name>\n </person>\n</people
|
31
|
+
xml.encode(people).should eql("#{XML_INSTRUCT}<people type=\"array\">\n <person>\n <name>Benny</name>\n </person>\n</people>\n")
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -36,7 +36,7 @@ describe Pump::Xml do
|
|
36
36
|
let(:people) { [person, Struct.new(:name, :age).new('Carlo', 5)] }
|
37
37
|
|
38
38
|
it "returns xml string" do
|
39
|
-
xml.encode(people).should eql("#{XML_INSTRUCT}<people type=\"array\">\n <person>\n <name>Benny</name>\n </person>\n <person>\n <name>Carlo</name>\n </person>\n</people
|
39
|
+
xml.encode(people).should eql("#{XML_INSTRUCT}<people type=\"array\">\n <person>\n <name>Benny</name>\n </person>\n <person>\n <name>Carlo</name>\n </person>\n</people>\n")
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -44,7 +44,7 @@ describe Pump::Xml do
|
|
44
44
|
let(:people) { [] }
|
45
45
|
|
46
46
|
it "returns xml string" do
|
47
|
-
xml.encode(people).should eql("#{XML_INSTRUCT}<people type=\"array\"
|
47
|
+
xml.encode(people).should eql("#{XML_INSTRUCT}<people type=\"array\" />\n")
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -53,7 +53,7 @@ describe Pump::Xml do
|
|
53
53
|
let(:people) { [] }
|
54
54
|
|
55
55
|
it "returns xml string" do
|
56
|
-
xml.encode(people).should eql("<people type=\"array\"
|
56
|
+
xml.encode(people).should eql("<people type=\"array\" />\n")
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
@@ -62,7 +62,7 @@ describe Pump::Xml do
|
|
62
62
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false, :extra_indent => 1) }
|
63
63
|
|
64
64
|
it "returns xml string" do
|
65
|
-
xml.encode(people).should eql(" <people type=\"array\">\n <person>\n <name>Benny</name>\n </person>\n </people
|
65
|
+
xml.encode(people).should eql(" <people type=\"array\">\n <person>\n <name>Benny</name>\n </person>\n </people>\n")
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
@@ -71,7 +71,7 @@ describe Pump::Xml do
|
|
71
71
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false, :array_root => "personas") }
|
72
72
|
|
73
73
|
it "returns xml string" do
|
74
|
-
xml.encode(people).should eql("<personas type=\"array\">\n <person>\n <name>Benny</name>\n </person>\n</personas
|
74
|
+
xml.encode(people).should eql("<personas type=\"array\">\n <person>\n <name>Benny</name>\n </person>\n</personas>\n")
|
75
75
|
end
|
76
76
|
end
|
77
77
|
end
|
@@ -80,7 +80,7 @@ describe Pump::Xml do
|
|
80
80
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false) }
|
81
81
|
|
82
82
|
it "returns xml string" do
|
83
|
-
xml.encode(person).should eql("<person>\n <name>Benny</name>\n</person
|
83
|
+
xml.encode(person).should eql("<person>\n <name>Benny</name>\n</person>\n")
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
@@ -88,7 +88,7 @@ describe Pump::Xml do
|
|
88
88
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}], :instruct => false, :extra_indent => 1) }
|
89
89
|
|
90
90
|
it "returns xml string" do
|
91
|
-
xml.encode(person).should eql(" <person>\n <name>Benny</name>\n </person
|
91
|
+
xml.encode(person).should eql(" <person>\n <name>Benny</name>\n </person>\n")
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
@@ -101,7 +101,7 @@ describe Pump::Xml do
|
|
101
101
|
end
|
102
102
|
|
103
103
|
it do
|
104
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Benny</name>\n <age type=\"integer\">9</age>\n</person
|
104
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Benny</name>\n <age type=\"integer\">9</age>\n</person>\n")
|
105
105
|
end
|
106
106
|
end
|
107
107
|
|
@@ -109,7 +109,7 @@ describe Pump::Xml do
|
|
109
109
|
let(:person) { Struct.new(:name, :age).new('', 9) }
|
110
110
|
|
111
111
|
it do
|
112
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name/>\n</person
|
112
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name/>\n</person>\n")
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
@@ -117,14 +117,14 @@ describe Pump::Xml do
|
|
117
117
|
let(:person) { Struct.new(:name, :age).new(nil, 9) }
|
118
118
|
|
119
119
|
it do
|
120
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name/>\n</person
|
120
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name/>\n</person>\n")
|
121
121
|
end
|
122
122
|
|
123
123
|
context "and with :nil_check => true" do
|
124
124
|
let(:xml) { Pump::Xml.new('person', [{:name => :name, :nil_check => true}]) }
|
125
125
|
|
126
126
|
it do
|
127
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name nil=\"true\"/>\n</person
|
127
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name nil=\"true\"/>\n</person>\n")
|
128
128
|
end
|
129
129
|
end
|
130
130
|
end
|
@@ -133,7 +133,7 @@ describe Pump::Xml do
|
|
133
133
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age}]) }
|
134
134
|
|
135
135
|
it "returns xml string" do
|
136
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Benny</name>\n <age>9</age>\n</person
|
136
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Benny</name>\n <age>9</age>\n</person>\n")
|
137
137
|
end
|
138
138
|
end
|
139
139
|
|
@@ -141,7 +141,7 @@ describe Pump::Xml do
|
|
141
141
|
let(:xml) { Pump::Xml.new('person', [{"last-name" => :last_name}]) }
|
142
142
|
|
143
143
|
it "returns xml string" do
|
144
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <last-name>Hellman</last-name>\n</person
|
144
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <last-name>Hellman</last-name>\n</person>\n")
|
145
145
|
end
|
146
146
|
end
|
147
147
|
|
@@ -150,7 +150,7 @@ describe Pump::Xml do
|
|
150
150
|
let(:xml) { Pump::Xml.new('person', [{:at => :at, :attributes => {:type => 'date'}}]) }
|
151
151
|
|
152
152
|
it "returns xml string" do
|
153
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <at type=\"date\">2013-02-07</at>\n</person
|
153
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <at type=\"date\">2013-02-07</at>\n</person>\n")
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
@@ -159,14 +159,14 @@ describe Pump::Xml do
|
|
159
159
|
let(:xml) { Pump::Xml.new('person', [{:at => :at, :typecast => :xmlschema, :attributes => {:type => 'datetime'}}]) }
|
160
160
|
|
161
161
|
it "returns xml string" do
|
162
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <at type=\"datetime\">2013-02-07T00:00:00+01:00</at>\n</person
|
162
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <at type=\"datetime\">2013-02-07T00:00:00+01:00</at>\n</person>\n")
|
163
163
|
end
|
164
164
|
|
165
165
|
context "but nil" do
|
166
166
|
let(:person) { Struct.new(:at).new(nil) }
|
167
167
|
|
168
168
|
it "returns xml string" do
|
169
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <at type=\"datetime\"/>\n</person
|
169
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <at type=\"datetime\"/>\n</person>\n")
|
170
170
|
end
|
171
171
|
end
|
172
172
|
end
|
@@ -178,7 +178,7 @@ describe Pump::Xml do
|
|
178
178
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :if => :is_young}]) }
|
179
179
|
|
180
180
|
it "skips tag on false" do
|
181
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n</person
|
181
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n</person>\n")
|
182
182
|
end
|
183
183
|
end
|
184
184
|
|
@@ -186,7 +186,7 @@ describe Pump::Xml do
|
|
186
186
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :unless => :is_old}]) }
|
187
187
|
|
188
188
|
it "skips tag on false" do
|
189
|
-
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n</person
|
189
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n</person>\n")
|
190
190
|
end
|
191
191
|
end
|
192
192
|
|
@@ -195,17 +195,29 @@ describe Pump::Xml do
|
|
195
195
|
let(:people) { [person, Struct.new(:name, :age).new('Schewardnadse', nil)] }
|
196
196
|
|
197
197
|
it "skips tag on false" do
|
198
|
-
xml.encode(people).should eql("#{XML_INSTRUCT}<people type=\"array\">\n <person>\n <name>Gorbatschow</name>\n <age>82</age>\n </person>\n <person>\n <name>Schewardnadse</name>\n </person>\n</people
|
198
|
+
xml.encode(people).should eql("#{XML_INSTRUCT}<people type=\"array\">\n <person>\n <name>Gorbatschow</name>\n <age>82</age>\n </person>\n <person>\n <name>Schewardnadse</name>\n </person>\n</people>\n")
|
199
199
|
end
|
200
200
|
end
|
201
201
|
end
|
202
202
|
|
203
|
-
context "
|
203
|
+
context "deep hash-like nesting" do
|
204
204
|
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:parent => [{:name => :name}, {:age => :age}]}], :instruct => false) }
|
205
205
|
|
206
206
|
it "returns xml string" do
|
207
|
-
xml.encode(person).should eql("<person>\n <name>Benny</name>\n <parent>\n <name>Benny</name>\n <age>9</age>\n </parent>\n</person
|
207
|
+
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")
|
208
208
|
end
|
209
209
|
end
|
210
|
+
|
211
|
+
context "deep array-like nesting" do
|
212
|
+
let(:person) { Struct.new(:name, :children).new('Gustav', [Struct.new(:name).new('Lilly'), Struct.new(:name).new('Lena')]) }
|
213
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:child => :children, :array => [{:name => :name}]}], :instruct => false) }
|
214
|
+
|
215
|
+
it "returns xml string" do
|
216
|
+
xml.encode(person).should eql("<person>\n <name>Gustav</name>\n <children type=\"array\">\n <child>\n <name>Lilly</name>\n </child>\n <child>\n <name>Lena</name>\n </child>\n </children>\n</person>\n")
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
|
221
|
+
|
210
222
|
end
|
211
223
|
end
|