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

Inherits:
Object
  • Object
show all
Extended by:
Converter
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::BenefitFeaturesAndOperations::SupportedFeatures, Models::HRIS::BenefitListParams, Models::HRIS::BenefitListSupportedBenefitsParams, Models::HRIS::BenefitListSupportedBenefitsResponse, Models::HRIS::BenefitRetrieveParams, Models::HRIS::BenefitUpdateParams, Models::HRIS::Benefits::EnrolledIndividualBenefitResponse, Models::HRIS::Benefits::IndividualBenefit, Models::HRIS::Benefits::IndividualBenefit::Body, 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::PayStatementResponse, Models::HRIS::PayStatementResponseBody, 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::Earning::Attributes, Models::Sandbox::PaymentCreateParams::PayStatement::Earning::Attributes::Metadata, Models::Sandbox::PaymentCreateParams::PayStatement::EmployeeDeduction, Models::Sandbox::PaymentCreateParams::PayStatement::EmployeeDeduction::Attributes, Models::Sandbox::PaymentCreateParams::PayStatement::EmployeeDeduction::Attributes::Metadata, Models::Sandbox::PaymentCreateParams::PayStatement::EmployerContribution, Models::Sandbox::PaymentCreateParams::PayStatement::EmployerContribution::Attributes, Models::Sandbox::PaymentCreateParams::PayStatement::EmployerContribution::Attributes::Metadata, Models::Sandbox::PaymentCreateParams::PayStatement::Tax, Models::Sandbox::PaymentCreateParams::PayStatement::Tax::Attributes, Models::Sandbox::PaymentCreateParams::PayStatement::Tax::Attributes::Metadata, Models::Sandbox::PaymentCreateResponse, RequestOptions

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Converter

coerce, dump, type_info

Constructor Details

#initialize(data = {}) ⇒ BaseModel

Create a new instance of a model.

Parameters:

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


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

def initialize(data = {}) = (@data = FinchAPI::Internal::Util.coerce_hash!(data).to_h)

Class Method Details

.==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



174
175
176
# File 'lib/finch_api/internal/type/base_model.rb', line 174

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

.coerce(value, state:) ⇒ FinchAPI::Internal::Type::BaseModel, 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, :strong] :strictness

    @option state [HashSymbol=>Object] :exactness

    @option state [Integer] :branched

Returns:



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/finch_api/internal/type/base_model.rb', line 210

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

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

  unless (val = FinchAPI::Internal::Util.coerce_hash(value)).is_a?(Hash)
    exactness[:no] += 1
    return value
  end
  exactness[:yes] += 1

  keys = val.keys.to_set
  instance = new
  data = instance.to_h

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

    unless val.key?(api_name)
      if required && mode != :dump && const == FinchAPI::Internal::OMIT
        exactness[nilable ? :maybe : :no] += 1
      else
        exactness[:yes] += 1
      end
      next
    end

    item = val.fetch(api_name)
    keys.delete(api_name)

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

Returns:

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


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
# File 'lib/finch_api/internal/type/base_model.rb', line 275

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


24
25
26
27
28
# File 'lib/finch_api/internal/type/base_model.rb', line 24

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

.hashInteger

Returns:

  • (Integer)


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

def hash = fields.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.

Parameters:

  • depth (Integer) (defaults to: 0)

Returns:

  • (String)


408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/finch_api/internal/type/base_model.rb', line 408

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.

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

Returns:

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


17
18
19
# File 'lib/finch_api/internal/type/base_model.rb', line 17

def known_fields
  @known_fields ||= (self < FinchAPI::Internal::Type::BaseModel ? superclass.known_fields.dup : {})
end

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



140
141
142
# File 'lib/finch_api/internal/type/base_model.rb', line 140

def optional(name_sym, type_info, spec = {})
  add_field(name_sym, required: false, type_info: type_info, spec: spec)
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?”



121
122
123
# File 'lib/finch_api/internal/type/base_model.rb', line 121

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

.walk(model) ⇒ 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})


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/finch_api/internal/type/base_model.rb', line 366

def walk(model)
  walk = ->(x) do
    case x
    in FinchAPI::Internal::Type::BaseModel
      walk.call(x.to_h)
    in Hash
      x.transform_values(&walk)
    in Array
      x.map(&walk)
    else
      x
    end
  end
  walk.call(model)
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:



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

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)


321
322
323
324
325
326
327
# File 'lib/finch_api/internal/type/base_model.rb', line 321

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}

Parameters:

  • keys (Array<Symbol>, nil)

Returns:

  • (Hash{Symbol=>Object})


348
349
350
351
352
353
354
355
356
357
358
# File 'lib/finch_api/internal/type/base_model.rb', line 348

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

#hashInteger

Returns:

  • (Integer)


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

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)


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

def inspect = "#<#{self.class}:0x#{object_id.to_s(16)} #{self}>"

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


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

def to_h = @data

#to_json(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


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

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

#to_sString

Returns:

  • (String)


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

def to_s = self.class.walk(@data).to_s

#to_yaml(*a) ⇒ String

Parameters:

  • a (Object)

Returns:

  • (String)


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

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