Class: LedgerSync::Resource
- Inherits:
-
Object
- Object
- LedgerSync::Resource
show all
- Includes:
- Fingerprintable::Mixin, Validatable, SimplySerializable::Mixin
- Defined in:
- lib/ledger_sync/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#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_id ⇒ Object
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_id ⇒ Object
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_token ⇒ Object
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
|
.attributes ⇒ Object
59
60
61
|
# File 'lib/ledger_sync/resource.rb', line 59
def self.attributes
@attributes ||= []
end
|
.klass_from_resource_type(obj) ⇒ Object
.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
|
.references ⇒ Object
72
73
74
|
# File 'lib/ledger_sync/resource.rb', line 72
def self.references
@references ||= {}
end
|
Instance Method Details
#==(other) ⇒ Object
88
89
90
|
# File 'lib/ledger_sync/resource.rb', line 88
def ==(other)
other.fingerprint == fingerprint
end
|
#attributes ⇒ Object
38
39
40
|
# File 'lib/ledger_sync/resource.rb', line 38
def attributes
self.class.attributes
end
|
#references ⇒ Object
42
43
44
|
# File 'lib/ledger_sync/resource.rb', line 42
def references
self.class.references
end
|
#serialize_attributes ⇒ Object
46
47
48
|
# File 'lib/ledger_sync/resource.rb', line 46
def serialize_attributes
Hash[self.class.attributes.map { |a| [a, send(a)] }]
end
|