pump 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +1 -1
- data/lib/pump/version.rb +1 -1
- data/lib/pump/xml/tag.rb +20 -2
- data/spec/pump/xml_spec.rb +29 -0
- metadata +1 -1
data/Guardfile
CHANGED
data/lib/pump/version.rb
CHANGED
data/lib/pump/xml/tag.rb
CHANGED
@@ -12,9 +12,9 @@ module Pump
|
|
12
12
|
|
13
13
|
def to_s
|
14
14
|
if !value_nodes? || options[:never_blank]
|
15
|
-
"#{open_tag}#{value_and_close_tag}"
|
15
|
+
"#{condition_start}#{open_tag}#{value_and_close_tag}#{condition_end}"
|
16
16
|
else
|
17
|
-
"#{open_tag}\#{v = #{nodes.first.plain};''}#{nil_attribute}\#{#{value_and_close_tag_with_blank_check}}"
|
17
|
+
"#{condition_start}#{open_tag}\#{v = #{nodes.first.plain};''}#{nil_attribute}\#{#{value_and_close_tag_with_blank_check}}#{condition_end}"
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -67,6 +67,24 @@ module Pump
|
|
67
67
|
def close_blank_tag
|
68
68
|
"\"/>\""
|
69
69
|
end
|
70
|
+
|
71
|
+
def condition_start
|
72
|
+
"\#{\"" if conditional?
|
73
|
+
end
|
74
|
+
|
75
|
+
def condition_end
|
76
|
+
return unless conditional?
|
77
|
+
|
78
|
+
if options[:if]
|
79
|
+
"\" if object.#{options[:if]} }"
|
80
|
+
elsif options[:unless]
|
81
|
+
"\" unless object.#{options[:unless]} }"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def conditional?
|
86
|
+
!!(options[:if] || options[:unless])
|
87
|
+
end
|
70
88
|
end
|
71
89
|
end
|
72
90
|
end
|
data/spec/pump/xml_spec.rb
CHANGED
@@ -170,5 +170,34 @@ describe Pump::Xml do
|
|
170
170
|
end
|
171
171
|
end
|
172
172
|
end
|
173
|
+
|
174
|
+
context "with conditionals" do
|
175
|
+
let(:person) { Struct.new(:name, :age, :is_young, :is_old).new('Gorbatschow', 82, false, true) }
|
176
|
+
|
177
|
+
context "simple if" do
|
178
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :if => :is_young}]) }
|
179
|
+
|
180
|
+
it "skips tag on false" do
|
181
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n</person>")
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context "simple unless" do
|
186
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :unless => :is_old}]) }
|
187
|
+
|
188
|
+
it "skips tag on false" do
|
189
|
+
xml.encode(person).should eql("#{XML_INSTRUCT}<person>\n <name>Gorbatschow</name>\n</person>")
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
context "chained" do
|
194
|
+
let(:xml) { Pump::Xml.new('person', [{:name => :name}, {:age => :age, :unless => 'age.nil?'}]) }
|
195
|
+
let(:people) { [person, Struct.new(:name, :age).new('Schewardnadse', nil)] }
|
196
|
+
|
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>")
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
173
202
|
end
|
174
203
|
end
|