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.85 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.84 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.83 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.82 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.81 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.80 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.79 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.78 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.77 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.76 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.75 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.74 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.73 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.72 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.71 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.70 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.69 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.68 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.67 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb
ree_lib-1.0.66 lib/ree_lib/packages/ree_mapper/package/ree_mapper/types/bool.rb