class ShallowAttributes::Type::Boolean

Abstract class for typecast object to Boolean type.

@abstract

@since 0.1.0

Constants

FALSE_VALUES

Array of false values

@private

@since 0.1.0

TRUE_VALUES

Array of true values

@private

@since 0.1.0

Public Instance Methods

coerce(value, _options = {}) click to toggle source

Convert value to Boolean type

@private

@param [Object] value @param [Hash] _options

@example Convert integer to boolean value

ShallowAttributes::Type::Boolean.new.coerce(1)
  # => true

ShallowAttributes::Type::Boolean.new.coerce(0)
  # => false

@raise [InvalidValueError] if value is not included in true and false arrays

@return [boolean]

@since 0.1.0

# File lib/shallow_attributes/type/boolean.rb, line 42
def coerce(value, _options = {})
  if TRUE_VALUES.include?(value)
    true
  elsif FALSE_VALUES.include?(value)
    false
  else
    raise ShallowAttributes::Type::InvalidValueError, %(Invalid value "#{value}" for type "Boolean")
  end
end