Class: UnifiedSettings::Coercer
- Inherits:
-
Object
- Object
- UnifiedSettings::Coercer
- Defined in:
- lib/unified_settings/coercer.rb
Overview
This will take string values and coerce them to the approrpiate ruby objects (e.g. “true” to the boolean true or “1.2” to the float 1.2)
Instance Method Summary collapse
- #coerce(value) ⇒ Object
-
#initialize(coercions: %i[nil boolean integer float],, coerce_arrays: true, array_separator: ',') ⇒ Coercer
constructor
A new instance of Coercer.
Constructor Details
#initialize(coercions: %i[nil boolean integer float],, coerce_arrays: true, array_separator: ',') ⇒ Coercer
Returns a new instance of Coercer.
9 10 11 12 13 14 15 16 17 |
# File 'lib/unified_settings/coercer.rb', line 9 def initialize( coercions: %i[nil boolean integer float], coerce_arrays: true, array_separator: ',' ) @coercions = coercions @coerce_arrays = coerce_arrays @array_separator = array_separator end |
Instance Method Details
#coerce(value) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/unified_settings/coercer.rb', line 19 def coerce(value) return value unless value # If it's already been cast to something other than a string, just # return what it is. return value unless value.is_a?(String) stripped_value = value.strip coerced_value, is_array = coerce_to_array(stripped_value) if is_array coerced_value.map do |array_value| array_value = array_value.strip coerced_value, did_coerce = coerce_value(array_value) did_coerce ? coerced_value : array_value end else coerced_value, did_coerce = coerce_value(stripped_value) did_coerce ? coerced_value : stripped_value end end |