Class: LedgerSync::Resource

Inherits:
Object
  • Object
show all
Includes:
Fingerprintable::Mixin, Validatable, SimplySerializable::Mixin
Defined in:
lib/ledger_sync/resource.rb

Direct Known Subclasses

Customer, Invoice, Payment, Product, Vendor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validatable

#valid?, #validate, #validate_or_fail

Constructor Details

#initialize(external_id: nil, ledger_id: nil, sync_token: nil, **data) ⇒ Resource

Returns a new instance of Resource.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ledger_sync/resource.rb', line 20

def initialize(external_id: nil, ledger_id: nil, sync_token: nil, **data)
  @external_id = external_id.to_s.to_sym
  @ledger_id = ledger_id
  @sync_token = sync_token

  data.each do |attr_key, val|
    if (self.class.references || {}).key?(attr_key)
      raise "#{val} must be of type #{self.class.references[attr_key]}" if self.class.references[attr_key] != val.class
    end

    raise "#{attr_key} is not an attribute of #{self.class}" unless self.class.attributes.include?(attr_key)
  end

  self.class.attributes.each do |attribute|
    instance_variable_set("@#{attribute}", data.dig(attribute))
  end
end

Instance Attribute Details

#external_idObject

serialize only: %i[

attributes
external_id
ledger_id
sync_token

]



18
19
20
# File 'lib/ledger_sync/resource.rb', line 18

def external_id
  @external_id
end

#ledger_idObject

serialize only: %i[

attributes
external_id
ledger_id
sync_token

]



18
19
20
# File 'lib/ledger_sync/resource.rb', line 18

def ledger_id
  @ledger_id
end

#sync_tokenObject

serialize only: %i[

attributes
external_id
ledger_id
sync_token

]



18
19
20
# File 'lib/ledger_sync/resource.rb', line 18

def sync_token
  @sync_token
end

Class Method Details

.attribute(name) ⇒ Object

def serializable_type

self.class.resource_type

end



54
55
56
57
# File 'lib/ledger_sync/resource.rb', line 54

def self.attribute(name)
  attributes << name.to_sym
  class_eval { attr_accessor name }
end

.attributesObject



59
60
61
# File 'lib/ledger_sync/resource.rb', line 59

def self.attributes
  @attributes ||= []
end

.klass_from_resource_type(obj) ⇒ Object



63
64
65
# File 'lib/ledger_sync/resource.rb', line 63

def self.klass_from_resource_type(obj)
  LedgerSync.const_get(LedgerSync::Util::StringHelpers.camelcase(obj))
end

.reference(name, type) ⇒ Object



67
68
69
70
# File 'lib/ledger_sync/resource.rb', line 67

def self.reference(name, type)
  attribute(name)
  references[name.to_sym] = type
end

.reference_klass(name) ⇒ Object



76
77
78
# File 'lib/ledger_sync/resource.rb', line 76

def self.reference_klass(name)
  references[name.to_sym]
end

.reference_resource_type(name) ⇒ Object



80
81
82
# File 'lib/ledger_sync/resource.rb', line 80

def self.reference_resource_type(name)
  reference_klass(name).resource_type
end

.referencesObject



72
73
74
# File 'lib/ledger_sync/resource.rb', line 72

def self.references
  @references ||= {}
end

.resource_typeObject



84
85
86
# File 'lib/ledger_sync/resource.rb', line 84

def self.resource_type
  @resource_type ||= LedgerSync::Util::StringHelpers.underscore(name.split('::').last).to_sym
end

Instance Method Details

#==(other) ⇒ Object



88
89
90
# File 'lib/ledger_sync/resource.rb', line 88

def ==(other)
  other.fingerprint == fingerprint
end

#attributesObject



38
39
40
# File 'lib/ledger_sync/resource.rb', line 38

def attributes
  self.class.attributes
end

#referencesObject



42
43
44
# File 'lib/ledger_sync/resource.rb', line 42

def references
  self.class.references
end

#serialize_attributesObject



46
47
48
# File 'lib/ledger_sync/resource.rb', line 46

def serialize_attributes
  Hash[self.class.attributes.map { |a| [a, send(a)] }]
end