Class: LedgerSync::Resource

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

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



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

def self.inherited(subclass)
  subclass.attribute :external_id, type: Type::ID
  subclass.attribute :ledger_id, type: Type::ID
end

.resource_typeObject



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

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

.serialize_attribute?(sattr) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/ledger_sync/resource.rb', line 68

def self.serialize_attribute?(sattr)
  sattr = sattr.to_sym
  return true if resource_attributes.key?(sattr)

  false
end

Instance Method Details

#==(other) ⇒ Object



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

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

#assign_attribute(name, value) ⇒ Object



31
32
33
# File 'lib/ledger_sync/resource.rb', line 31

def assign_attribute(name, value)
  public_send("#{name}=", value)
end

#assign_attributes(**keywords) ⇒ Object



35
36
37
# File 'lib/ledger_sync/resource.rb', line 35

def assign_attributes(**keywords)
  keywords.each { |k, v| assign_attribute(k, v) }
end

#changed?Boolean

Returns:

  • (Boolean)


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

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

#changesObject



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

def changes
  super.merge(Hash[resource_attributes.references_many.map { |ref| [ref.name, ref.changes['value']] if ref.changed? }.compact])
end

#dupObject



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

def dup
  Marshal.load(Marshal.dump(self))
end

#klass_from_resource_type(obj) ⇒ Object



51
52
53
# File 'lib/ledger_sync/resource.rb', line 51

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

#to_hObject



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

def to_h
  resource_attributes.to_h.merge(dirty_attributes_to_h)
end