Class: LedgerSync::Resource
Constant Summary
collapse
- PRIMITIVES =
[
ActiveModel::Type,
Date,
DateTime,
FalseClass,
Float,
Integer,
NilClass,
String,
Time,
TrueClass
].freeze
Class Method Summary
collapse
Instance Method Summary
collapse
included
#dup
included
included
included, #initialize, #resource_attributes, #save, #serialize_attributes
#valid?, #validate, #validate_or_fail
Class Method Details
.inherited(subclass) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/ledger_sync/resource.rb', line 64
def self.inherited(subclass)
resource_attributes.each do |_name, resource_attribute|
subclass._add_resource_attribute(resource_attribute)
end
LedgerSync.register_resource(resource: subclass)
return if subclass.inferred_config.nil?
subclass.inferred_config.client_class.register_resource(resource: subclass)
super
end
|
.operations ⇒ Object
78
79
80
|
# File 'lib/ledger_sync/resource.rb', line 78
def self.operations
@operations ||= {}
end
|
.resource_module_str ⇒ Object
82
83
84
|
# File 'lib/ledger_sync/resource.rb', line 82
def self.resource_module_str
@resource_module_str ||= name.split('::').last
end
|
.serialize_attribute?(sattr) ⇒ Boolean
90
91
92
93
94
95
|
# File 'lib/ledger_sync/resource.rb', line 90
def self.serialize_attribute?(sattr)
sattr = sattr.to_sym
return true if resource_attributes.key?(sattr)
false
end
|
Instance Method Details
#==(other) ⇒ Object
97
98
99
|
# File 'lib/ledger_sync/resource.rb', line 97
def ==(other)
other&.fingerprint == fingerprint
end
|
#assign_attribute(name, value) ⇒ Object
36
37
38
39
|
# File 'lib/ledger_sync/resource.rb', line 36
def assign_attribute(name, value)
public_send("#{name}=", value)
self
end
|
#assign_attributes(**keywords) ⇒ Object
41
42
43
44
|
# File 'lib/ledger_sync/resource.rb', line 41
def assign_attributes(**keywords)
keywords.each { |k, v| assign_attribute(k, v) }
self
end
|
#changed? ⇒ Boolean
46
47
48
|
# File 'lib/ledger_sync/resource.rb', line 46
def changed?
super || resource_attributes.references_many.select(&:changed?).any?
end
|
#changes ⇒ Object
50
51
52
53
54
|
# File 'lib/ledger_sync/resource.rb', line 50
def changes
super.merge(Hash[resource_attributes.references_many.map do |ref|
[ref.name, ref.changes['value']] if ref.changed?
end.compact])
end
|
#class_from_resource_type(obj) ⇒ Object
#to_h ⇒ Object
60
61
62
|
# File 'lib/ledger_sync/resource.rb', line 60
def to_h
resource_attributes.to_h.merge(dirty_attributes_to_h)
end
|