Module: Transproc::Coercions
- Extended by:
- Registry
- Defined in:
- lib/transproc/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
-
.identity(value = nil) ⇒ Object
Does nothing and returns a value.
-
.to_boolean(value) ⇒ TrueClass, FalseClass
Coerce value into a boolean.
-
.to_date(value) ⇒ Date
Coerce value into a date.
-
.to_datetime(value) ⇒ DateTime
Coerce value into a datetime.
-
.to_decimal(value) ⇒ Decimal
Coerce value into a decimal.
-
.to_float(value) ⇒ Float
Coerce value into a float.
-
.to_integer(value) ⇒ Integer
Coerce value into a integer.
-
.to_string(value) ⇒ String
Coerce value into a string.
-
.to_symbol(value) ⇒ Symbol
Coerce value into a symbol.
-
.to_time(value) ⇒ Time
Coerce value into a time.
-
.to_tuples(value) ⇒ Array<Hash>
Coerce value into an array containing tuples only.
Methods included from Registry
[], contain?, fetch, import, register, store
Class Method Details
.identity(value = nil) ⇒ Object
Does nothing and returns a value
33 34 35 |
# File 'lib/transproc/coercions.rb', line 33 def self.identity(value = nil) value end |
.to_boolean(value) ⇒ TrueClass, FalseClass
Coerce value into a boolean
125 126 127 |
# File 'lib/transproc/coercions.rb', line 125 def self.to_boolean(value) BOOLEAN_MAP.fetch(value) end |
.to_date(value) ⇒ Date
Coerce value into a date
140 141 142 |
# File 'lib/transproc/coercions.rb', line 140 def self.to_date(value) Date.parse(value) end |
.to_datetime(value) ⇒ DateTime
Coerce value into a datetime
170 171 172 |
# File 'lib/transproc/coercions.rb', line 170 def self.to_datetime(value) DateTime.parse(value) end |
.to_decimal(value) ⇒ Decimal
Coerce value into a decimal
108 109 110 |
# File 'lib/transproc/coercions.rb', line 108 def self.to_decimal(value) value.to_d end |
.to_float(value) ⇒ Float
Coerce value into a float
93 94 95 |
# File 'lib/transproc/coercions.rb', line 93 def self.to_float(value) value.to_f end |
.to_integer(value) ⇒ Integer
Coerce value into a integer
78 79 80 |
# File 'lib/transproc/coercions.rb', line 78 def self.to_integer(value) value.to_i end |
.to_string(value) ⇒ String
Coerce value into a string
48 49 50 |
# File 'lib/transproc/coercions.rb', line 48 def self.to_string(value) value.to_s end |
.to_symbol(value) ⇒ Symbol
Coerce value into a symbol
63 64 65 |
# File 'lib/transproc/coercions.rb', line 63 def self.to_symbol(value) value.to_s.to_sym end |
.to_time(value) ⇒ Time
Coerce value into a time
155 156 157 |
# File 'lib/transproc/coercions.rb', line 155 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
188 189 190 191 192 |
# File 'lib/transproc/coercions.rb', line 188 def self.to_tuples(value) array = value.is_a?(Array) ? Array[*value] : [{}] array.select! { |item| item.is_a?(Hash) } array.any? ? array : [{}] end |