Class: FinchAPI::Internal::Type::HashOf Abstract Private
- Inherits:
-
Object
- Object
- FinchAPI::Internal::Type::HashOf
- Includes:
- Converter
- Defined in:
- lib/finch_api/internal/type/hash_of.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Hash of items of a given type.
Class Method Summary collapse
- .[](type_info, spec = {}) ⇒ self private
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #===(other) ⇒ Boolean
- #coerce(value, state:) ⇒ Hash{Symbol=>Object}, Object private
- #dump(value, state:) ⇒ Hash{Symbol=>Object}, Object private
- #hash ⇒ Integer
-
#initialize(type_info, spec = {}) ⇒ HashOf
constructor
private
A new instance of HashOf.
- #inspect(depth: 0) ⇒ String private
Methods included from Converter
coerce, dump, inspect, type_info
Methods included from Util::SorbetRuntimeSupport
#const_missing, #define_sorbet_constant!
Constructor Details
#initialize(type_info, spec = {}) ⇒ HashOf
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 a new instance of HashOf.
156 157 158 159 |
# File 'lib/finch_api/internal/type/hash_of.rb', line 156 def initialize(type_info, spec = {}) @item_type_fn = FinchAPI::Internal::Type::Converter.type_info(type_info || spec) @nilable = spec.fetch(:nil?, false) end |
Class Method Details
.[](type_info, spec = {}) ⇒ self
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.
33 |
# File 'lib/finch_api/internal/type/hash_of.rb', line 33 def self.[](...) = new(...) |
Instance Method Details
#==(other) ⇒ Boolean
62 63 64 65 66 |
# File 'lib/finch_api/internal/type/hash_of.rb', line 62 def ==(other) # rubocop:disable Layout/LineLength other.is_a?(FinchAPI::Internal::Type::HashOf) && other.nilable? == nilable? && other.item_type == item_type # rubocop:enable Layout/LineLength end |
#===(other) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/finch_api/internal/type/hash_of.rb', line 40 def ===(other) type = item_type case other in Hash other.all? do |key, val| case [key, val] in [Symbol | String, ^type] true else false end end else false end end |
#coerce(value, state:) ⇒ Hash{Symbol=>Object}, 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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/finch_api/internal/type/hash_of.rb', line 86 def coerce(value, state:) exactness = state.fetch(:exactness) unless value.is_a?(Hash) exactness[:no] += 1 return value end target = item_type exactness[:yes] += 1 value .to_h do |key, val| k = key.is_a?(String) ? key.to_sym : key v = case [nilable?, val] in [true, nil] exactness[:yes] += 1 nil else FinchAPI::Internal::Type::Converter.coerce(target, val, state: state) end exactness[:no] += 1 unless k.is_a?(Symbol) [k, v] end end |
#dump(value, state:) ⇒ Hash{Symbol=>Object}, 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.
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/finch_api/internal/type/hash_of.rb', line 122 def dump(value, state:) target = item_type if value.is_a?(Hash) value.transform_values do FinchAPI::Internal::Type::Converter.dump(target, _1, state: state) end else super end end |
#hash ⇒ Integer
71 |
# File 'lib/finch_api/internal/type/hash_of.rb', line 71 def hash = [self.class, item_type].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.
166 167 168 169 170 |
# File 'lib/finch_api/internal/type/hash_of.rb', line 166 def inspect(depth: 0) items = FinchAPI::Internal::Type::Converter.inspect(item_type, depth: depth.succ) "#{self.class}[#{[items, nilable? ? 'nil' : nil].compact.join(' | ')}]" end |