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 CHANGED
@@ -1,4 +1,4 @@
1
- guard 'rspec', :version => 2 do
1
+ guard 'rspec' do
2
2
  watch(%r{^spec/.+_spec\.rb})
3
3
  watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
4
4
  watch('spec/spec_helper.rb') { "spec" }
data/lib/pump/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Pump
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pump
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: