Class: FinchAPI::Internal::Type::BaseModel Abstract

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

Overview

This class is abstract.

Direct Known Subclasses

Models::AccessTokenCreateParams, Models::AccountDisconnectParams, Models::AccountIntrospectParams, Models::AccountUpdateEvent::Data, Models::AccountUpdateEvent::Data::AuthenticationMethod, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Company, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Company::Accounts, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Company::Departments, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Company::Departments::Parent, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Company::Entity, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Company::Locations, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Directory, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Directory::Individuals, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Directory::Individuals::Manager, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Directory::Paging, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Employment, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Employment::Department, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Employment::Employment, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Employment::Income, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Employment::Location, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Employment::Manager, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Individual, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Individual::Emails, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Individual::PhoneNumbers, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Individual::Residence, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::PayGroup, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::PayStatement, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::PayStatement::Paging, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::PayStatement::PayStatements, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::PayStatement::PayStatements::Earnings, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::PayStatement::PayStatements::EmployeeDeductions, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::PayStatement::PayStatements::EmployerContributions, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::PayStatement::PayStatements::Taxes, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Payment, Models::AccountUpdateEvent::Data::AuthenticationMethod::SupportedFields::Payment::PayPeriod, Models::BaseWebhookEvent, Models::Connect::SessionNewParams, Models::Connect::SessionNewParams::Integration, Models::Connect::SessionNewResponse, Models::Connect::SessionReauthenticateParams, Models::Connect::SessionReauthenticateResponse, Models::CreateAccessTokenResponse, Models::DirectoryEvent::Data, Models::DisconnectResponse, Models::EmploymentEvent::Data, Models::HRIS::BenefitContribution, Models::HRIS::BenefitCreateParams, Models::HRIS::BenefitCreateParams::CompanyContribution, Models::HRIS::BenefitCreateParams::CompanyContribution::Tier, Models::HRIS::BenefitFeaturesAndOperations, Models::HRIS::BenefitListParams, Models::HRIS::BenefitListSupportedBenefitsParams, Models::HRIS::BenefitRetrieveParams, Models::HRIS::BenefitUpdateParams, Models::HRIS::Benefits::EnrolledIndividualBenefitResponse, Models::HRIS::Benefits::IndividualBenefit, Models::HRIS::Benefits::IndividualBenefit::Body::BatchError, Models::HRIS::Benefits::IndividualBenefit::Body::UnionMember0, Models::HRIS::Benefits::IndividualEnrollManyParams, Models::HRIS::Benefits::IndividualEnrollManyParams::Individual, Models::HRIS::Benefits::IndividualEnrollManyParams::Individual::Configuration, Models::HRIS::Benefits::IndividualEnrollManyParams::Individual::Configuration::CompanyContribution, Models::HRIS::Benefits::IndividualEnrollManyParams::Individual::Configuration::EmployeeDeduction, Models::HRIS::Benefits::IndividualEnrolledIDsParams, Models::HRIS::Benefits::IndividualEnrolledIDsResponse, Models::HRIS::Benefits::IndividualRetrieveManyBenefitsParams, Models::HRIS::Benefits::IndividualUnenrollManyParams, Models::HRIS::Benefits::UnenrolledIndividualBenefitResponse, Models::HRIS::BenefitsSupport, Models::HRIS::Company::PayStatementItem::RuleCreateParams, Models::HRIS::Company::PayStatementItem::RuleCreateParams::Attributes, Models::HRIS::Company::PayStatementItem::RuleCreateParams::Condition, Models::HRIS::Company::PayStatementItem::RuleCreateResponse, Models::HRIS::Company::PayStatementItem::RuleCreateResponse::Attributes, Models::HRIS::Company::PayStatementItem::RuleCreateResponse::Condition, Models::HRIS::Company::PayStatementItem::RuleDeleteParams, Models::HRIS::Company::PayStatementItem::RuleDeleteResponse, Models::HRIS::Company::PayStatementItem::RuleDeleteResponse::Attributes, Models::HRIS::Company::PayStatementItem::RuleDeleteResponse::Condition, Models::HRIS::Company::PayStatementItem::RuleListParams, Models::HRIS::Company::PayStatementItem::RuleListResponse, Models::HRIS::Company::PayStatementItem::RuleListResponse::Attributes, Models::HRIS::Company::PayStatementItem::RuleListResponse::Condition, Models::HRIS::Company::PayStatementItem::RuleUpdateParams, Models::HRIS::Company::PayStatementItem::RuleUpdateResponse, Models::HRIS::Company::PayStatementItem::RuleUpdateResponse::Attributes, Models::HRIS::Company::PayStatementItem::RuleUpdateResponse::Condition, Models::HRIS::Company::PayStatementItemListParams, Models::HRIS::Company::PayStatementItemListResponse, Models::HRIS::Company::PayStatementItemListResponse::Attributes, Models::HRIS::CompanyBenefit, Models::HRIS::CompanyBenefit::CompanyContribution, Models::HRIS::CompanyBenefit::CompanyContribution::Tier, Models::HRIS::CompanyRetrieveParams, Models::HRIS::CreateCompanyBenefitsResponse, Models::HRIS::DirectoryListIndividualsParams, Models::HRIS::DirectoryListParams, Models::HRIS::DocumentListParams, Models::HRIS::DocumentListResponse, Models::HRIS::DocumentResponse, Models::HRIS::DocumentRetreiveParams, Models::HRIS::EmploymentData::BatchError, Models::HRIS::EmploymentData::UnionMember0, Models::HRIS::EmploymentData::UnionMember0::CustomField, Models::HRIS::EmploymentData::UnionMember0::Department, Models::HRIS::EmploymentData::UnionMember0::Employment, Models::HRIS::EmploymentData::UnionMember0::Manager, Models::HRIS::EmploymentDataResponse, Models::HRIS::EmploymentRetrieveManyParams, Models::HRIS::EmploymentRetrieveManyParams::Request, Models::HRIS::HRISCompany, Models::HRIS::HRISCompany::Account, Models::HRIS::HRISCompany::Department, Models::HRIS::HRISCompany::Department::Parent, Models::HRIS::HRISCompany::Entity, Models::HRIS::Individual::BatchError, Models::HRIS::Individual::UnionMember0, Models::HRIS::Individual::UnionMember0::Email, Models::HRIS::Individual::UnionMember0::PhoneNumber, Models::HRIS::IndividualInDirectory, Models::HRIS::IndividualInDirectory::Department, Models::HRIS::IndividualInDirectory::Manager, Models::HRIS::IndividualResponse, Models::HRIS::IndividualRetrieveManyParams, Models::HRIS::IndividualRetrieveManyParams::Options, Models::HRIS::IndividualRetrieveManyParams::Request, Models::HRIS::PayStatement, Models::HRIS::PayStatement::Earning, Models::HRIS::PayStatement::Earning::Attributes, Models::HRIS::PayStatement::Earning::Attributes::Metadata, Models::HRIS::PayStatement::EmployeeDeduction, Models::HRIS::PayStatement::EmployeeDeduction::Attributes, Models::HRIS::PayStatement::EmployeeDeduction::Attributes::Metadata, Models::HRIS::PayStatement::EmployerContribution, Models::HRIS::PayStatement::EmployerContribution::Attributes, Models::HRIS::PayStatement::EmployerContribution::Attributes::Metadata, Models::HRIS::PayStatement::Tax, Models::HRIS::PayStatement::Tax::Attributes, Models::HRIS::PayStatement::Tax::Attributes::Metadata, Models::HRIS::PayStatementDataSyncInProgress, Models::HRIS::PayStatementResponse, Models::HRIS::PayStatementResponse::Body::BatchError, Models::HRIS::PayStatementResponseBody, Models::HRIS::PayStatementResponseBody::Paging, Models::HRIS::PayStatementRetrieveManyParams, Models::HRIS::PayStatementRetrieveManyParams::Request, Models::HRIS::Payment, Models::HRIS::Payment::PayPeriod, Models::HRIS::PaymentListParams, Models::HRIS::SupportPerBenefitType, Models::HRIS::SupportedBenefit, Models::HRIS::UpdateCompanyBenefitResponse, Models::HRIS::W42005, Models::HRIS::W42005::Data, Models::HRIS::W42020, Models::HRIS::W42020::Data, Models::Income, Models::IndividualEvent::Data, Models::Introspection, Models::Introspection::AuthenticationMethod, Models::Introspection::AuthenticationMethod::ConnectionStatus, Models::Introspection::ConnectionStatus, Models::JobCompletionEvent::Data, Models::Jobs::AutomatedAsyncJob, Models::Jobs::AutomatedAsyncJob::Params, Models::Jobs::AutomatedCreateParams, Models::Jobs::AutomatedCreateParams::Params, Models::Jobs::AutomatedCreateResponse, Models::Jobs::AutomatedListParams, Models::Jobs::AutomatedListResponse, Models::Jobs::AutomatedListResponse::Meta, Models::Jobs::AutomatedListResponse::Meta::Quotas, Models::Jobs::AutomatedListResponse::Meta::Quotas::DataSyncAll, Models::Jobs::AutomatedRetrieveParams, Models::Jobs::ManualAsyncJob, Models::Jobs::ManualRetrieveParams, Models::Location, Models::Money, Models::OperationSupportMatrix, Models::Paging, Models::PayStatementEvent::Data, Models::PaymentEvent::Data, Models::Payroll::PayGroupListParams, Models::Payroll::PayGroupListResponse, Models::Payroll::PayGroupRetrieveParams, Models::Payroll::PayGroupRetrieveResponse, Models::Provider, Models::Provider::AuthenticationMethod, Models::Provider::AuthenticationMethod::SupportedFields, Models::Provider::AuthenticationMethod::SupportedFields::Company, Models::Provider::AuthenticationMethod::SupportedFields::Company::Accounts, Models::Provider::AuthenticationMethod::SupportedFields::Company::Departments, Models::Provider::AuthenticationMethod::SupportedFields::Company::Departments::Parent, Models::Provider::AuthenticationMethod::SupportedFields::Company::Entity, Models::Provider::AuthenticationMethod::SupportedFields::Company::Locations, Models::Provider::AuthenticationMethod::SupportedFields::Directory, Models::Provider::AuthenticationMethod::SupportedFields::Directory::Individuals, Models::Provider::AuthenticationMethod::SupportedFields::Directory::Individuals::Manager, Models::Provider::AuthenticationMethod::SupportedFields::Directory::Paging, Models::Provider::AuthenticationMethod::SupportedFields::Employment, Models::Provider::AuthenticationMethod::SupportedFields::Employment::Department, Models::Provider::AuthenticationMethod::SupportedFields::Employment::Employment, Models::Provider::AuthenticationMethod::SupportedFields::Employment::Income, Models::Provider::AuthenticationMethod::SupportedFields::Employment::Location, Models::Provider::AuthenticationMethod::SupportedFields::Employment::Manager, Models::Provider::AuthenticationMethod::SupportedFields::Individual, Models::Provider::AuthenticationMethod::SupportedFields::Individual::Emails, Models::Provider::AuthenticationMethod::SupportedFields::Individual::PhoneNumbers, Models::Provider::AuthenticationMethod::SupportedFields::Individual::Residence, Models::Provider::AuthenticationMethod::SupportedFields::PayGroup, Models::Provider::AuthenticationMethod::SupportedFields::PayStatement, Models::Provider::AuthenticationMethod::SupportedFields::PayStatement::Paging, Models::Provider::AuthenticationMethod::SupportedFields::PayStatement::PayStatements, Models::Provider::AuthenticationMethod::SupportedFields::PayStatement::PayStatements::Earnings, Models::Provider::AuthenticationMethod::SupportedFields::PayStatement::PayStatements::EmployeeDeductions, Models::Provider::AuthenticationMethod::SupportedFields::PayStatement::PayStatements::EmployerContributions, Models::Provider::AuthenticationMethod::SupportedFields::PayStatement::PayStatements::Taxes, Models::Provider::AuthenticationMethod::SupportedFields::Payment, Models::Provider::AuthenticationMethod::SupportedFields::Payment::PayPeriod, Models::ProviderListParams, Models::RequestForwardingForwardParams, Models::RequestForwardingForwardResponse, Models::RequestForwardingForwardResponse::Request, Models::Sandbox::CompanyUpdateParams, Models::Sandbox::CompanyUpdateParams::Account, Models::Sandbox::CompanyUpdateParams::Department, Models::Sandbox::CompanyUpdateParams::Department::Parent, Models::Sandbox::CompanyUpdateParams::Entity, Models::Sandbox::CompanyUpdateResponse, Models::Sandbox::CompanyUpdateResponse::Account, Models::Sandbox::CompanyUpdateResponse::Department, Models::Sandbox::CompanyUpdateResponse::Department::Parent, Models::Sandbox::CompanyUpdateResponse::Entity, Models::Sandbox::ConnectionCreateParams, Models::Sandbox::ConnectionCreateResponse, Models::Sandbox::Connections::AccountCreateParams, Models::Sandbox::Connections::AccountCreateResponse, Models::Sandbox::Connections::AccountUpdateParams, Models::Sandbox::Connections::AccountUpdateResponse, Models::Sandbox::DirectoryCreateParams, Models::Sandbox::DirectoryCreateParams::Body, Models::Sandbox::DirectoryCreateParams::Body::CustomField, Models::Sandbox::DirectoryCreateParams::Body::Department, Models::Sandbox::DirectoryCreateParams::Body::Email, Models::Sandbox::DirectoryCreateParams::Body::Employment, Models::Sandbox::DirectoryCreateParams::Body::Manager, Models::Sandbox::DirectoryCreateParams::Body::PhoneNumber, Models::Sandbox::EmploymentUpdateParams, Models::Sandbox::EmploymentUpdateParams::CustomField, Models::Sandbox::EmploymentUpdateParams::Department, Models::Sandbox::EmploymentUpdateParams::Employment, Models::Sandbox::EmploymentUpdateParams::Manager, Models::Sandbox::EmploymentUpdateResponse, Models::Sandbox::EmploymentUpdateResponse::CustomField, Models::Sandbox::EmploymentUpdateResponse::Department, Models::Sandbox::EmploymentUpdateResponse::Employment, Models::Sandbox::EmploymentUpdateResponse::Manager, Models::Sandbox::IndividualUpdateParams, Models::Sandbox::IndividualUpdateParams::Email, Models::Sandbox::IndividualUpdateParams::PhoneNumber, Models::Sandbox::IndividualUpdateResponse, Models::Sandbox::IndividualUpdateResponse::Email, Models::Sandbox::IndividualUpdateResponse::PhoneNumber, Models::Sandbox::JobCreateParams, Models::Sandbox::JobCreateResponse, Models::Sandbox::Jobs::ConfigurationRetrieveParams, Models::Sandbox::Jobs::SandboxJobConfiguration, Models::Sandbox::PaymentCreateParams, Models::Sandbox::PaymentCreateParams::PayStatement, Models::Sandbox::PaymentCreateParams::PayStatement::Earning, Models::Sandbox::PaymentCreateParams::PayStatement::EmployeeDeduction, Models::Sandbox::PaymentCreateParams::PayStatement::EmployerContribution, Models::Sandbox::PaymentCreateParams::PayStatement::Tax, Models::Sandbox::PaymentCreateResponse, 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: {})


