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
-
#to_boolean(value, _world) ⇒ TrueClass, FalseClass
Coerce value into a boolean.
-
#to_date(value, _world) ⇒ Date
Coerce value into a date.
-
#to_datetime(value, _world) ⇒ DateTime
Coerce value into a datetime.
-
#to_decimal(value, _world) ⇒ Decimal
Coerce value into a decimal.
-
#to_float(value, _world) ⇒ Float
Coerce value into a float.
-
#to_integer(value, _world) ⇒ Integer
Coerce value into a integer.
-
#to_string(value, _world) ⇒ String
Coerce value into a string.
-
#to_symbol(value, _world) ⇒ Symbol
Coerce value into a symbol.
-
#to_time(value, _world) ⇒ Time
Coerce value into a time.
Instance Method Details
#to_boolean(value, _world) ⇒ TrueClass, FalseClass
Coerce value into a boolean
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
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
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
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
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
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
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
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
140 141 142 |
# File 'lib/tablecloth/coercions.rb', line 140 def to_time(value, _world) Time.parse(value) end |