Module: LedgerSync::ResourceAttribute::Mixin

Included in:
LedgerSync::Resource
Defined in:
lib/ledger_sync/resource_attribute/mixin.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 10

def self.included(base)
  base.include(DirtyMixin)
  base.extend(ClassMethods)
end

Instance Method Details

#assign_attribute(name, value) ⇒ Object



75
76
77
78
79
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 75

def assign_attribute(name, value)
  raise "#{name} is not an attribute of #{self.class.name}" unless resource_attributes.key?(name)

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

#assign_attributes(attribute_hash) ⇒ Object



81
82
83
84
85
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 81

def assign_attributes(attribute_hash)
  attribute_hash.each do |name, value|
    assign_attribute(name, value)
  end
end

#initialize(**data) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 66

def initialize(**data)
  # Initialize empty values
  resource_attributes.keys.each { |e| instance_variable_set("@#{e}", nil) }

  assign_attributes(data)

  super()
end

#resource_attributesObject

Store attribute instance values separately



88
89
90
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 88

def resource_attributes
  @resource_attributes ||= Marshal.load(Marshal.dump(self.class.resource_attributes))
end

#saveObject



92
93
94
95
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 92

def save
  resoure_attributes.map(&:save)
  super
end

#serialize_attributesObject



97
98
99
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 97

def serialize_attributes
  Hash[resource_attributes.map { |k, v| [k, v.value] }]
end