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



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

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

.resource_module_strObject



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

def self.resource_module_str
  @resource_module_str ||= name.split('LedgerSync::')[1..-1].join('LedgerSync::')
end

.resource_typeObject



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

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

.serialize_attribute?(sattr) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  false
end

Instance Method Details

#==(other) ⇒ Object



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

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

#assign_attribute(name, value) ⇒ Object



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

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

#assign_attributes(**keywords) ⇒ Object



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

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

#changed?Boolean

Returns:

  • (Boolean)


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

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

#changesObject



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

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

#dupObject



49
50
51
# File 'lib/ledger_sync/resource.rb', line 49

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

#klass_from_resource_type(obj) ⇒ Object



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

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

#to_hObject



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

def to_h
  resource_attributes.to_h.merge(dirty_attributes_to_h)
end