Module: LedgerSync::ResourceAttribute::Mixin::ClassMethods

Defined in:
lib/ledger_sync/resource_attribute/mixin.rb

Instance Method Summary collapse

Instance Method Details

#_add_resource_attribute(resource_attribute) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 16

def _add_resource_attribute(resource_attribute)
  _define_attribute_methods(resource_attribute.name)

  resource_attributes.add(resource_attribute)
  references_many_resource_attributes << resource_attribute if resource_attribute.type.is_a?(Reference::Many)

  resource_attribute
end

#_define_attribute_methods(name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 25

def _define_attribute_methods(name)
  class_eval do
    define_attribute_methods name

    define_method name do
      resource_attributes[name].value
    end

    define_method "#{name}=" do |val|
      resource_attribute = resource_attributes[name]
      public_send("#{name}_will_change!") if resource_attribute.will_change?(val) # For Dirty
      resource_attribute.value = val
    end

    simply_serialize attributes: [name]
  end
end

#attribute(name, type:) ⇒ Object



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

def attribute(name, type:)
  _add_resource_attribute(ResourceAttribute.new(name: name, resource_class: self, type: type))
end

#references_many_resource_attributesObject



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

def references_many_resource_attributes
  @references_many_resource_attributes ||= []
end

#resource_attributesObject



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

def resource_attributes
  @resource_attributes ||= ResourceAttributeSet.new(resource: self)
end