Class: Stockboy::Translations::Boolean

Inherits:
Stockboy::Translator show all
Defined in:
lib/stockboy/translations/boolean.rb

Overview

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

Examples:

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

Constant Summary collapse

TRUTHY_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE', 'y', 'Y', 'yes', 'YES']
FALSY_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'n', 'N', 'no', 'NO']

Instance Attribute Summary

Attributes inherited from Stockboy::Translator

#field_key

Instance Method Summary collapse

Methods inherited from Stockboy::Translator

#call, #initialize, #inspect

Constructor Details

This class inherits a constructor from Stockboy::Translator

Instance Method Details

#translate(context) ⇒ Boolean

Returns:



49
50
51
52
53
54
55
# 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