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

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

Instance Method Summary collapse

Instance Method Details

#_define_attribute_methods(name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 16

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|
      public_send("#{name}_will_change!") unless val == resource_attributes[name] # For Dirty

      attribute = resource_attributes[name]

      unless attribute.valid_with?(value: val)
        raise ResourceError::AttributeTypeError.new(
          attribute: attribute,
          resource: self,
          value: val
        )
      end

      attribute.value = val
    end

    serialize attributes: [name]
  end
end

#attribute(name, type:) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 44

def attribute(name, type:)
  resource_attribute = ResourceAttribute.new(name: name, type: type)

  _define_attribute_methods(name)

  resource_attributes.add(resource_attribute)

  resource_attribute
end

#resource_attributesObject



54
55
56
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 54

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