class OnebusawaySDK::Internal::Type::BaseModel

@abstract

def ==(other)

Returns:
  • (Boolean) -

Parameters:
  • other (Object) --

Other tags:
    Api: - public
def ==(other)
  other.is_a?(Class) && other <= OnebusawaySDK::Internal::Type::BaseModel && other.fields == fields
end

def ==(other) = self.class == other.class && @data == other.to_h

Returns:
  • (Boolean) -

Parameters:
  • other (Object) --

Other tags:
    Api: - public
def ==(other) = self.class == other.class && @data == other.to_h

def [](key)

Returns:
  • (Object, nil) -

Parameters:
  • key (Symbol) --

Other tags:
    Api: - public
def [](key)
  unless key.instance_of?(Symbol)
    raise ArgumentError.new("Expected symbol key for lookup, got #{key.inspect}")
  end
  @data[key]
end

def add_field(name_sym, required:, type_info:, spec:)

Options Hash: (**spec)
  • :"nil?" (Boolean) --
  • :union (Proc) --
  • :enum (Proc) --
  • :const (NilClass, TrueClass, FalseClass, Integer, Float, Symbol) --

Parameters:
  • spec (Hash{Symbol=>Object}) -- .
  • type_info (Hash{Symbol=>Object}, Proc, OnebusawaySDK::Internal::Type::Converter, Class) --
  • required (Boolean) --
  • name_sym (Symbol) --

Other tags:
    Api: - private
def add_field(name_sym, required:, type_info:, spec:)
n, info =
 type_info
roc | OnebusawaySDK::Internal::Type::Converter | Class
nebusawaySDK::Internal::Type::Converter.type_info({**spec, union: type_info}), spec]
ash
nebusawaySDK::Internal::Type::Converter.type_info(type_info), type_info]
 = :"#{name_sym}="
me = info.fetch(:api_name, name_sym)
e = info.fetch(:nil?, false)
= if required && !nilable
.fetch(
onst,
ebusawaySDK::Internal::OMIT
usawaySDK::Internal::OMIT
sym, setter].each { undef_method(_1) } if known_fields.key?(name_sym)
fields[name_sym] =
de: @mode,
i_name: api_name,
quired: required,
lable: nilable,
nst: const,
pe_fn: type_fn
_method(setter) do |value|
et = type_fn.call
e = OnebusawaySDK::Internal::Type::Converter.new_coerce_state(translate_names: false)
ced = OnebusawaySDK::Internal::Type::Converter.coerce(target, value, state: state)
us = @coerced.store(name_sym, state.fetch(:error) || true)
ed =
se [target, status]
 [OnebusawaySDK::Internal::Type::Converter | Symbol, true]
coerced
se
value
d
a.store(name_sym, stored)
cop:disable Style/CaseEquality
cop:disable Metrics/BlockLength
_method(name_sym) do
et = type_fn.call
 @coerced[name_sym]
rue | false if OnebusawaySDK::Internal::Type::Converter === target
ata.fetch(name_sym)
:StandardError => e
ise OnebusawaySDK::Errors::ConversionError.new(
on: self.class,
method: __method__,
target: target,
value: @data.fetch(name_sym),
cause: e

rnel.then do
value = @data.fetch(name_sym) { const == OnebusawaySDK::Internal::OMIT ? nil : const }
state = OnebusawaySDK::Internal::Type::Converter.new_coerce_state(translate_names: false)
if (nilable || !required) && value.nil?
  nil
else
  OnebusawaySDK::Internal::Type::Converter.coerce(
    target, value, state: state
  )
end
scue StandardError => e
raise OnebusawaySDK::Errors::ConversionError.new(
  on: self.class,
  method: __method__,
  target: target,
  value: value,
  cause: e
)
d
cop:enable Metrics/BlockLength
cop:enable Style/CaseEquality

def coerce(value, state:)

Returns:
  • (self, Object) -

Options Hash: (**state)
  • :branched (Integer) --
  • :error (Class) --
  • :exactness (Hash{Symbol=>Object}) --
  • :strictness (Boolean) --
  • :translate_names (Boolean) --

Parameters:
  • state (Hash{Symbol=>Object}) -- .
  • value (OnebusawaySDK::Internal::Type::BaseModel, Hash{Object=>Object}, Object) --

Other tags:
    Api: - private
def coerce(value, state:)
  exactness = state.fetch(:exactness)
  if value.is_a?(self)
    exactness[:yes] += 1
    return value
  end
  unless (val = OnebusawaySDK::Internal::Util.coerce_hash(value)).is_a?(Hash)
    exactness[:no] += 1
    state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
    return value
  end
  exactness[:yes] += 1
  keys = val.keys.to_set
  instance = new
  data = instance.to_h
  status = instance.instance_variable_get(:@coerced)
  # rubocop:disable Metrics/BlockLength
  fields.each do |name, field|
    mode, required, target = field.fetch_values(:mode, :required, :type)
    api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
    src_name = state.fetch(:translate_names) ? api_name : name
    unless val.key?(src_name)
      if required && mode != :dump && const == OnebusawaySDK::Internal::OMIT
        exactness[nilable ? :maybe : :no] += 1
      else
        exactness[:yes] += 1
      end
      next
    end
    item = val.fetch(src_name)
    keys.delete(src_name)
    state[:error] = nil
    converted =
      if item.nil? && (nilable || !required)
        exactness[nilable ? :yes : :maybe] += 1
        nil
      else
        coerced = OnebusawaySDK::Internal::Type::Converter.coerce(target, item, state: state)
        case target
        in OnebusawaySDK::Internal::Type::Converter | Symbol
          coerced
        else
          item
        end
      end
    status.store(name, state.fetch(:error) || true)
    data.store(name, converted)
  end
  # rubocop:enable Metrics/BlockLength
  keys.each { data.store(_1, val.fetch(_1)) }
  instance
end

def deconstruct_keys(keys)

Returns:
  • (Hash{Symbol=>Object}) -

Parameters:
  • keys (Array, nil) --
def deconstruct_keys(keys)
  (keys || self.class.known_fields.keys)
    .filter_map do |k|
      unless self.class.known_fields.key?(k)
        next
      end
      [k, public_send(k)]
    end
    .to_h
end

def deep_to_h = self.class.recursively_to_h(@data, convert: false)

Returns:
  • (Hash{Symbol=>Object}) -

Other tags:
    Api: - public
def deep_to_h = self.class.recursively_to_h(@data, convert: false)

def dump(value, state:)

Returns:
  • (Hash{Object=>Object}, Object) -

Options Hash: (**state)
  • :can_retry (Boolean) --

Parameters:
  • state (Hash{Symbol=>Object}) -- .
  • value (self, Object) --

Other tags:
    Api: - private
def dump(value, state:)
  unless (coerced = OnebusawaySDK::Internal::Util.coerce_hash(value)).is_a?(Hash)
    return super
  end
  acc = {}
  coerced.each do |key, val|
    name = key.is_a?(String) ? key.to_sym : key
    case (field = known_fields[name])
    in nil
      acc.store(name, super(val, state: state))
    else
      api_name, mode, type_fn = field.fetch_values(:api_name, :mode, :type_fn)
      case mode
      in :coerce
        next
      else
        target = type_fn.call
        acc.store(
          api_name,
          OnebusawaySDK::Internal::Type::Converter.dump(target, val, state: state)
        )
      end
    end
  end
  known_fields.each_value do |field|
    api_name, mode, const = field.fetch_values(:api_name, :mode, :const)
    next if mode == :coerce || acc.key?(api_name) || const == OnebusawaySDK::Internal::OMIT
    acc.store(api_name, const)
  end
  acc
end

def fields

Returns:
  • (Hash{Symbol=>Hash{Symbol=>Object}}) -

Other tags:
    Api: - private
def fields
  known_fields.transform_values do |field|
    {**field.except(:type_fn), type: field.fetch(:type_fn).call}
  end
end

def hash = fields.hash

Returns:
  • (Integer) -

Other tags:
    Api: - public
def hash = fields.hash

def hash = [self.class, @data].hash

Returns:
  • (Integer) -

Other tags:
    Api: - public
def hash = [self.class, @data].hash

def inherited(child)

Parameters:
  • child (Class) --

Other tags:
    Api: - private
def inherited(child)
  super
  child.known_fields.replace(known_fields.dup)
end

def initialize(data = {})

Parameters:
  • data (Hash{Symbol=>Object}, self) --
def initialize(data = {})
  @data = {}
  @coerced = {}
  OnebusawaySDK::Internal::Util.coerce_hash!(data).each do
    if self.class.known_fields.key?(_1)
      public_send(:"#{_1}=", _2)
    else
      @data.store(_1, _2)
      @coerced.store(_1, false)
    end
  end
end

def inspect(depth: 0)

Returns:
  • (String) -

Parameters:
  • depth (Integer) --

Other tags:
    Api: - private
def inspect(depth: 0)
  return super() if depth.positive?
  depth = depth.succ
  deferred = fields.transform_values do |field|
    type, required, nilable = field.fetch_values(:type, :required, :nilable)
    inspected = [
      OnebusawaySDK::Internal::Type::Converter.inspect(type, depth: depth),
      !required || nilable ? "nil" : nil
    ].compact.join(" | ")
    -> { inspected }.tap { _1.define_singleton_method(:inspect) { call } }
  end
  "#{name}[#{deferred.inspect}]"
end

def inspect

Returns:
  • (String) -

Other tags:
    Api: - private
def inspect
  converted = self.class.recursively_to_h(self, convert: true)
  "#<#{self.class}:0x#{object_id.to_s(16)} #{converted}>"
end

def known_fields = @known_fields ||= {}

Returns:
  • (Hash{Symbol=>Hash{Symbol=>Object}}) -

Other tags:
    Api: - private
def known_fields = @known_fields ||= {}

def optional(name_sym, type_info, spec = {})

Options Hash: (**spec)
  • :"nil?" (Boolean) --
  • :union (Proc) --
  • :enum (Proc) --
  • :const (NilClass, TrueClass, FalseClass, Integer, Float, Symbol) --

Parameters:
  • spec (Hash{Symbol=>Object}) -- .
  • type_info (Hash{Symbol=>Object}, Proc, OnebusawaySDK::Internal::Type::Converter, Class) --
  • name_sym (Symbol) --

Other tags:
    Api: - private
def optional(name_sym, type_info, spec = {})
  add_field(name_sym, required: false, type_info: type_info, spec: spec)
end

def recursively_to_h(model, convert:)

Returns:
  • (Hash{Symbol=>Object}) -

Parameters:
  • convert (Boolean) --
  • model (OnebusawaySDK::Internal::Type::BaseModel) --

Other tags:
    Api: - private
def recursively_to_h(model, convert:)
  rec = ->(x) do
    case x
    in OnebusawaySDK::Internal::Type::BaseModel
      if convert
        fields = x.class.known_fields
        x.to_h.to_h do |key, val|
          [key, rec.call(fields.key?(key) ? x.public_send(key) : val)]
        rescue OnebusawaySDK::Errors::ConversionError
          [key, rec.call(val)]
        end
      else
        rec.call(x.to_h)
      end
    in Hash
      x.transform_values(&rec)
    in Array
      x.map(&rec)
    else
      x
    end
  end
  rec.call(model)
end

def request_only(&blk)

Parameters:
  • blk (Proc) --

Other tags:
    Api: - private
def request_only(&blk)
= :dump
ll
= nil

def required(name_sym, type_info, spec = {})

Options Hash: (**spec)
  • :"nil?" (Boolean) --
  • :union (Proc) --
  • :enum (Proc) --
  • :const (NilClass, TrueClass, FalseClass, Integer, Float, Symbol) --

Parameters:
  • spec (Hash{Symbol=>Object}) -- .
  • type_info (Hash{Symbol=>Object}, Proc, OnebusawaySDK::Internal::Type::Converter, Class) --
  • name_sym (Symbol) --

Other tags:
    Api: - private
def required(name_sym, type_info, spec = {})
  add_field(name_sym, required: true, type_info: type_info, spec: spec)
end

def response_only(&blk)

Parameters:
  • blk (Proc) --

Other tags:
    Api: - private
def response_only(&blk)
= :coerce
ll
= nil

def to_h = @data

Returns:
  • (Hash{Symbol=>Object}) -

Other tags:
    Api: - public
def to_h = @data

def to_json(*a) = OnebusawaySDK::Internal::Type::Converter.dump(self.class, self).to_json(*a)

Returns:
  • (String) -

Parameters:
  • a (Object) --

Other tags:
    Api: - public
def to_json(*a) = OnebusawaySDK::Internal::Type::Converter.dump(self.class, self).to_json(*a)

def to_s = deep_to_h.to_s

Returns:
  • (String) -

Other tags:
    Api: - public
def to_s = deep_to_h.to_s

def to_sorbet_type

Returns:
  • (Object) -

Other tags:
    Api: - private
def to_sorbet_type
  self
end

def to_yaml(*a) = OnebusawaySDK::Internal::Type::Converter.dump(self.class, self).to_yaml(*a)

Returns:
  • (String) -

Parameters:
  • a (Object) --

Other tags:
    Api: - public
def to_yaml(*a) = OnebusawaySDK::Internal::Type::Converter.dump(self.class, self).to_yaml(*a)