Sha256: 625e3601b00468248ce1e187edd6884e36074db8067cd8f9e44c43b96c23e42b

Contents?: true

Size: 1.12 KB

Versions: 17

Compression:

Stored size: 1.12 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[role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::TypeError)
  def serialize(value, role: nil)
    if value.is_a?(TrueClass) || value.is_a?(FalseClass)
      value
    else
      raise ReeMapper::TypeError, 'should be a boolean'
    end
  end

  contract(Any, Kwargs[role: Nilor[Symbol, ArrayOf[Symbol]]] => Bool).throws(ReeMapper::CoercionError)
  def cast(value, role: nil)
    if TRUE_CAST_VALUES.include?(value)
      true
    elsif FALSE_CAST_VALUES.include?(value)
      false
    else
      raise ReeMapper::CoercionError, "should be a boolean"
    end
  end

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

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

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ree_lib-1.0.16 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.15 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.14 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.13 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.12 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.11 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.10 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.9 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.8 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.7 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.6 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.5 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.4 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.3 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.2 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.1 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.0 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb