Class: XTwitterScraper::Internal::Type::HashOf Abstract Private
- Inherits:
-
Object
- Object
- XTwitterScraper::Internal::Type::HashOf
- Includes:
- Converter, Util::SorbetRuntimeSupport
- Defined in:
- lib/x_twitter_scraper/internal/type/hash_of.rb,
sig/x_twitter_scraper/internal/type/hash_of.rbs
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
- #item_type ⇒ generic<Elem> private
- #nilable? ⇒ Boolean private
- #to_sorbet_type ⇒ Object private
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
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.
173 174 175 176 177 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 173 def initialize(type_info, spec = {}) @item_type_fn = XTwitterScraper::Internal::Type::Converter.type_info(type_info || spec) @meta = XTwitterScraper::Internal::Type::Converter.(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.
38 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 38 def self.[](...) = new(...) |
Instance Method Details
#==(other) ⇒ Boolean
67 68 69 70 71 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 67 def ==(other) # rubocop:disable Layout/LineLength other.is_a?(XTwitterScraper::Internal::Type::HashOf) && other.nilable? == nilable? && other.item_type == item_type # rubocop:enable Layout/LineLength end |
#===(other) ⇒ Boolean
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 45 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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 95 def coerce(value, state:) exactness = state.fetch(:exactness) unless value.is_a?(Hash) exactness[:no] += 1 state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}") 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 XTwitterScraper::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.
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 132 def dump(value, state:) target = item_type if value.is_a?(Hash) value.transform_values do XTwitterScraper::Internal::Type::Converter.dump(target, _1, state: state) end else super end end |
#hash ⇒ Integer
76 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 76 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.
184 185 186 187 188 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 184 def inspect(depth: 0) items = XTwitterScraper::Internal::Type::Converter.inspect(item_type, depth: depth.succ) "#{self.class}[#{[items, nilable? ? 'nil' : nil].compact.join(' | ')}]" end |
#item_type ⇒ generic<Elem>
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.
153 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 153 protected def item_type = @item_type_fn.call |
#nilable? ⇒ Boolean
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.
158 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 158 protected def nilable? = @nilable |
#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.
146 147 148 |
# File 'lib/x_twitter_scraper/internal/type/hash_of.rb', line 146 def to_sorbet_type T::Hash[XTwitterScraper::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(item_type)] end |