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