From 5320351fbc5f5174bbd636a2d8d70657cf0e8c0a Mon Sep 17 00:00:00 2001 From: Harry Mills Date: Tue, 4 Nov 2014 15:40:08 +0000 Subject: [PATCH 1/2] Monetary nodes should ignore nil values All the other nodes accept nil values and pass them through. Monetary nodes were choking when trying to sprintf a nil. This fixes that. --- lib/hermod/xml_section_builder.rb | 2 +- spec/hermod/xml_section_builder/monetary_node_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/hermod/xml_section_builder.rb b/lib/hermod/xml_section_builder.rb index 02fc0da..90b0e48 100755 --- a/lib/hermod/xml_section_builder.rb +++ b/lib/hermod/xml_section_builder.rb @@ -152,7 +152,7 @@ def monetary_node(name, options={}) end create_method(name, [], validators, options) do |value, attributes| - if options[:optional] && value == 0 + if (options[:optional] && value == 0) || value.blank? [nil, attributes] else [sprintf(format_for(:money), value), attributes] diff --git a/spec/hermod/xml_section_builder/monetary_node_spec.rb b/spec/hermod/xml_section_builder/monetary_node_spec.rb index e40b1fe..f947f91 100644 --- a/spec/hermod/xml_section_builder/monetary_node_spec.rb +++ b/spec/hermod/xml_section_builder/monetary_node_spec.rb @@ -51,6 +51,11 @@ module Hermod ex = proc { subject.student_loan 0 }.must_raise Hermod::InvalidInputError ex.message.must_equal "student_loan cannot be zero" end + + it "should ignore blank nodes" do + subject.ni nil + nodes("NI").must_be_empty + end end end end From 978d78b81f65884df1321ec5e8ef73ae95b71005 Mon Sep 17 00:00:00 2001 From: Harry Mills Date: Tue, 4 Nov 2014 16:35:37 +0000 Subject: [PATCH 2/2] Cast monetary node values to integers first --- lib/hermod/xml_section_builder.rb | 3 ++- spec/hermod/xml_section_builder/monetary_node_spec.rb | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/hermod/xml_section_builder.rb b/lib/hermod/xml_section_builder.rb index 90b0e48..5b3e7a9 100755 --- a/lib/hermod/xml_section_builder.rb +++ b/lib/hermod/xml_section_builder.rb @@ -152,7 +152,8 @@ def monetary_node(name, options={}) end create_method(name, [], validators, options) do |value, attributes| - if (options[:optional] && value == 0) || value.blank? + value = value.to_i + if options[:optional] && value == 0 [nil, attributes] else [sprintf(format_for(:money), value), attributes] diff --git a/spec/hermod/xml_section_builder/monetary_node_spec.rb b/spec/hermod/xml_section_builder/monetary_node_spec.rb index f947f91..6bfdf54 100644 --- a/spec/hermod/xml_section_builder/monetary_node_spec.rb +++ b/spec/hermod/xml_section_builder/monetary_node_spec.rb @@ -52,9 +52,12 @@ module Hermod ex.message.must_equal "student_loan cannot be zero" end - it "should ignore blank nodes" do + it "should treat blank nodes as zero nodes" do subject.ni nil - nodes("NI").must_be_empty + value_of_node("NI").must_equal "0.00" + + subject.tax nil + nodes("Tax").must_be_empty end end end