Module: Dry::Transformer::Coercions

Extended by:
Registry
Defined in:
lib/dry/transformer/coercions.rb

Overview

Coercion functions for common types

Constant Summary collapse

TRUE_VALUES =
[true, 1, "1", "on", "t", "true", "y", "yes"].freeze
FALSE_VALUES =
[false, 0, "0", "off", "f", "false", "n", "no", nil].freeze
BOOLEAN_MAP =
Hash[
  TRUE_VALUES.product([true]) + FALSE_VALUES.product([false])
].freeze

Class Method Summary collapse

Methods included from Registry

[], contain?, fetch, import, register, store

Class Method Details

.identity(value = nil) ⇒ Object

Does nothing and returns a value

Examples:

fn = Coercions[:identity]
fn[:foo] # => :foo

Parameters:

  • value (Object) (defaults to: nil)

Returns:

  • (Object)


34
35
36
# File 'lib/dry/transformer/coercions.rb', line 34

def self.identity(value = nil)
  value
end

.to_boolean(value) ⇒ TrueClass, FalseClass

Coerce value into a boolean

Examples:

Dry::Transformer(:to_boolean)['true']
# => true
Dry::Transformer(:to_boolean)['f']
# => false

Parameters:

  • value (Object)

    The input value

Returns:

  • (TrueClass, FalseClass)


126
127
128
# File 'lib/dry/transformer/coercions.rb', line 126

def self.to_boolean(value)
  BOOLEAN_MAP.fetch(value)
end

.to_date(value) ⇒ Date

Coerce value into a date

Examples:

Dry::Transformer(:to_date)['2015-04-14']
# => #<Date: 2015-04-14 ((2457127j,0s,0n),+0s,2299161j)>

Parameters:

  • value (Object)

    The input value

Returns:

  • (Date)


141
142
143
# File 'lib/dry/transformer/coercions.rb', line 141

def self.to_date(value)
  Date.parse(value)
end

.to_datetime(value) ⇒ DateTime

Coerce value into a datetime

Examples:

Dry::Transformer(:to_datetime)['2015-04-14 12:01:45']
# => #<DateTime: 2015-04-14T12:01:45+00:00 ((2457127j,43305s,0n),+0s,2299161j)>

Parameters:

  • value (Object)

    The input value

Returns:

  • (DateTime)


171
172
173
# File 'lib/dry/transformer/coercions.rb', line 171

def self.to_datetime(value)
  DateTime.parse(value)
end

.to_decimal(value) ⇒ Decimal

Coerce value into a decimal

Examples:

Dry::Transformer(:to_decimal)[1.2]
# => #<BigDecimal:7fca32acea50,'0.12E1',18(36)>

Parameters:

  • value (Object)

    The input value

Returns:

  • (Decimal)


109
110
111
# File 'lib/dry/transformer/coercions.rb', line 109

def self.to_decimal(value)
  value.to_d
end

.to_float(value) ⇒ Float

Coerce value into a float

Examples:

Dry::Transformer(:to_float)['1.2']
# => 1.2

Parameters:

  • value (Object)

    The input value

Returns:

  • (Float)


94
95
96
# File 'lib/dry/transformer/coercions.rb', line 94

def self.to_float(value)
  value.to_f
end

.to_integer(value) ⇒ Integer

Coerce value into a integer

Examples:

Dry::Transformer(:to_integer)['1']
# => 1

Parameters:

  • value (Object)

    The input value

Returns:

  • (Integer)


79
80
81
# File 'lib/dry/transformer/coercions.rb', line 79

def self.to_integer(value)
  value.to_i
end

.to_string(value) ⇒ String

Coerce value into a string

Examples:

Dry::Transformer(:to_string)[1]
# => "1"

Parameters:

  • value (Object)

    The input value

Returns:

  • (String)


49
50
51
# File 'lib/dry/transformer/coercions.rb', line 49

def self.to_string(value)
  value.to_s
end

.to_symbol(value) ⇒ Symbol

Coerce value into a symbol

Examples:

Dry::Transformer(:to_symbol)['foo']
# => :foo

Parameters:

  • value (#to_s)

    The input value

Returns:

  • (Symbol)


64
65
66
# File 'lib/dry/transformer/coercions.rb', line 64

def self.to_symbol(value)
  value.to_s.to_sym
end

.to_time(value) ⇒ Time

Coerce value into a time

Examples:

Dry::Transformer(:to_time)['2015-04-14 12:01:45']
# => 2015-04-14 12:01:45 +0200

Parameters:

  • value (Object)

    The input value

Returns:

  • (Time)


156
157
158
# File 'lib/dry/transformer/coercions.rb', line 156

def self.to_time(value)
  Time.parse(value)
end

.to_tuples(value) ⇒ Array<Hash>

Coerce value into an array containing tuples only

If the source is not an array, or doesn’t contain a tuple, returns an array with one empty tuple

Examples:

Dry::Transformer(:to_tuples)[:foo]                  # => [{}]
Dry::Transformer(:to_tuples)[[]]                    # => [{}]
Dry::Transformer(:to_tuples)[[{ foo: :FOO, :bar }]] # => [{ foo: :FOO }]

Parameters:

  • value (Object)

Returns:

  • (Array<Hash>)


189
190
191
192
193
# File 'lib/dry/transformer/coercions.rb', line 189

def self.to_tuples(value)
  array = value.is_a?(Array) ? Array[*value] : [{}]
  array.select! { |item| item.is_a?(Hash) }
  array.any? ? array : [{}]
end