Sha256: 538e154e91239dee85d25c629ebc92f58a2a0a2fe42b9e2f5105424139aa9ef6

Contents?: true

Size: 1.24 KB

Versions: 68

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

class ReeMapper::Bool < ReeMapper::AbstractType
  TRUE_CAST_VALUES = ['1', 'true', 'on', 1, true].freeze
  FALSE_CAST_VALUES = ['0', 'false', 'off', 0, false].freeze

  contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::TypeError)
  def serialize(value, name:, role: nil)
    if value.is_a?(TrueClass) || value.is_a?(FalseClass)
      value
    else
      raise ReeMapper::TypeError, "`#{name}` should be a boolean"
    end
  end

  contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::CoercionError)
  def cast(value, name:, role: nil)
    if TRUE_CAST_VALUES.include?(value)
      true
    elsif FALSE_CAST_VALUES.include?(value)
      false
    else
      raise ReeMapper::CoercionError, "`#{name}` is invalid boolean"
    end
  end

  contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::TypeError)
  def db_dump(value, name:, role: nil)
    serialize(value, name: name, role: role)
  end

  contract(Any, Kwargs[name: String, role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::CoercionError)
  def db_load(value, name:, role: nil)
    cast(value, name: name, role: role)
  end
end

Version data entries

68 entries across 68 versions & 1 rubygems

Version Path
ree_lib-1.0.24 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.23 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.22 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.21 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.20 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.19 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.18 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.17 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb