Module: Dry::Types::Coercions::JSON

Extended by:
Dry::Types::Coercions
Defined in:
lib/dry/types/coercions/json.rb

Overview

JSON-specific coercions

Class Method Summary collapse

Methods included from Dry::Types::Coercions

to_date, to_date_time, to_symbol, to_time

Class Method Details

.to_decimal(input, &_block) ⇒ BigDecimal?

Parameters:

  • input (#to_d, Object)

Returns:

  • (BigDecimal, nil)

Raises:

  • CoercionError



41
42
43
44
45
46
47
48
49
50
51
52
53
# 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, &_block) ⇒ nil

Returns if the input is nil.

Parameters:

  • input (Object)

Returns:

  • (nil)

    if the input is nil

Raises:

  • CoercionError



24
25
26
27
28
29
30
31
32
# 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