Class: ShallowAttributes::Type::Boolean Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/shallow_attributes/type/boolean.rb

Overview

This class is abstract.

Abstract class for typecast object to Boolean type.

Since:

  • 0.1.0

Constant Summary collapse

TRUE_VALUES =

Array of true values

Since:

  • 0.1.0

[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].freeze
FALSE_VALUES =

Array of false values

Since:

  • 0.1.0

[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', nil].freeze

Instance Method Summary collapse

Instance Method Details

#coerce(value, _options = {}) ⇒ boolean

Convert value to Boolean type

Examples:

Convert integer to boolean value

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

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

Parameters:

  • value (Object)
  • option (Hash)

Returns:

  • (boolean)

Raises:

Since:

  • 0.1.0


42
43
44
45
46
47
48
49
50
# 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