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



65
66
67
68
69
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 65

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



71
72
73
74
75
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 71

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

#initialize(**data) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 56

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

  assign_attributes(**data)

  super()
end

#resource_attributesObject

Store attribute instance values separately



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

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

#saveObject



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

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

#serialize_attributesObject



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

def serialize_attributes
  resource_attributes.transform_values(&:value)
end