483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/finch_api/internal/type/base_model.rb', line 483

def initialize(data = {})
  @data = {}
  @coerced = {}
  FinchAPI::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:



214
215
216
# File 'lib/finch_api/internal/type/base_model.rb', line 214

def ==(other)
  other.is_a?(Class) && other <= FinchAPI::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 (FinchAPI::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)


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
308
309
310
311
312
313
314
# File 'lib/finch_api/internal/type/base_model.rb', line 254

def coerce(value, state:)
  exactness = state.fetch(:exactness)

  if value.is_a?(self)
    exactness[:yes] += 1
    return value
  end

  unless (val = FinchAPI::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 == FinchAPI::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 = FinchAPI::Internal::Type::Converter.coerce(target, item, state: state)
        case target
        in FinchAPI::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)


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
353
354
355
356
# File 'lib/finch_api/internal/type/base_model.rb', line 325

def dump(value, state:)
  unless (coerced = FinchAPI::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, FinchAPI::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 == FinchAPI::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/finch_api/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)


221
# File 'lib/finch_api/internal/type/base_model.rb', line 221

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/finch_api/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)


502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/finch_api/internal/type/base_model.rb', line 502

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 = [
      FinchAPI::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/finch_api/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, FinchAPI::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?”



180
181
182
# File 'lib/finch_api/internal/type/base_model.rb', line 180

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})


