class Stockboy::Translations::Boolean
Convert common false-like and true-like values to proper boolean true
or false
.
Returns nil for indeterminate values. This should be chained with a default value translator like [DefaultFalse] or [DefaultTrue].
Job
template DSL
¶ ↑
Registered as :boolean
. Use with:
attributes do active as: :boolean end
@example
bool = Stockboy::Translator::Boolean.new record.active = 't' bool.translate(record, :active) # => true record.active = 'f' bool.translate(record, :active) # => false record.active = '1' bool.translate(record, :active) # => true record.active = '0' bool.translate(record, :active) # => false record.active = 'y' bool.translate(record, :active) # => true record.active = 'n' bool.translate(record, :active) # => false record.active = '?' bool.translate(record, :active) # => nil
Constants
- FALSY_VALUES
- TRUTHY_VALUES
Public Instance Methods
translate(context)
click to toggle source
@return [Boolean]
# File lib/stockboy/translations/boolean.rb, line 49 def translate(context) value = field_value(context, field_key) return true if TRUTHY_VALUES.include?(value) return false if FALSY_VALUES.include?(value) return nil end