Class: OnebusawaySDK::Internal::Type::BaseModel Abstract

Inherits:
Object
  • Object
show all
Extended by:
Converter, Util::SorbetRuntimeSupport
Defined in:
lib/onebusaway_sdk/internal/type/base_model.rb

Overview

This class is abstract.

Direct Known Subclasses

Models::AgenciesWithCoverageListParams, Models::AgenciesWithCoverageListResponse::Data, Models::AgenciesWithCoverageListResponse::Data::List, Models::AgencyRetrieveParams, Models::AgencyRetrieveResponse::Data, Models::AgencyRetrieveResponse::Data::Entry, Models::ArrivalAndDepartureListParams, Models::ArrivalAndDepartureListResponse::Data, Models::ArrivalAndDepartureListResponse::Data::Entry, Models::ArrivalAndDepartureListResponse::Data::Entry::ArrivalsAndDeparture, Models::ArrivalAndDepartureListResponse::Data::Entry::ArrivalsAndDeparture::TripStatus, Models::ArrivalAndDepartureListResponse::Data::Entry::ArrivalsAndDeparture::TripStatus::LastKnownLocation, Models::ArrivalAndDepartureListResponse::Data::Entry::ArrivalsAndDeparture::TripStatus::Position, Models::ArrivalAndDepartureRetrieveParams, Models::ArrivalAndDepartureRetrieveResponse::Data, Models::ArrivalAndDepartureRetrieveResponse::Data::Entry, Models::ArrivalAndDepartureRetrieveResponse::Data::Entry::TripStatus, Models::ArrivalAndDepartureRetrieveResponse::Data::Entry::TripStatus::LastKnownLocation, Models::ArrivalAndDepartureRetrieveResponse::Data::Entry::TripStatus::Position, Models::BlockRetrieveParams, Models::BlockRetrieveResponse::Data, Models::BlockRetrieveResponse::Data::Entry, Models::BlockRetrieveResponse::Data::Entry::Configuration, Models::BlockRetrieveResponse::Data::Entry::Configuration::Trip, Models::BlockRetrieveResponse::Data::Entry::Configuration::Trip::BlockStopTime, Models::BlockRetrieveResponse::Data::Entry::Configuration::Trip::BlockStopTime::StopTime, Models::ConfigRetrieveParams, Models::ConfigRetrieveResponse::Data, Models::ConfigRetrieveResponse::Data::Entry, Models::ConfigRetrieveResponse::Data::Entry::GitProperties, Models::CurrentTimeRetrieveParams, Models::CurrentTimeRetrieveResponse::Data, Models::CurrentTimeRetrieveResponse::Data::Entry, Models::References, Models::References::Agency, Models::References::Route, Models::References::Situation, Models::References::Situation::ActiveWindow, Models::References::Situation::AllAffect, Models::References::Situation::Consequence, Models::References::Situation::Consequence::ConditionDetails, Models::References::Situation::Consequence::ConditionDetails::DiversionPath, Models::References::Situation::Description, Models::References::Situation::PublicationWindow, Models::References::Situation::Summary, Models::References::Situation::URL, Models::References::Stop, Models::References::StopTime, Models::References::Trip, Models::ReportProblemWithStopRetrieveParams, Models::ReportProblemWithTripRetrieveParams, Models::ResponseWrapper, Models::RouteIDsForAgencyListParams, Models::RouteIDsForAgencyListResponse::Data, Models::RouteRetrieveParams, Models::RouteRetrieveResponse::Data, Models::RouteRetrieveResponse::Data::Entry, Models::RoutesForAgencyListParams, Models::RoutesForAgencyListResponse::Data, Models::RoutesForAgencyListResponse::Data::List, Models::RoutesForLocationListParams, Models::RoutesForLocationListResponse::Data, Models::RoutesForLocationListResponse::Data::List, Models::ScheduleForRouteRetrieveParams, Models::ScheduleForRouteRetrieveResponse::Data, Models::ScheduleForRouteRetrieveResponse::Data::Entry, Models::ScheduleForRouteRetrieveResponse::Data::Entry::Stop, Models::ScheduleForRouteRetrieveResponse::Data::Entry::StopTripGrouping, Models::ScheduleForRouteRetrieveResponse::Data::Entry::StopTripGrouping::TripsWithStopTime, Models::ScheduleForRouteRetrieveResponse::Data::Entry::StopTripGrouping::TripsWithStopTime::StopTime, Models::ScheduleForRouteRetrieveResponse::Data::Entry::Trip, Models::ScheduleForStopRetrieveParams, Models::ScheduleForStopRetrieveResponse::Data, Models::ScheduleForStopRetrieveResponse::Data::Entry, Models::ScheduleForStopRetrieveResponse::Data::Entry::StopRouteSchedule, Models::ScheduleForStopRetrieveResponse::Data::Entry::StopRouteSchedule::StopRouteDirectionSchedule, Models::ScheduleForStopRetrieveResponse::Data::Entry::StopRouteSchedule::StopRouteDirectionSchedule::ScheduleFrequency, Models::ScheduleForStopRetrieveResponse::Data::Entry::StopRouteSchedule::StopRouteDirectionSchedule::ScheduleStopTime, Models::SearchForRouteListParams, Models::SearchForRouteListResponse::Data, Models::SearchForRouteListResponse::Data::List, Models::SearchForStopListParams, Models::SearchForStopListResponse::Data, Models::SearchForStopListResponse::Data::List, Models::ShapeRetrieveParams, Models::ShapeRetrieveResponse::Data, Models::ShapeRetrieveResponse::Data::Entry, Models::StopIDsForAgencyListParams, Models::StopIDsForAgencyListResponse::Data, Models::StopRetrieveParams, Models::StopRetrieveResponse::Data, Models::StopRetrieveResponse::Data::Entry, Models::StopsForAgencyListParams, Models::StopsForAgencyListResponse::List, Models::StopsForLocationListParams, Models::StopsForLocationListResponse::Data, Models::StopsForLocationListResponse::Data::List, Models::StopsForRouteListParams, Models::StopsForRouteListResponse::Data, Models::StopsForRouteListResponse::Data::Entry, Models::StopsForRouteListResponse::Data::Entry::Polyline, Models::StopsForRouteListResponse::Data::Entry::StopGrouping, Models::StopsForRouteListResponse::Data::Entry::StopGrouping::Name, Models::StopsForRouteListResponse::Data::Entry::StopGrouping::Polyline, Models::TripDetailRetrieveParams, Models::TripDetailRetrieveResponse::Data, Models::TripDetailRetrieveResponse::Data::Entry, Models::TripDetailRetrieveResponse::Data::Entry::Schedule, Models::TripDetailRetrieveResponse::Data::Entry::Schedule::StopTime, Models::TripDetailRetrieveResponse::Data::Entry::Status, Models::TripDetailRetrieveResponse::Data::Entry::Status::LastKnownLocation, Models::TripDetailRetrieveResponse::Data::Entry::Status::Position, Models::TripForVehicleRetrieveParams, Models::TripForVehicleRetrieveResponse::Data, Models::TripForVehicleRetrieveResponse::Data::Entry, Models::TripForVehicleRetrieveResponse::Data::Entry::Schedule, Models::TripForVehicleRetrieveResponse::Data::Entry::Schedule::StopTime, Models::TripForVehicleRetrieveResponse::Data::Entry::Status, Models::TripForVehicleRetrieveResponse::Data::Entry::Status::LastKnownLocation, Models::TripForVehicleRetrieveResponse::Data::Entry::Status::Position, Models::TripRetrieveParams, Models::TripRetrieveResponse::Data, Models::TripRetrieveResponse::Data::Entry, Models::TripsForLocationListParams, Models::TripsForLocationListResponse::Data, Models::TripsForLocationListResponse::Data::List, Models::TripsForLocationListResponse::Data::List::Schedule, Models::TripsForLocationListResponse::Data::List::Schedule::StopTime, Models::TripsForLocationListResponse::Data::List::Status, Models::TripsForLocationListResponse::Data::List::Status::LastKnownLocation, Models::TripsForLocationListResponse::Data::List::Status::Position, Models::TripsForRouteListParams, Models::TripsForRouteListResponse::Data, Models::TripsForRouteListResponse::Data::List, Models::TripsForRouteListResponse::Data::List::Schedule, Models::TripsForRouteListResponse::Data::List::Schedule::StopTime, Models::TripsForRouteListResponse::Data::List::Status, Models::TripsForRouteListResponse::Data::List::Status::LastKnownLocation, Models::TripsForRouteListResponse::Data::List::Status::Position, Models::VehiclesForAgencyListParams, Models::VehiclesForAgencyListResponse::Data, Models::VehiclesForAgencyListResponse::Data::List, Models::VehiclesForAgencyListResponse::Data::List::Location, Models::VehiclesForAgencyListResponse::Data::List::TripStatus, Models::VehiclesForAgencyListResponse::Data::List::TripStatus::LastKnownLocation, Models::VehiclesForAgencyListResponse::Data::List::TripStatus::Position, RequestOptions

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Converter

coerce, dump, meta_info, new_coerce_state, type_info

Methods included from Util::SorbetRuntimeSupport

const_missing, define_sorbet_constant!, sorbet_constant_defined?, to_sorbet_type

Constructor Details

#initialize(data = {}) ⇒ BaseModel

Create a new instance of a model.

Parameters:

  • data (Hash{Symbol=>Object}, self) (defaults to: {})


479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 479

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

Class Method Details

.==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



207
208
209
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 207

def ==(other)
  other.is_a?(Class) && other <= OnebusawaySDK::Internal::Type::BaseModel && other.fields == fields
end

.coerce(value, state:) ⇒ self, 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.

Parameters:

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

    .

    @option state [Boolean] :translate_names

    @option state [Boolean] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Class<StandardError>] :error

    @option state [Integer] :branched

Returns:

  • (self, Object)


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 247

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

.dump(value, state:) ⇒ Hash{Object=>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.

Parameters:

  • value (self, Object)
  • state (Hash{Symbol=>Object})

    .

    @option state [Boolean] :can_retry

Returns:

  • (Hash{Object=>Object}, Object)


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 318

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

.fieldsHash{Symbol=>Hash{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.

Returns:

  • (Hash{Symbol=>Hash{Symbol=>Object}})


31
32
33
34
35
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 31

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

.hashInteger

Returns:

  • (Integer)


214
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 214

def hash = fields.hash

.inherited(child) ⇒ 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.

Assumes superclass fields are totally defined before fields are accessed / defined on subclasses.

Parameters:



18
19
20
21
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 18

def inherited(child)
  super
  child.known_fields.replace(known_fields.dup)
end

.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.

Parameters:

  • depth (Integer) (defaults to: 0)

Returns:

  • (String)


498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 498

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

.known_fieldsHash{Symbol=>Hash{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.

Returns:

  • (Hash{Symbol=>Hash{Symbol=>Object}})


26
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 26

def known_fields = @known_fields ||= {}

.optional(name_sym, type_info, spec = {}) ⇒ 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.

Parameters:

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

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :“nil?”



173
174
175
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 173

def optional(name_sym, type_info, spec = {})
  add_field(name_sym, required: false, type_info: type_info, spec: spec)
end

.recursively_to_h(model, convert:) ⇒ Hash{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.

Parameters:

Returns:

  • (Hash{Symbol=>Object})


369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 369

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

.required(name_sym, type_info, spec = {}) ⇒ 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.

Parameters:

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

    .

    @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const

    @option spec [Proc] :enum

    @option spec [Proc] :union

    @option spec [Boolean] :“nil?”



154
155
156
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 154

def required(name_sym, type_info, spec = {})
  add_field(name_sym, required: true, type_info: type_info, spec: spec)
end

.to_sorbet_typeObject

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:

  • (Object)


357
358
359
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 357

def to_sorbet_type
  self
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



222
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 222

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

#[](key) ⇒ Object?

Returns the raw value associated with the given key, if found. Otherwise, nil is returned.

It is valid to lookup keys that are not in the API spec, for example to access undocumented features. This method does not parse response data into higher-level types. Lookup by anything other than a Symbol is an ArgumentError.

Parameters:

  • key (Symbol)

Returns:

  • (Object, nil)


407
408
409
410
411
412
413
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 407

def [](key)
  unless key.instance_of?(Symbol)
    raise ArgumentError.new("Expected symbol key for lookup, got #{key.inspect}")
  end

  @data[key]
end

#deconstruct_keys(keys) ⇒ Hash{Symbol=>Object}

Examples:

# `references` is a `OnebusawaySDK::References`
references => {
  agencies: agencies,
  routes: routes,
  situations: situations
}

Parameters:

  • keys (Array<Symbol>, nil)

Returns:

  • (Hash{Symbol=>Object})


450
451
452
453
454
455
456
457
458
459
460
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 450

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

#deep_to_hHash{Symbol=>Object}

In addition to the behaviour of ‘#to_h`, this method will recursively call `#to_h` on nested models.

Returns:

  • (Hash{Symbol=>Object})


437
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 437

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

#hashInteger

Returns:

  • (Integer)


227
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 227

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

#inspectString

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:

  • (String)


523
524
525
526
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 523

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

#to_hHash{Symbol=>Object} Also known as: to_hash

Returns a Hash of the data underlying this object. O(1)

Keys are Symbols and values are the raw values from the response. The return value indicates which values were ever set on the object. i.e. there will be a key in this hash if they ever were, even if the set value was nil.

This method is not recursive. The returned value is shared by the object, so it should not be mutated.

Returns:

  • (Hash{Symbol=>Object})


427
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 427

def to_h = @data

#to_json(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


467
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 467

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

#to_sString

Returns:

  • (String)


518
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 518

def to_s = deep_to_h.to_s

#to_yaml(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


474
# File 'lib/onebusaway_sdk/internal/type/base_model.rb', line 474

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