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.45 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.44 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.43 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.42 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.41 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.40 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.39 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.38 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.37 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.36 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.35 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.34 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.33 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.32 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.31 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.30 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.29 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.28 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.27 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.26 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb