Class: OpenAI::Internal::Type::ArrayOf Abstract Private
- Inherits:
-
Object
- Object
- OpenAI::Internal::Type::ArrayOf
- Includes:
- Converter, Util::SorbetRuntimeSupport
- Defined in:
- lib/openai/internal/type/array_of.rb,
sig/openai/internal/type/array_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.
Array of items of a given type.
Direct Known Subclasses
Class Method Summary collapse
- .[](type_info, spec = {}) ⇒ self private
Instance Method Summary collapse
- #==(other) ⇒ Boolean
- #===(other) ⇒ Boolean
- #coerce(value, state:) ⇒ Array<Object>, Object private
- #dump(value, state:) ⇒ Array<Object>, Object private
- #hash ⇒ Integer
-
#initialize(type_info, spec = {}) ⇒ ArrayOf
constructor
private
A new instance of ArrayOf.
- #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, coerce_with_error, dump, inspect, meta_info, new_coerce_state, type_info
Constructor Details
#initialize(type_info, spec = {}) ⇒ ArrayOf
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 ArrayOf.
169 170 171 172 173 |
# File 'lib/openai/internal/type/array_of.rb', line 169 def initialize(type_info, spec = {}) @item_type_fn = OpenAI::Internal::Type::Converter.type_info(type_info || spec) @meta = OpenAI::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.
34 |
# File 'lib/openai/internal/type/array_of.rb', line 34 def self.[](...) = new(...) |
Instance Method Details
#==(other) ⇒ Boolean
62 63 64 |
# File 'lib/openai/internal/type/array_of.rb', line 62 def ==(other) other.is_a?(OpenAI::Internal::Type::ArrayOf) && other.nilable? == nilable? && other.item_type == item_type end |
#===(other) ⇒ Boolean
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/openai/internal/type/array_of.rb', line 41 def ===(other) return false unless other.is_a?(Array) target = item_type other.all? do |item| case item in ^target true in nil nilable? else false end end end |
#coerce(value, state:) ⇒ Array<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.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/openai/internal/type/array_of.rb', line 88 def coerce(value, state:) exactness = state.fetch(:exactness) unless value.is_a?(Array) exactness[:no] += 1 state[:error] = TypeError.new("#{value.class} can't be coerced into #{Array}") return value end target = item_type exactness[:yes] += 1 previous_error = state.fetch(:error) last_error = nil converted = value.map do |item| case [nilable?, item] in [true, nil] exactness[:yes] += 1 nil else coerced, item_error = OpenAI::Internal::Type::Converter.coerce_with_error(target, item, state: state) last_error = item_error if item_error coerced end end state[:error] = last_error || previous_error converted end |
#dump(value, state:) ⇒ Array<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.
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/openai/internal/type/array_of.rb', line 126 def dump(value, state:) target = item_type if value.is_a?(Array) value.map do OpenAI::Internal::Type::Converter.dump(target, _1, state: state) end else super end end |
#hash ⇒ Integer
69 |
# File 'lib/openai/internal/type/array_of.rb', line 69 def hash = [self.class, item_type, nilable?].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.
180 181 182 183 184 |
# File 'lib/openai/internal/type/array_of.rb', line 180 def inspect(depth: 0) items = OpenAI::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.
149 |
# File 'lib/openai/internal/type/array_of.rb', line 149 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.
154 |
# File 'lib/openai/internal/type/array_of.rb', line 154 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.
140 141 142 143 144 |
# File 'lib/openai/internal/type/array_of.rb', line 140 def to_sorbet_type type = OpenAI::Internal::Util::SorbetRuntimeSupport.to_sorbet_type(item_type) type = T.nilable(type) if nilable? T::Array[type] end |