module Dry::Types::Coercions::Params

Params-specific coercions

@api public

Constants

BOOLEAN_MAP
FALSE_VALUES
TRUE_VALUES

Public Class Methods

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

@param [Array, String, Object] input

@return [Array, Object]

@raise CoercionError

@api public

# File lib/dry/types/coercions/params.rb, line 134
def self.to_ary(input, &_block)
  if empty_str?(input)
    []
  elsif input.is_a?(::Array)
    input
  elsif block_given?
    yield
  else
    raise CoercionError, "#{input.inspect} cannot be coerced to array"
  end
end
to_decimal(input) { || ... } click to toggle source

@param [#to_d, Object] input

@return [BigDecimal, nil, Object]

@raise CoercionError

@api public

# File lib/dry/types/coercions/params.rb, line 115
def self.to_decimal(input, &_block)
  to_float(input) do
    if block_given?
      return yield
    else
      raise CoercionError, "#{input.inspect} cannot be coerced to decimal"
    end
  end

  input.to_d
end
to_false(input) { || ... } click to toggle source

@param [String, Object] input

@return [Boolean,Object]

@see TRUE_VALUES @see FALSE_VALUES

@raise CoercionError

@api public

# File lib/dry/types/coercions/params.rb, line 68
def self.to_false(input, &_block)
  BOOLEAN_MAP.fetch(input.to_s) do
    if block_given?
      yield
    else
      raise CoercionError, "#{input} cannot be coerced to false"
    end
  end
end
to_float(input, &block) click to toggle source

@param [#to_f, Object] input

@return [Float, nil, Object]

@raise CoercionError

@api public

# File lib/dry/types/coercions/params.rb, line 102
def self.to_float(input, &block)
  Float(input)
rescue ArgumentError, TypeError => e
  CoercionError.handle(e, &block)
end
to_hash(input) { || ... } click to toggle source

@param [Hash, String, Object] input

@return [Hash, Object]

@raise CoercionError

@api public

# File lib/dry/types/coercions/params.rb, line 153
def self.to_hash(input, &_block)
  if empty_str?(input)
    {}
  elsif input.is_a?(::Hash)
    input
  elsif block_given?
    yield
  else
    raise CoercionError, "#{input.inspect} cannot be coerced to hash"
  end
end
to_int(input, &block) click to toggle source

@param [#to_int, to_i, Object] input

@return [Integer, nil, Object]

@raise CoercionError

@api public

# File lib/dry/types/coercions/params.rb, line 85
def self.to_int(input, &block)
  if input.is_a? String
    Integer(input, 10)
  else
    Integer(input)
  end
rescue ArgumentError, TypeError => e
  CoercionError.handle(e, &block)
end
to_nil(input) { || ... } click to toggle source

@param [Object] input

@return [nil] if the input is an empty string or nil

@raise CoercionError

@api public

# File lib/dry/types/coercions/params.rb, line 28
def self.to_nil(input, &_block)
  if input.nil? || empty_str?(input)
    nil
  elsif block_given?
    yield
  else
    raise CoercionError, "#{input.inspect} is not nil"
  end
end
to_true(input) { || ... } click to toggle source

@param [String, Object] input

@return [Boolean,Object]

@see TRUE_VALUES @see FALSE_VALUES

@raise CoercionError

@api public

# File lib/dry/types/coercions/params.rb, line 48
def self.to_true(input, &_block)
  BOOLEAN_MAP.fetch(input.to_s) do
    if block_given?
      yield
    else
      raise CoercionError, "#{input} cannot be coerced to true"
    end
  end
end