module Dry::Types::Coercions::JSON

JSON-specific coercions

@api public

Public Class Methods

to_decimal(input) { || ... } click to toggle source

@param [#to_d, Object] input

@return [BigDecimal,nil]

@raise CoercionError

@api public

# File lib/dry/types/coercions/json.rb, line 41
def self.to_decimal(input, &_block)
  if input.is_a?(::Float)
    input.to_d
  else
    BigDecimal(input)
  end
rescue ArgumentError, TypeError
  if block_given?
    yield
  else
    raise CoercionError, "#{input} cannot be coerced to decimal"
  end
end
to_nil(input) { || ... } click to toggle source

@param [Object] input

@return [nil] if the input is nil

@raise CoercionError

@api public

# File lib/dry/types/coercions/json.rb, line 24
def self.to_nil(input, &_block)
  if input.nil?
    nil
  elsif block_given?
    yield
  else
    raise CoercionError, "#{input.inspect} is not nil"
  end
end