Class: Sink::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/sink/base_model.rb

Direct Known Subclasses

Models::APIStatus, Models::Address, Models::AllofBaseParent, Models::ArrayMissingItemsResponse, Models::ArrayObjectItem, Models::ArrayObjectItemsResponse, Models::Balance, Models::BasicSharedModelObject, Models::BodyParamTopLevelAllOfResponse, Models::BodyParamUnionOverlappingPropResponse, Models::Card, Models::CardListResponse, Models::CardProvisionFooResponse, Models::ChildInlinedResponseResponse, Models::ChildModel, Models::ChildModel::InlineObject, Models::Class, Models::Client, Models::Company, Models::CompanyPayment, Models::ComplexQueryArrayQueryResponse, Models::ConfigToolModelRefFromNestedResponseBodyResponse, Models::ConfigToolModelRefFromNestedResponseBodyResponse::Foo, Models::DecoratorTestKeepMeResponse, Models::DocstringLeadingDoubleQuoteResponse, Models::DocstringTrailingDoubleQuoteResponse, Models::Documents, Models::EEOC, Models::EmploymentData, Models::EnumBasicResponse, Models::EnvelopeInlineResponseResponse, Models::Export, Models::FileCreateMultipartResponse, Models::FileEverythingMultipartResponse, Models::FileNoFileMultipartResponse, Models::FileWithOptionalParamResponse, Models::FundingAccount, Models::Import, Models::Interface, Models::KeepThisResourceKeepThisMethodResponse, Models::MakeAmbiguousSchemasExplicitMakeAmbiguousSchemasExplicitResponse, Models::MakeAmbiguousSchemasLooserMakeAmbiguousSchemasLooserResponse, Models::MapNullableItemsResponse, Models::MethodConfigSkippedTestsAllResponse, Models::MethodConfigSkippedTestsGoResponse, Models::MethodConfigSkippedTestsJavaResponse, Models::MethodConfigSkippedTestsKotlinResponse, Models::MethodConfigSkippedTestsNodeAndPythonResponse, Models::MethodConfigSkippedTestsNodeResponse, Models::MethodConfigSkippedTestsPythonResponse, Models::MethodConfigSkippedTestsRubyResponse, Models::ModelFromNestedPath, Models::ModelFromNestedResponseBodyRef, Models::ModelFromSchemasRef, Models::ModelFromSchemasRefOpenAPIUri, Models::ModelFromSchemasRefOpenAPIUriJmespath, Models::ModelFromSchemasRefOpenAPIUriJsonpath, Models::ModelLevel1, Models::ModelLevel2, Models::ModelLevel3, Models::ModelReferencedInParentAndChild, Models::ModelWithNestedModel, Models::MyModel, Models::NameChildPropImportClashResponse, Models::NameChildPropImportClashResponse::Balance, Models::NamePropertiesCommonConflictsResponse, Models::NamePropertiesIllegalGoIdentifiersResponse, Models::NameResponsePropertyClashesModelImportResponse, Models::NameResponseShadowsPydanticResponse, Models::NestedRequestModelA, Models::NestedRequestModelB, Models::NestedRequestModelC, Models::NestedRequestModelC::Baz, Models::ObjectMissingItemsResponse, Models::ObjectMixedKnownAndUnknownResponse, Models::ObjectMultipleArrayPropertiesSameRefResponse, Models::ObjectMultipleArrayPropertiesSameRefResponse::Bar, Models::ObjectMultipleArrayPropertiesSameRefResponse::Foo, Models::ObjectMultipleArrayPropertiesSameRefResponse::RequiredProp, Models::ObjectMultiplePropertiesSameModelResponse, Models::ObjectMultiplePropertiesSameRefResponse, Models::ObjectMultiplePropertiesSameRefResponse::Bar, Models::ObjectMultiplePropertiesSameRefResponse::Foo, Models::ObjectMultiplePropertiesSameRefResponse::RequiredProp, Models::ObjectSkippedProps, Models::ObjectTwoDimensionalArrayPrimitivePropertyResponse, Models::ObjectWithAnyOfNullProperty, Models::ObjectWithAnyOfNullProperty::Foo, Models::ObjectWithChildRef, Models::ObjectWithOneOfNullProperty, Models::ObjectWithOneOfNullProperty::Foo, Models::ObjectWithUnionProperties, Models::OpenAPIFormatArrayTypeOneEntryResponse, Models::OpenAPIFormatArrayTypeOneEntryWithNullResponse, Models::OpenAPISpecialUsedUsedAsPropertyNameResponse, Models::PageCursorSharedRefPagination, Models::ParentModelWithChildRef, Models::PathParamColonSuffixResponse, Models::PathParamFileExtensionResponse, Models::PathParamMultipleResponse, Models::PathParamQueryParamResponse, Models::PathParamSingularResponse, Models::PrimitiveStringsResponse, Models::Private, Models::Public, Models::RecursionCreateEnvelopeResponse, Models::ResponseAllofSimpleResponse, Models::ResponseMissingRequiredResponse, Models::ResponseNestedArrayResponse, Models::ResponseNestedArrayResponse::Object, Models::ResponseObjectAllPropertiesResponse, Models::ResponseObjectNoPropertiesResponse, Models::ResponseObjectWithAdditionalPropertiesPropResponse, Models::ResponseObjectWithAdditionalPropertiesPropResponse::Foo, Models::ResponseObjectWithHeavilyNestedUnionResponse, Models::ResponseOnlyReadOnlyPropertiesResponse, Models::ResponsesAllofCrossObject, Models::Return, Models::RootResponse, Models::SelfRecursion, Models::SharedCursorNestedResponsePropMeta, Models::SharedCursorNestedResponsePropMeta::Pagination, Models::SharedSelfRecursion, Models::ShippingAddress, Models::SimpleAllof, Models::SimpleObject, Models::SkipThisResourceINeverAppearResponse, Models::StreamingBasicResponse, Models::StreamingNestedParamsResponse, Models::StreamingNoDiscriminatorResponse, Models::StreamingQueryParamDiscriminatorResponse, Models::StreamingWithUnrelatedDefaultParamResponse, Models::TypeDatesResponse, Models::TypeDatetimesResponse, Models::UnionDiscriminatedVariantA, Models::UnionDiscriminatedVariantB, Models::Version1_30NameCreateResponse, Models::Widget, Models::WriteOnlyResponseSimpleResponse, PageCursorNestedItems::Data, PageCursorNestedItems::ObjectProp, PageCursorNestedObjectRef::ObjectProp, PageCursorSharedRef::PageCursorSharedRefPagination

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ BaseModel

