From 10159d2d8d752566607c2572c78d98cdc13262e8 Mon Sep 17 00:00:00 2001 From: Antony Bowen Date: Tue, 9 Jan 2018 15:46:13 +1100 Subject: [PATCH 1/2] Add support for BigDecimal parsing --- .../handlers/sax_abstract_handler.rb | 2 ++ spec/sax-machine/sax_document_spec.rb | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/sax-machine/handlers/sax_abstract_handler.rb b/lib/sax-machine/handlers/sax_abstract_handler.rb index 442bc2c..6591a42 100644 --- a/lib/sax-machine/handlers/sax_abstract_handler.rb +++ b/lib/sax-machine/handlers/sax_abstract_handler.rb @@ -65,6 +65,7 @@ def _start_element(name, attrs = []) case element_config.data_class.to_s when "Integer" then 0 when "Float" then 0.0 + when "BigDecimal" then BigDecimal.new("0.0") when "Symbol" then nil when "Time" then Time.at(0) when "" then object @@ -180,6 +181,7 @@ def data_class_value(data_class, value) when "String" then value != NO_BUFFER ? value.to_s : value when "Integer" then value != NO_BUFFER ? value.to_i : value when "Float" then value != NO_BUFFER ? value.to_s.gsub(",",".").to_f : value + when "BigDecimal" then value != NO_BUFFER ? BigDecimal.new(value.to_s.gsub(",",".")) : value when "Symbol" then if value != NO_BUFFER value.to_s.empty? ? nil : value.to_s.downcase.to_sym diff --git a/spec/sax-machine/sax_document_spec.rb b/spec/sax-machine/sax_document_spec.rb index f87037d..e07939e 100644 --- a/spec/sax-machine/sax_document_spec.rb +++ b/spec/sax-machine/sax_document_spec.rb @@ -245,6 +245,40 @@ class TestFloatWithAttribute end end + describe "big decimal" do + before do + class TestBigDecimal + include SAXMachine + element :number, class: BigDecimal + end + + class TestBigDecimalAttribute + include SAXMachine + attribute :sub_number, class: BigDecimal + end + + class TestBigDecimalWithAttribute + include SAXMachine + element :number, class: TestBigDecimalAttribute + end + end + + it "is handled in an element with '.' delimiter" do + document = TestBigDecimal.parse("3.0000000000000021") + expect(document.number).to eq(BigDecimal.new('3.0000000000000021')) + end + + it "is handled in an element with ',' delimiter" do + document = TestBigDecimal.parse("3,0000000000000021") + expect(document.number).to eq(BigDecimal.new('3.0000000000000021')) + end + + it "is handled in an attribute" do + document = TestBigDecimalWithAttribute.parse("3.0000000000000021") + expect(document.number.sub_number).to eq(BigDecimal.new('3.0000000000000021')) + end + end + describe "symbol" do before do class TestSymbol From ccc11c267e1f6931f2934bc9cb0b3c6244ec8716 Mon Sep 17 00:00:00 2001 From: Antony Bowen Date: Tue, 5 Jan 2021 11:47:46 +1100 Subject: [PATCH 2/2] Remove deprecated use of BigDecimal#new --- lib/sax-machine/handlers/sax_abstract_handler.rb | 4 ++-- spec/sax-machine/sax_document_spec.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/sax-machine/handlers/sax_abstract_handler.rb b/lib/sax-machine/handlers/sax_abstract_handler.rb index 6591a42..2ecb5cf 100644 --- a/lib/sax-machine/handlers/sax_abstract_handler.rb +++ b/lib/sax-machine/handlers/sax_abstract_handler.rb @@ -65,7 +65,7 @@ def _start_element(name, attrs = []) case element_config.data_class.to_s when "Integer" then 0 when "Float" then 0.0 - when "BigDecimal" then BigDecimal.new("0.0") + when "BigDecimal" then BigDecimal("0.0") when "Symbol" then nil when "Time" then Time.at(0) when "" then object @@ -181,7 +181,7 @@ def data_class_value(data_class, value) when "String" then value != NO_BUFFER ? value.to_s : value when "Integer" then value != NO_BUFFER ? value.to_i : value when "Float" then value != NO_BUFFER ? value.to_s.gsub(",",".").to_f : value - when "BigDecimal" then value != NO_BUFFER ? BigDecimal.new(value.to_s.gsub(",",".")) : value + when "BigDecimal" then value != NO_BUFFER ? BigDecimal(value.to_s.gsub(",",".")) : value when "Symbol" then if value != NO_BUFFER value.to_s.empty? ? nil : value.to_s.downcase.to_sym diff --git a/spec/sax-machine/sax_document_spec.rb b/spec/sax-machine/sax_document_spec.rb index e07939e..97e1426 100644 --- a/spec/sax-machine/sax_document_spec.rb +++ b/spec/sax-machine/sax_document_spec.rb @@ -265,17 +265,17 @@ class TestBigDecimalWithAttribute it "is handled in an element with '.' delimiter" do document = TestBigDecimal.parse("3.0000000000000021") - expect(document.number).to eq(BigDecimal.new('3.0000000000000021')) + expect(document.number).to eq(BigDecimal('3.0000000000000021')) end it "is handled in an element with ',' delimiter" do document = TestBigDecimal.parse("3,0000000000000021") - expect(document.number).to eq(BigDecimal.new('3.0000000000000021')) + expect(document.number).to eq(BigDecimal('3.0000000000000021')) end it "is handled in an attribute" do document = TestBigDecimalWithAttribute.parse("3.0000000000000021") - expect(document.number.sub_number).to eq(BigDecimal.new('3.0000000000000021')) + expect(document.number.sub_number).to eq(BigDecimal('3.0000000000000021')) end end