happymapper
happymapper copied to clipboard
`BigDecimal` not working with Ruby 2.7+ because `#new` was removed
BigDecimal#new has been marked for deprecation for awhile and it looks like it finally happened in Ruby 2.7? This broke our configuration that used #new as the parser:
element :payment, BigDecimal, :tag => 'payment', :parser => :new
I have been able to register a new SupportedType and get the new syntax working:
module HappyMapper
module SupportedTypes
register_type BigDecimal do |value|
BigDecimal(value) if value && !value.empty?
end
end
end
Is the right approach or am I missing some other way to solve this issue?
Yes, I think that's the best way if you want to have the exact same behavior.
An alternative option is to parse using BigDecimal.interpret_loosely. That method is a bit more forgiving so it will accept "4.2 hello", whereas BigDecimal() will not:
element :payment, BigDecimal, :tag => 'payment', :parser => :interpret_loosely
Do you think this new type should be merged into Happymapper? I can make a PR
Yes, @sumirolabs that would be great!