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.124 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.109 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_enum/package/ree_enum/string_value_enum_mapper.rb