Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/sax-machine/handlers/sax_abstract_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this change is from a while ago, but the API changed to BigDecimal("0.0") Ruby change

when "Symbol" then nil
when "Time" then Time.at(0)
when "" then object
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions spec/sax-machine/sax_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("<number>3.0000000000000021</number>")
expect(document.number).to eq(BigDecimal.new('3.0000000000000021'))
end

it "is handled in an element with ',' delimiter" do
document = TestBigDecimal.parse("<number>3,0000000000000021</number>")
expect(document.number).to eq(BigDecimal.new('3.0000000000000021'))
end

it "is handled in an attribute" do
document = TestBigDecimalWithAttribute.parse("<number sub_number='3.0000000000000021'>3.0000000000000021</number>")
expect(document.number.sub_number).to eq(BigDecimal.new('3.0000000000000021'))
end
end

describe "symbol" do
before do
class TestSymbol
Expand Down