Module: AmritkScalarTest::Internal::Type::Enum Private
- Includes:
- Converter, Util::SorbetRuntimeSupport
- Defined in:
- lib/amritk-scalar-test/internal/type/enum.rb,
sig/amritk-scalar-test/internal/type/enum.rbs
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:
- boolean => true | false
- integer => Integer
- float => Float
- string => Symbol
We can therefore convert string values to Symbols, but can't convert other values safely.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #===(other) ⇒ Boolean
-
#coerce(value, state:) ⇒ Symbol, Object
private
Unlike with primitives,
Enumadditionally validates that the value is a member of the enum. - #dump(value, state:) ⇒ Symbol, Object private
- #hash ⇒ Integer
- #inspect(depth: 0) ⇒ String private
- #to_sorbet_type ⇒ Object private
-
#values ⇒ Array<NilClass, Boolean, Integer, Float, Symbol>
private
All of the valid Symbol values for this enum.
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
Class Method Details
.values ⇒ ::Array[(nil | bool | 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.
8 |
# File 'sig/amritk-scalar-test/internal/type/enum.rbs', line 8
def self.values: -> ::Array[(nil | bool | Integer | Float | Symbol)]
|
Instance Method Details
#==(other) ⇒ Boolean
40 41 42 43 44 |
# File 'lib/amritk-scalar-test/internal/type/enum.rb', line 40 def ==(other) # rubocop:disable Style/CaseEquality AmritkScalarTest::Internal::Type::Enum === other && other.values.to_set == values.to_set # rubocop:enable Style/CaseEquality end |
#===(other) ⇒ Boolean
33 |
# File 'lib/amritk-scalar-test/internal/type/enum.rb', line 33 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.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/amritk-scalar-test/internal/type/enum.rb', line 71 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.
|
|
# File 'lib/amritk-scalar-test/internal/type/enum.rb', line 88
|
#hash ⇒ Integer
49 |
# File 'lib/amritk-scalar-test/internal/type/enum.rb', line 49 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.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/amritk-scalar-test/internal/type/enum.rb', line 119 def inspect(depth: 0) if depth.positive? return is_a?(Module) ? super() : self.class.name end members = values.map { AmritkScalarTest::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.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/amritk-scalar-test/internal/type/enum.rb', line 102 def to_sorbet_type types = values.map { AmritkScalarTest::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(_1) }.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.
26 |
# File 'lib/amritk-scalar-test/internal/type/enum.rb', line 26 def values = constants.map { const_get(_1) } |