Module: DeliveryFivePostClient::Internal::Type::Enum Private

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

A value from among a specified list of options. OpenAPI enum values map to Ruby values in the SDK as follows:

  1. boolean => true | false
  2. integer => Integer
  3. float => Float
  4. string => Symbol

We can therefore convert string values to Symbols, but can't convert other values safely.

Examples:

# `currency` is a `DeliveryFivePostClient::API::V1::Currency`
case currency
when DeliveryFivePostClient::API::V1::Currency::RUB
  # ...
else
  puts(currency)
end
case currency
in :RUB
  # ...
else
  puts(currency)
end

Instance Method Summary collapse

Methods included from Util::SorbetRuntimeSupport

#const_missing, #define_sorbet_constant!, #sorbet_constant_defined?, to_sorbet_type

Methods included from Converter

coerce, dump, inspect, meta_info, new_coerce_state, type_info

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



56
57
58
59
60
# File 'lib/delivery_five_post_client/internal/type/enum.rb', line 56

def ==(other)
  # rubocop:disable Style/CaseEquality
  DeliveryFivePostClient::Internal::Type::Enum === other && other.values.to_set == values.to_set
  # rubocop:enable Style/CaseEquality
end

#===(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



49
# File 'lib/delivery_five_post_client/internal/type/enum.rb', line 49

def ===(other) = values.include?(other)

#coerce(value, state:) ⇒ Symbol, Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Unlike with primitives, Enum additionally validates that the value is a member of the enum.

Parameters:

  • value (String, Symbol, Object)
  • state (Hash{Symbol=>Object}) —

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class] :error

    @option state [Integer] :branched

Returns:

  • (Symbol, Object)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/delivery_five_post_client/internal/type/enum.rb', line 87

def coerce(value, state:)
  exactness = state.fetch(:exactness)
  val = value.is_a?(String) ? value.to_sym : value

  if values.include?(val)
    exactness[:yes] += 1
    val
  elsif values.first&.class == val.class
    exactness[:maybe] += 1
    value
  else
    exactness[:no] += 1
    state[:error] = TypeError.new("#{value.class} can't be coerced into #{self}")
    value
  end
end

#dump(value, state:) ⇒ Symbol, Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • value (Symbol, Object)
  • state (Hash{Symbol=>Object}) —

    .

    @option state [Boolean] :can_retry

Returns:

  • (Symbol, Object)


# File 'lib/delivery_five_post_client/internal/type/enum.rb', line 104

#hash ⇒ Integer

Returns:

  • (Integer)


65
# File 'lib/delivery_five_post_client/internal/type/enum.rb', line 65

def hash = values.to_set.hash

#inspect(depth: 0) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • depth (Integer) (defaults to: 0)

Returns:

  • (String)


137
138
139
140
141
142
143
144
145
146
# File 'lib/delivery_five_post_client/internal/type/enum.rb', line 137

def inspect(depth: 0)
  if depth.positive?
    return is_a?(Module) ? super() : self.class.name
  end

  members = values.map { DeliveryFivePostClient::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
  prefix = is_a?(Module) ? name : self.class.name

  "#{prefix}[#{members.join(' | ')}]"
end

#to_sorbet_type ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Object)


118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/delivery_five_post_client/internal/type/enum.rb', line 118

def to_sorbet_type
  types = values.map do
    DeliveryFivePostClient::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1)
  end.uniq
  case types
  in []
    T.noreturn
  in [type]
    type
  else
    T.any(*types)
  end
end

#values ⇒ Array<NilClass, Boolean, Integer, Float, Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All of the valid Symbol values for this enum.

Returns:

  • (Array<NilClass, Boolean, Integer, Float, Symbol>)


42
# File 'lib/delivery_five_post_client/internal/type/enum.rb', line 42

def values = constants.map { const_get(_1) }