Class: DearInventory::Model

Inherits:
Object
  • Object
show all
Extended by:
IsASubclass, T::Helpers, T::Sig
Defined in:
lib/dear_inventory/model.rb

Direct Known Subclasses

DearInventory::Models::AdditionalAttributes, DearInventory::Models::Address, DearInventory::Models::AdvancedPurchase, DearInventory::Models::Attachment, DearInventory::Models::BillOfMaterialsProduct, DearInventory::Models::BillOfMaterialsService, DearInventory::Models::Customer, DearInventory::Models::Customers, DearInventory::Models::Customers::Address, DearInventory::Models::Customers::Contact, DearInventory::Models::InventoryMovement, DearInventory::Models::Product, DearInventory::Models::ProductAvailability, DearInventory::Models::ProductAvailabilityResults, DearInventory::Models::Products, DearInventory::Models::Products::Movement, DearInventory::Models::Purchase, DearInventory::Models::Purchases, DearInventory::Models::Purchases::AdditionalCharge, DearInventory::Models::Purchases::Advanced::Invoice, DearInventory::Models::Purchases::Advanced::PutAway, DearInventory::Models::Purchases::Advanced::Stock, DearInventory::Models::Purchases::Advanced::StockLine, DearInventory::Models::Purchases::CreditNote, DearInventory::Models::Purchases::InventoryMovement, DearInventory::Models::Purchases::Invoice, DearInventory::Models::Purchases::Line, DearInventory::Models::Purchases::ManualJournal, DearInventory::Models::Purchases::ManualJournalLine, DearInventory::Models::Purchases::Order, DearInventory::Models::Purchases::PaymentLine, DearInventory::Models::Purchases::Stock, DearInventory::Models::Purchases::StockLine, DearInventory::Models::Purchases::UnstockLine, DearInventory::Models::PurchasesResults, DearInventory::Models::ReorderLevel, DearInventory::Models::Request, DearInventory::Models::Sale, DearInventory::Models::Sales, DearInventory::Models::Sales::AdditionalCharge, DearInventory::Models::Sales::CreditNote, DearInventory::Models::Sales::Fulfilment, DearInventory::Models::Sales::Fulfilments::PickPack, DearInventory::Models::Sales::Fulfilments::PickPackLine, DearInventory::Models::Sales::Fulfilments::Ship, DearInventory::Models::Sales::Fulfilments::ShipLine, DearInventory::Models::Sales::Invoice, DearInventory::Models::Sales::Line, DearInventory::Models::Sales::ManualJournal, DearInventory::Models::Sales::ManualJournalLine, DearInventory::Models::Sales::Order, DearInventory::Models::Sales::PaymentLine, DearInventory::Models::Sales::Quote, DearInventory::Models::SalesResults, DearInventory::Models::Supplier, DearInventory::Models::Transaction

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IsASubclass

is_a?

Constructor Details

#initialize(values = nil) ⇒ Model

Returns a new instance of Model.



56
57
58
59
60
61
62
63
64
65
# File 'lib/dear_inventory/model.rb', line 56

def initialize(values = nil)
  initialize_abstract

  return if values.nil?

  self.class.enumerate_fields do |response_name, specifications|
    value = field_value(response_name, T.must(values), specifications)
    instance_variable_set(:"@#{specifications[:name]}", value)
  end
end

Class Method Details

.define_readersObject



34
35
36
37
38
39
40
41
42
# File 'lib/dear_inventory/model.rb', line 34

def self.define_readers
  enumerate_fields do |_, specifications|
    __send__(:attr_reader, specifications[:name])

    if specifications[:type] == :ResultSet
      alias_method :records, specifications[:name]
    end
  end
end

.enumerate_fieldsObject



45
46
47
48
49
# File 'lib/dear_inventory/model.rb', line 45

def self.enumerate_fields
  const_get(:FIELDS).each do |response_name, specifications|
    yield(response_name, specifications)
  end
end

.fields(fields) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/dear_inventory/model.rb', line 23

def self.fields(fields)
  ancestor = T.must(ancestors[1])
  if ancestor.const_defined?(:FIELDS)
    fields = ancestor.const_get(:FIELDS).merge(fields)
  end
  const_set(:FIELDS, fields.freeze)

  define_readers
end

Instance Method Details

#initialize_abstractObject



51
# File 'lib/dear_inventory/model.rb', line 51

alias initialize_abstract initialize

#to_hObject



68
69
70
71
72
73
74
75
# File 'lib/dear_inventory/model.rb', line 68

def to_h
  {}.tap do |hash|
    self.class.enumerate_fields do |_, specifications|
      key = specifications[:name]
      hash[key] = public_send(key)
    end
  end
end