Create a new instance of a model.



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/sink/base_model.rb', line 150

def initialize(data = {})
  @data = {}
  # TODO: what if data isn't a hash?
  data.each do |field_name, value|
    next if value.nil?

    field = self.class.fields[field_name.to_sym]
    if field
      next if field[:mode] == :w
    end
    @data[field_name.to_sym] = value
  end
end

Instance Method Details

#[](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.



188
189
190
191
192
193
# File 'lib/sink/base_model.rb', line 188

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

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



197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/sink/base_model.rb', line 197

def deconstruct_keys(keys)
  (keys || self.class.fields.keys).to_h do |k|
    if !k.instance_of?(Symbol)
      raise ArgumentError, "Expected symbol key for lookup, got #{k.inspect}"
    end

    if !self.class.fields.key?(k)
      raise KeyError, "Expected one of #{self.class.fields.keys}, got #{k.inspect}"
    end

    [k, method(k).call]
  end
end

#inspectString



212
213
214
215
216
# File 'lib/sink/base_model.rb', line 212

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} #{deconstruct_keys(nil).map do |k, v|
    "#{k}=#{v.inspect}"
  end.join(' ')}>"
end

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

Returns a Hash of the data underlying this object. 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.



173
174
175
# File 'lib/sink/base_model.rb', line 173

def to_h
  @data
end

#to_sString



219
220
221
# File 'lib/sink/base_model.rb', line 219

def to_s
  @data.to_s
end