Class: LedgerSync::Resource

Inherits:
Object
  • Object
show all
Includes:
Fingerprintable::Mixin, Ledgers::Mixins::InferConfigMixin, LedgerSync::ResourceAttribute::Mixin, LedgerSync::ResourceAttribute::Reference::Many::Mixin, LedgerSync::ResourceAttribute::Reference::One::Mixin, Util::Mixins::DupableMixin, Validatable, SimplySerializable::Mixin
Defined in:
lib/ledger_sync/resource.rb

Direct Known Subclasses

Ledgers::TestLedger::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

Methods included from Ledgers::Mixins::InferConfigMixin

included

Methods included from Util::Mixins::DupableMixin

#dup

Methods included from LedgerSync::ResourceAttribute::Reference::Many::Mixin

included

Methods included from LedgerSync::ResourceAttribute::Reference::One::Mixin

included

Methods included from LedgerSync::ResourceAttribute::Mixin

included, #initialize, #resource_attributes, #save, #serialize_attributes

Methods included from Validatable

#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

.operationsObject



78
79
80
# File 'lib/ledger_sync/resource.rb', line 78

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

.resource_module_strObject



82
83
84
# File 'lib/ledger_sync/resource.rb', line 82

def self.resource_module_str
  @resource_module_str ||= name.split('::').last
end

.resource_typeObject



86
87
88
# File 'lib/ledger_sync/resource.rb', line 86

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

.serialize_attribute?(sattr) ⇒ Boolean

Returns:

  • (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

Returns:

  • (Boolean)


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

def changed?
  super || resource_attributes.references_many.select(&:changed?).any?
end

#changesObject



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



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

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

#to_hObject



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

def to_h
  resource_attributes.to_h.merge(dirty_attributes_to_h)
end