Sha256: 9bd676999465d659eb7e8b99593ae31489cc4a68734aaa0e6c7c9a4f2dc304c1

Contents?: true

Size: 634 Bytes

Versions: 31

Compression:

Stored size: 634 Bytes

Contents

# frozen_string_literal: true
require_relative "base_enum_mapper"

class ReeEnum::StringValueEnumMapper < ReeEnum::BaseEnumMapper
  contract(ReeEnum::Value => String)
  def serialize(value)
    value.value
  end

  contract(Any => ReeEnum::Value).throws(ReeMapper::CoercionError)
  def cast(value)
    enum_value = case value
    when String
      @enum.get_values.by_value(value)
    when ReeEnum::Value
      @enum.get_values.each.find { _1 == value }
    end

    if enum_value.nil?
      raise ReeMapper::CoercionError.new("should be one of #{enum_inspection}, got `#{truncate(value.inspect)}`")
    end

    enum_value
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
ree_lib-1.0.104 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.103 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.102 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.101 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.100 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.99 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.98 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.97 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.96 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.95 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.94 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb