Class: ReeMapper::Bool

Inherits:
AbstractType show all
Defined in:
lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb

Constant Summary collapse

TRUE_CAST_VALUES =
['1', 'true', 'on', 1, true].freeze
FALSE_CAST_VALUES =
['0', 'false', 'off', 0, false].freeze

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb', line 17

def cast(value)
  if TRUE_CAST_VALUES.include?(value)
    true
  elsif FALSE_CAST_VALUES.include?(value)
    false
  else
    raise ReeMapper::CoercionError.new("is invalid boolean, got `#{truncate(value.inspect)}`")
  end
end

#db_dump(value) ⇒ Object



28
29
30
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb', line 28

def db_dump(value)
  serialize(value)
end

#db_load(value) ⇒ Object



33
34
35
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb', line 33

def db_load(value)
  cast(value)
end

#serialize(value) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb', line 8

def serialize(value)
  if value.is_a?(TrueClass) || value.is_a?(FalseClass)
    value
  else
    raise ReeMapper::TypeError.new("should be a boolean, got `#{truncate(value.inspect)}`")
  end
end