Module: Dolibarr::Api::Serializable

Included in:
Models::BankaccountsAddLineModel, Models::BankaccountsAddLinkModel, Models::BankaccountsTransferModel, Models::BankaccountsUpdateLineModel, Models::ContactsCreateUserModel, Models::CreateBankaccountsModel, Models::CreateContactsModel, Models::CreateDocumentsModel, Models::CreateEmailtemplatesModel, Models::CreateInvoicesModel, Models::CreateProductsModel, Models::CreateProposalsModel, Models::CreateSupplierinvoicesModel, Models::CreateSupplierordersModel, Models::CreateThirdpartiesModel, Models::CreateUsersModel, Models::DocumentsBuilddocModel, Models::EmailtemplatesUpdateByIdModel, Models::EmailtemplatesUpdatebyLabelModel, Models::InvoicesAddContactModel, Models::InvoicesAddPaymentDistributedModel, Models::InvoicesAddPaymentModel, Models::InvoicesCreateContactModel, Models::InvoicesCreateLineModel, Models::InvoicesSettodraftModel, Models::InvoicesSettopaidModel, Models::InvoicesUpdateLineModel, Models::InvoicesUpdatePaymentModel, Models::InvoicesValidateModel, Models::ListLoginModel, Models::Obj, Models::ObjectlinksCreateModel, Models::ProductsAddAttributeValueModel, Models::ProductsAddAttributesModel, Models::ProductsAddPurchasePriceModel, Models::ProductsAddSubproductsModel, Models::ProductsAddVariantByProductRefModel, Models::ProductsAddVariantModel, Models::ProductsCreateContactModel, Models::ProductsUpdateAttributeValueModel, Models::ProductsUpdateAttributesModel, Models::ProductsUpdateVariantModel, Models::ProposalsCloseModel, Models::ProposalsCreateContactModel, Models::ProposalsCreateLineModel, Models::ProposalsCreateLinesModel, Models::ProposalsUpdateLineModel, Models::ProposalsValidateModel, Models::SetupCreateExtrafieldsModel, Models::SetupUpdateExtrafieldsModel, Models::SupplierinvoicesAddPaymentModel, Models::SupplierinvoicesCreateLineModel, Models::SupplierinvoicesSettodraftModel, Models::SupplierinvoicesSettopaidModel, Models::SupplierinvoicesUpdateLineModel, Models::SupplierinvoicesValidateModel, Models::SupplierordersApproveModel, Models::SupplierordersCreateLineModel, Models::SupplierordersMakeOrderModel, Models::SupplierordersReceiveOrderModel, Models::SupplierordersValidateModel, Models::ThirdpartiesCreateCompanyBankAccountModel, Models::ThirdpartiesCreateCompanyNotificationByCodeModel, Models::ThirdpartiesCreateCompanyNotificationModel, Models::ThirdpartiesCreateSocieteAccountModel, Models::ThirdpartiesSplitdiscountModel, Models::ThirdpartiesUpdateCompanyBankAccountModel, Models::ThirdpartiesUpdateCompanyNotificationModel, Models::ThirdpartiesUpdateSocieteAccountModel, Models::UpdateBankaccountsModel, Models::UpdateContactsModel, Models::UpdateInvoicesModel, Models::UpdatePaiementsModel, Models::UpdateProductsModel, Models::UpdateProposalsModel, Models::UpdateSupplierinvoicesModel, Models::UpdateSupplierordersModel, Models::UpdateThirdpartiesModel, Models::UpdateUsersModel, Models::UsersCreateGroupsModel, Models::UsersCreateUserNotificationByCodeModel, Models::UsersCreateUserNotificationModel, Models::UsersUpdateGroupsModel, Models::UsersUpdateUserNotificationModel
Defined in:
lib/dolibarr-api/serializable.rb

Overview

Shared serialization for all models. Models declare their attributes via the attribute DSL (see Validations); this reads the resulting attribute table.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.serialize(value) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/dolibarr-api/serializable.rb', line 42

def self.serialize(value)
  case value
  when Array then value.map { |v| serialize(v) }
  when Hash  then value.transform_values { |v| serialize(v) }
  else value.respond_to?(:to_hash) ? value.to_hash : value
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



33
34
35
# File 'lib/dolibarr-api/serializable.rb', line 33

def ==(other)
  other.is_a?(self.class) && to_hash == other.to_hash
end

#hashObject



38
39
40
# File 'lib/dolibarr-api/serializable.rb', line 38

def hash
  to_hash.hash
end

#to_bodyObject



21
22
23
# File 'lib/dolibarr-api/serializable.rb', line 21

def to_body
  to_hash
end

#to_hashObject



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dolibarr-api/serializable.rb', line 7

def to_hash
  result = self.class.openapi_attributes.each_with_object({}) do |(name, attr), hash|
    value = public_send(name)
    next if value.nil? && !attr[:required]

    hash[attr[:json_key]] = Serializable.serialize(value)
  end
  if respond_to?(:additional_properties) && additional_properties && !additional_properties.empty?
    # A declared attribute always wins over an overflow key of the same name.
    result.merge!(Serializable.serialize(additional_properties)) { |_key, declared, _extra| declared }
  end
  result
end

#to_json(*args) ⇒ Object



25
26
27
# File 'lib/dolibarr-api/serializable.rb', line 25

def to_json(*args)
  to_hash.to_json(*args)
end

#to_sObject



29
30
31
# File 'lib/dolibarr-api/serializable.rb', line 29

def to_s
  to_hash.to_s
end