Module: LedgerSync::ResourceAttribute::Mixin::ClassMethods
- Defined in:
- lib/ledger_sync/resource_attribute/mixin.rb
Instance Method Summary collapse
- #_define_attribute_methods(name) ⇒ Object
- #attribute(name, type:) ⇒ Object
- #references_many_resource_attributes ⇒ Object
- #resource_attributes ⇒ Object
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 43 44 |
# 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}_valid_with_value?" do |val| unless resource_attributes[name].valid_with?(value: val) raise ResourceError::AttributeTypeError.new( attribute: resource_attributes[name], resource: self, value: val ) end end define_method "#{name}=" do |val| attribute = resource_attributes[name] public_send("_#{name}_valid_with_value?", val) val = attribute.type.cast(val) if attribute.type.cast? public_send("#{name}_will_change!") unless val == resource_attributes[name] # For Dirty attribute.value = val end serialize attributes: [name] end end |
#attribute(name, type:) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 46 def attribute(name, type:) resource_attribute = ResourceAttribute.new(name: name, type: type) _define_attribute_methods(name) resource_attributes.add(resource_attribute) references_many_resource_attributes << resource_attribute if resource_attribute.type.is_a?(Reference::Many) resource_attribute end |
#references_many_resource_attributes ⇒ Object
57 58 59 |
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 57 def references_many_resource_attributes @references_many_resource_attributes ||= [] end |
#resource_attributes ⇒ Object
61 62 63 |
# File 'lib/ledger_sync/resource_attribute/mixin.rb', line 61 def resource_attributes @resource_attributes ||= ResourceAttributeSet.new(resource: self) end |