Module: Dry::Types::Coercions::Params

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

Overview

Params-specific coercions

Constant Summary collapse

TRUE_VALUES =
%w[1 on On ON t true True TRUE T y yes Yes YES Y].freeze
FALSE_VALUES =
%w[0 off Off OFF f false False FALSE F n no No NO N].freeze
BOOLEAN_MAP =
EMPTY_HASH.merge(
  [true, *TRUE_VALUES].to_h { |v| [v, true] },
  [false, *FALSE_VALUES].to_h { |v| [v, false] }
).freeze

Class Method Summary collapse

Methods included from Dry::Types::Coercions

to_date, to_date_time, to_symbol, to_time

Class Method Details

.to_ary(input, &_block) ⇒ Array, Object

Parameters:

  • input (Array, String, Object)

Returns:

Raises:

  • CoercionError



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dry/types/coercions/params.rb', line 135

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, &_block) ⇒ BigDecimal, ...

Parameters:

  • input (#to_d, Object)

Returns:

  • (BigDecimal, nil, Object)

Raises:

  • CoercionError



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dry/types/coercions/params.rb', line 116

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, &_block) ⇒ Boolean, Object

Parameters:

  • input (String, Object)

Returns:

  • (Boolean, Object)

Raises:

  • CoercionError

See Also:



69
70
71
72
73
74
75
76
77
# File 'lib/dry/types/coercions/params.rb', line 69

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) ⇒ Float, ...

Parameters:

  • input (#to_f, Object)

Returns:

  • (Float, nil, Object)

Raises:

  • CoercionError



103
104
105
106
107
# File 'lib/dry/types/coercions/params.rb', line 103

def self.to_float(input, &block)
  Float(input)
rescue ArgumentError, TypeError => e
  CoercionError.handle(e, &block)
end

.to_hash(input, &_block) ⇒ Hash, Object

Parameters:

  • input (Hash, String, Object)

Returns:

Raises:

  • CoercionError



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/dry/types/coercions/params.rb', line 154

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) ⇒ Integer, ...

Parameters:

  • input (#to_int, #to_i, Object)

Returns:

  • (Integer, nil, Object)

Raises:

  • CoercionError



86
87
88
89
90
91
92
93
94
# File 'lib/dry/types/coercions/params.rb', line 86

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, &_block) ⇒ nil

Returns if the input is an empty string or nil.

Parameters:

  • input (Object)

Returns:

  • (nil)

    if the input is an empty string or nil

Raises:

  • CoercionError



29
30
31
32
33
34
35
36
37
# File 'lib/dry/types/coercions/params.rb', line 29

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, &_block) ⇒ Boolean, Object

Parameters:

  • input (String, Object)

Returns:

  • (Boolean, Object)

Raises:

  • CoercionError

See Also:



49
50
51
52
53
54
55
56
57
# File 'lib/dry/types/coercions/params.rb', line 49

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