happymapper icon indicating copy to clipboard operation
happymapper copied to clipboard

`BigDecimal` not working with Ruby 2.7+ because `#new` was removed

Open sumirolabs opened this issue 2 years ago • 3 comments
trafficstars

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?

sumirolabs avatar Jun 26 '23 22:06 sumirolabs

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

mvz avatar Jun 30 '23 07:06 mvz

Do you think this new type should be merged into Happymapper? I can make a PR

sumirolabs avatar Jun 30 '23 23:06 sumirolabs

Yes, @sumirolabs that would be great!

mvz avatar Jul 04 '23 19:07 mvz