Class: InferModel::Parsers::Boolean

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer, Callable
Defined in:
lib/infer_model/parsers/boolean.rb

Constant Summary collapse

TRUTHY_VALUES_LOWERCASE =
%w[true t x y j + * 1].freeze
FALSEY_VALUES_LOWERCASE =
%w[false f n - 0].freeze

Instance Method Summary collapse

Instance Method Details

#callObject

Raises:



14
15
16
17
18
19
20
21
22
# File 'lib/infer_model/parsers/boolean.rb', line 14

def call
  raise Parsers::Error, "value was blank which is not allowed" if value.nil? && !allow_blank
  return if value.nil?
  return false if value.empty?
  return false if FALSEY_VALUES_LOWERCASE.any? { |lie| value.casecmp(lie).zero? }
  return true if TRUTHY_VALUES_LOWERCASE.any? { |truth| value.casecmp(truth).zero? }

  raise Parsers::Error, "'#{value}' is not a Boolean"
end