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

Class Method Details

.to_date(input, &block) ⇒ Date, Object Originally defined in module Dry::Types::Coercions

Parameters:

  • input (#to_str, Object)

Returns:

  • (Date, Object)

See Also:

  • Date.parse

.to_date_time(input, &block) ⇒ DateTime, Object Originally defined in module Dry::Types::Coercions

Parameters:

  • input (#to_str, Object)

Returns:

  • (DateTime, Object)

See Also:

  • DateTime.parse

.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

.to_symbol(input, &block) ⇒ Symbol, Object Originally defined in module Dry::Types::Coercions

Parameters:

  • input (#to_sym, Object)

Returns:

  • (Symbol, Object)

Raises:

  • CoercionError

.to_time(input, &block) ⇒ Time, Object Originally defined in module Dry::Types::Coercions

Parameters:

  • input (#to_str, Object)

Returns:

  • (Time, Object)

See Also:

  • Time.parse