Module: Tablecloth::Coercions

Extended by:
Transproc::Functions
Defined in:
lib/tablecloth/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"].freeze
BOOLEAN_MAP =
Hash[
  TRUE_VALUES.product([true]) + FALSE_VALUES.product([false])
].freeze

Instance Method Summary collapse

Instance Method Details

#to_boolean(value, _world) ⇒ TrueClass, FalseClass

Coerce value into a boolean

Examples:

Transproc(:to_boolean)["true"]
# => true
Transproc(:to_boolean)["f"]
# => false

Parameters:

  • value (Object)

    The input value

Returns:

  • (TrueClass, FalseClass)


110
111
112
# File 'lib/tablecloth/coercions.rb', line 110

def to_boolean(value, _world)
  BOOLEAN_MAP.fetch(value)
end

#to_date(value, _world) ⇒ Date

Coerce value into a date

Examples:

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

Parameters:

  • value (Object)

    The input value

Returns:

  • (Date)


125
126
127
# File 'lib/tablecloth/coercions.rb', line 125

def to_date(value, _world)
  Date.parse(value)
end

#to_datetime(value, _world) ⇒ DateTime

Coerce value into a datetime

Examples:

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

Parameters:

  • value (Object)

    The input value

  • world (World)

    Cucumber world object

Returns:

  • (DateTime)


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

def to_datetime(value, _world)
  DateTime.parse(value)
end

#to_decimal(value, _world) ⇒ Decimal

Coerce value into a decimal

Examples:

Transproc(:to_decimal)[1.2]
# => #<BigDecimal:7fca32acea50,"0.12E1",18(36)>

Parameters:

  • value (Object)

    The input value

Returns:

  • (Decimal)


93
94
95
# File 'lib/tablecloth/coercions.rb', line 93

def to_decimal(value, _world)
  value.to_d
end

#to_float(value, _world) ⇒ Float

Coerce value into a float

Examples:

Transproc(:to_float)["1.2"]
# => 1.2

Parameters:

  • value (Object)

    The input value

Returns:

  • (Float)


78
79
80
# File 'lib/tablecloth/coercions.rb', line 78

def to_float(value, _world)
  value.to_f
end

#to_integer(value, _world) ⇒ Integer

Coerce value into a integer

Examples:

Transproc(:to_integer)["1"]
# => 1

Parameters:

  • value (Object)

    The input value

Returns:

  • (Integer)


63
64
65
# File 'lib/tablecloth/coercions.rb', line 63

def to_integer(value, _world)
  value.to_i
end

#to_string(value, _world) ⇒ String

Coerce value into a string

Examples:

Transproc(:to_string)[1]
# => "1"

Parameters:

  • value (Object)

    The input value

Returns:

  • (String)


33
34
35
# File 'lib/tablecloth/coercions.rb', line 33

def to_string(value, _world)
  value.to_s
end

#to_symbol(value, _world) ⇒ Symbol

Coerce value into a symbol

Examples:

Transproc(:to_symbol)["foo"]
# => :foo

Parameters:

  • value (Object)

    The input value

Returns:

  • (Symbol)


48
49
50
# File 'lib/tablecloth/coercions.rb', line 48

def to_symbol(value, _world)
  value.to_sym
end

#to_time(value, _world) ⇒ Time

Coerce value into a time

Examples:

Transproc(:to_time)["2015-04-14 12:01:45"]
# => 2015-04-14 12:01:45 +0200

Parameters:

  • value (Object)

    The input value

Returns:

  • (Time)


140
141
142
# File 'lib/tablecloth/coercions.rb', line 140

def to_time(value, _world)
  Time.parse(value)
end