373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/finch_api/internal/type/base_model.rb', line 373

def recursively_to_h(model, convert:)
  rec = ->(x) do
    case x
    in FinchAPI::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 FinchAPI::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, FinchAPI::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?”



161
162
163
# File 'lib/finch_api/internal/type/base_model.rb', line 161

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)


361
362
363
# File 'lib/finch_api/internal/type/base_model.rb', line 361

def to_sorbet_type
  self
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



229
# File 'lib/finch_api/internal/type/base_model.rb', line 229

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)


411
412
413
414
415
416
417
# File 'lib/finch_api/internal/type/base_model.rb', line 411

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:

# `operation_support_matrix` is a `FinchAPI::OperationSupportMatrix`
operation_support_matrix => {
  create: create,
  delete: delete,
  read: read
}

Parameters:

  • keys (Array<Symbol>, nil)

Returns:

  • (Hash{Symbol=>Object})


454
455
456
457
458
459
460
461
462
463
464
# File 'lib/finch_api/internal/type/base_model.rb', line 454

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})


441
# File 'lib/finch_api/internal/type/base_model.rb', line 441

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

#hashInteger

Returns:

  • (Integer)


234
# File 'lib/finch_api/internal/type/base_model.rb', line 234

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)


527
528
529
530
# File 'lib/finch_api/internal/type/base_model.rb', line 527

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})


431
# File 'lib/finch_api/internal/type/base_model.rb', line 431

def to_h = @data

#to_json(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


471
# File 'lib/finch_api/internal/type/base_model.rb', line 471

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

#to_sString

Returns:

  • (String)


522
# File 'lib/finch_api/internal/type/base_model.rb', line 522

def to_s = deep_to_h.to_s

#to_yaml(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


478
# File 'lib/finch_api/internal/type/base_model.rb', line 478

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