Class: DeliveryFivePostClient::Internal::Type::BaseModel Abstract
- Inherits:
-
Object
- Object
- DeliveryFivePostClient::Internal::Type::BaseModel
- Extended by:
- Converter, Util::SorbetRuntimeSupport
- Defined in:
- lib/delivery_five_post_client/internal/type/base_model.rb
Overview
Direct Known Subclasses
Models::API::V1::Address, Models::API::V1::Cargo, Models::API::V1::ClientInfo, Models::API::V1::GetLabelStatusBySenderOrderIDsParams, Models::API::V1::GetLabelStatusBySenderOrderIDsResponse, Models::API::V1::GetLabelStatusBySenderOrderIDsResponse::LabelStatus, Models::API::V1::OrderCost, Models::API::V1::OrderCreate, Models::API::V1::OrderCreate::Warehouse, Models::API::V1::OrderCreateResponse, Models::API::V1::OrderStatusRequest, Models::API::V1::PartnerDeliveryParamsParams, Models::API::V1::PartnerDeliveryParamsResponse, Models::API::V1::PartnerDeliveryParamsResponse::Zone, Models::API::V1::PartnerDeliveryToAddressParams, Models::API::V1::PartnerDeliveryToAddressParams::Dimensions, Models::API::V1::PartnerDeliveryToAddressResponse, Models::API::V1::PartnerGetActualCargoSizeParams, Models::API::V1::PartnerGetActualCargoSizeResponse, Models::API::V1::PartnerGetCargoStatusParams, Models::API::V1::PartnerGetCargoStatusResponse, Models::API::V1::PartnerGetCargoStatusResponse::Cargo, Models::API::V1::PartnerGetOrderHistoryMassResponse, Models::API::V1::PartnerGetOrderHistoryMassResponse::Order, Models::API::V1::PartnerGetOrderHistoryMassResponse::Order::History, Models::API::V1::PartnerGetOrderStatusResponse, Models::API::V1::PartnerGetOrderStatusResponse::Order, Models::API::V1::PartnerGetReceiptsParams, Models::API::V1::PartnerGetReceiptsResponse, Models::API::V1::PartnerGetReceiptsResponse::Receipt, Models::API::V1::PartnerGetReceiptsResponse::Receipt::Receipt, Models::API::V1::PartnerGetReceiptsResponse::Receipt::Receipt::FiscalData, Models::API::V1::PartnerGetReceiverLocationsParams, Models::API::V1::PartnerGetReceiverLocationsResponse, Models::API::V1::PartnerGetReceiverLocationsResponse::Redirection, Models::API::V1::PartnerGetSecurityCodeParams, Models::API::V1::PartnerGetSecurityCodeResponse, Models::API::V1::PartnerGetSecurityCodeResponse::SecurityCode, Models::API::V1::PartnerOrderLabelsParams, Models::API::V1::PartnerOrderLabelsResponse, Models::API::V1::PartnerOrderLabelsResponse::Label, Models::API::V1::PartnerPickupPointsParams, Models::API::V1::PartnerPickupPointsResponse, Models::API::V1::PartnerRetrieveParams, Models::API::V1::PartnerRetrieveResponse, Models::API::V1::PartnerRetrieveWarehouseAllParams, Models::API::V1::PartnerRetrieveWarehouseAllResponse, Models::API::V1::Partners::Contacts, Models::API::V1::Partners::ExtendStorageDayExtendStorageDaysParams, Models::API::V1::Partners::ExtendStorageDayExtendStorageDaysResponse, Models::API::V1::Partners::JwtGenerateParams, Models::API::V1::Partners::JwtGenerateResponse, Models::API::V1::Partners::OrderC2cParams, Models::API::V1::Partners::OrderC2cResponse, Models::API::V1::Partners::OrderC2cResponse::Cargo, Models::API::V1::Partners::OrderUpdateClientInfoParams, Models::API::V1::Partners::OrderUpdateClientInfoParams::Client, Models::API::V1::Partners::OrderUpdatePaymentValueParams, Models::API::V1::Partners::Orders::CargoCreateParams, Models::API::V1::Partners::Orders::CargoCreatedInfo, Models::API::V1::Partners::Orders::CargoDeleteParams, Models::API::V1::Partners::Orders::CargoUpdateParams, Models::API::V1::Partners::Orders::ProductValue, Models::API::V1::Partners::Orders::VendorInfo, Models::API::V1::Partners::PartnerSupplyCreateParams, Models::API::V1::Partners::PartnerSupplyDeleteParams, Models::API::V1::Partners::PartnerSupplyDeleteResponse, Models::API::V1::Partners::PartnerSupplyRetrieveParams, Models::API::V1::Partners::PartnerSupplyRetrievePartnerSupplyParams, Models::API::V1::Partners::PartnerSupplyRetrievePartnerSupplyResponse, Models::API::V1::Partners::PartnerSupplyUpdateParams, Models::API::V1::Partners::RedirectionCreateParams, Models::API::V1::Partners::RedirectionCreateResponse, Models::API::V1::Partners::ReturnRetrieveGetPlacementCodeParams, Models::API::V1::Partners::ReturnRetrieveGetPlacementCodeResponse, Models::API::V1::Partners::ReturnRetrieveGetReturnBarcodeParams, Models::API::V1::Partners::ReturnRetrieveGetReturnBarcodeResponse, Models::API::V1::Partners::SupplyCargo, Models::API::V1::Partners::SupplyInfo, Models::API::V1::Partners::TaskStatusRequest, Models::API::V1::Partners::TaskStatusResponse, Models::API::V1::Partners::Tasks::Report::AvdCreateParams, Models::API::V1::Partners::Tasks::Report::AvdCreateResponse, Models::API::V1::Partners::Tasks::Report::AvdResultParams, Models::API::V1::Partners::Tasks::Report::AvdStatusParams, Models::API::V1::Partners::Tasks::Report::AvdStatusResponse, Models::API::V1::Partners::WarehouseCreateParams, Models::API::V1::Partners::WarehouseCreateResponse, Models::API::V1::Partners::WarehouseInfo, Models::API::V1::Partners::WarehouseRetrieveParams, Models::API::V1::Partners::WarehouseUpdateParams, Models::API::V1::Partners::WarehouseUpdateParams::Address, Models::API::V1::Partners::WarehouseUpdateParams::Contacts, Models::API::V1::ReceiverLocation, Models::API::V1::TariffC2cParams, Models::API::V1::TariffC2cResponse, Models::API::V2::PartnerCreateReturnOrderParams, Models::API::V2::PartnerCreateReturnOrderParams::BaseOrder, Models::API::V2::PartnerCreateReturnOrderParams::Warehouse, Models::API::V2::PartnerCreateReturnOrderResponse, Models::API::V2::PartnerCreateReturnOrderResponse::Cargo, Models::API::V2::PickupPoint, Models::API::V2::PickupPoint::LastMileWarehouse, Models::API::V2::PickupPoint::Rate, Models::API::V2::PointGetC2cPickupPointsParams, Models::API::V2::PointGetC2cPlacementPointsParams, Models::API::V2::Points::SlGetC2cDeliveryTermsParams, Models::API::V2::Points::SlGetC2cDeliveryTermsResponse, Models::API::V3::Partners::OrderCancelOrderParams, Models::WebhookCreateCargoStatusParams, Models::WebhookCreateOrderStatusParams, RequestOptions
Class Method Summary collapse
- .==(other) ⇒ Boolean
- .coerce(value, state:) ⇒ self, Object private
- .dump(value, state:) ⇒ Hash{Object=>Object}, Object private
- .fields ⇒ Hash{Symbol=>Hash{Symbol=>Object}} private
- .hash ⇒ Integer
-
.inherited(child) ⇒ Object
private
Assumes superclass fields are totally defined before fields are accessed / defined on subclasses.
- .inspect(depth: 0) ⇒ String private
- .known_fields ⇒ Hash{Symbol=>Hash{Symbol=>Object}} private
- .optional(name_sym, type_info, spec = {}) ⇒ Object private
- .recursively_to_h(model, convert:) ⇒ Hash{Symbol=>Object} private
- .required(name_sym, type_info, spec = {}) ⇒ Object private
- .to_sorbet_type ⇒ Object private
Instance Method Summary collapse
- #==(other) ⇒ Boolean
-
#[](key) ⇒ Object?
Returns the raw value associated with the given key, if found.
- #deconstruct_keys(keys) ⇒ Hash{Symbol=>Object}
-
#deep_to_h ⇒ Hash{Symbol=>Object}
In addition to the behaviour of
#to_h, this method will recursively call#to_hon nested models. - #hash ⇒ Integer
-
#initialize(data = {}) ⇒ BaseModel
constructor
Create a new instance of a model.
- #inspect ⇒ String private
-
#to_h ⇒ Hash{Symbol=>Object}
(also: #to_hash)
Returns a Hash of the data underlying this object.
- #to_json(*a) ⇒ String
- #to_s ⇒ String
- #to_yaml(*a) ⇒ String
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.
486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 486 def initialize(data = {}) @data = {} @coerced = {} DeliveryFivePostClient::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
210 211 212 213 214 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 210 def ==(other) # rubocop:disable Layout/LineLength other.is_a?(Class) && other <= DeliveryFivePostClient::Internal::Type::BaseModel && other.fields == fields # rubocop:enable Layout/LineLength 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.
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 308 309 310 311 312 313 314 315 316 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 252 def coerce(value, state:) exactness = state.fetch(:exactness) if value.is_a?(self) exactness[:yes] += 1 return value end unless (val = DeliveryFivePostClient::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 == DeliveryFivePostClient::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 = DeliveryFivePostClient::Internal::Type::Converter.coerce( target, item, state: state ) case target in DeliveryFivePostClient::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.
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 357 358 359 360 361 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 327 def dump(value, state:) unless (coerced = DeliveryFivePostClient::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, DeliveryFivePostClient::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 == DeliveryFivePostClient::Internal::OMIT acc.store(api_name, const) end acc end |
.fields ⇒ Hash{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.
31 32 33 34 35 |
# File 'lib/delivery_five_post_client/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 |
.hash ⇒ Integer
219 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 219 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.
18 19 20 21 |
# File 'lib/delivery_five_post_client/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.
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 505 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 = [ DeliveryFivePostClient::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_fields ⇒ Hash{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.
26 |
# File 'lib/delivery_five_post_client/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.
176 177 178 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 176 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.
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 378 def recursively_to_h(model, convert:) rec = ->(x) do case x in DeliveryFivePostClient::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 DeliveryFivePostClient::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.
157 158 159 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 157 def required(name_sym, type_info, spec = {}) add_field(name_sym, required: true, type_info: type_info, spec: spec) end |
.to_sorbet_type ⇒ 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.
366 367 368 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 366 def to_sorbet_type self end |
Instance Method Details
#==(other) ⇒ Boolean
227 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 227 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.
416 417 418 419 420 421 422 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 416 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}
457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 457 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_h ⇒ Hash{Symbol=>Object}
In addition to the behaviour of #to_h, this method will recursively call
#to_h on nested models.
446 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 446 def deep_to_h = self.class.recursively_to_h(@data, convert: false) |
#hash ⇒ Integer
232 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 232 def hash = [self.class, @data].hash |
#inspect ⇒ 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.
530 531 532 533 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 530 def inspect converted = self.class.recursively_to_h(self, convert: true) "#<#{self.class}:0x#{object_id.to_s(16)} #{converted}>" end |
#to_h ⇒ Hash{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.
436 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 436 def to_h = @data |
#to_json(*a) ⇒ String
474 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 474 def to_json(*a) = DeliveryFivePostClient::Internal::Type::Converter.dump(self.class, self).to_json(*a) |
#to_s ⇒ String
525 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 525 def to_s = deep_to_h.to_s |
#to_yaml(*a) ⇒ String
481 |
# File 'lib/delivery_five_post_client/internal/type/base_model.rb', line 481 def to_yaml(*a) = DeliveryFivePostClient::Internal::Type::Converter.dump(self.class, self).to_yaml(*a) |