Sha256: 035ee9126e21403ccb353b3f1d90950df231bec67cc2395b22155947b2f4dae2

Contents?: true

Size: 942 Bytes

Versions: 1

Compression:

Stored size: 942 Bytes

Contents

# frozen_string_literal: true
require_relative "value"

class ReeEnum::BaseEnumMapper < ReeMapper::AbstractType
  attr_reader :enum

  def initialize(enum)
    @enum = enum
  end

  contract(
    ReeEnum::Value,
    Kwargs[
      name: String,
      role: Nilor[Symbol, ArrayOf[Symbol]]
    ] => Or[Integer, String]
  )
  def db_dump(value, name:, role: nil)
    value.mapped_value
  end

  contract(
    Or[Integer, String],
    Kwargs[
      name: String,
      role: Nilor[Symbol, ArrayOf[Symbol]]
    ] => ReeEnum::Value
  ).throws(ReeMapper::CoercionError)
  def db_load(value, name:, role: nil)
    enum_val = @enum.get_values.by_mapped_value(value)

    if !enum_val
      raise ReeMapper::CoercionError, "`#{name}` should be one of #{enum_inspection}, got `#{truncate(value.inspect)}`"
    end

    enum_val
  end

  private

  def enum_inspection
    @enum_inspection ||= truncate(@enum.get_values.each.map(&:to_s).inspect)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ree_lib-1.0.86 lib/ree_lib/packages/ree_enum/package/ree_enum/base_enum_mapper.rb