module Extant::Attributes
Attributes
with_metadata[W]
Public Class Methods
included(base)
click to toggle source
# File lib/extant/attributes.rb, line 6 def self.included(base) base.extend(ClassMethods) end
new(attrs={})
click to toggle source
# File lib/extant/attributes.rb, line 21 def initialize(attrs={}) attrs = Hash[attrs.map{ |k, v| [k.to_sym, v] }] attribute_definitions.each do |k, definition| value = if attrs.key?(k) attrs[k] else UnsetValue end if UnsetValue == value && definition.has_default value = definition.default_value.is_a?(Proc) ? definition.default_value.call : definition.default_value end extant_attributes[k] = Extant::Attribute.new(k, value, definition) end self.original_extant_attributes = extant_attributes end
Public Instance Methods
attribute_definitions()
click to toggle source
# File lib/extant/attributes.rb, line 47 def attribute_definitions self.class.attribute_definitions end
attributes()
click to toggle source
# File lib/extant/attributes.rb, line 41 def attributes self.class.attribute_definitions.each_with_object({}) do |(k, _), attrs| attrs[k] = send(k) end end
changed_attributes()
click to toggle source
# File lib/extant/attributes.rb, line 63 def changed_attributes self.class.attribute_definitions.each_with_object({}) do |(k, _), attrs| if extant_attributes[k].has_changed? attrs[k] = send(k) end end end
dirty?(attr=nil)
click to toggle source
# File lib/extant/attributes.rb, line 85 def dirty?(attr=nil) if attr !!changed_extant_attributes[attr] else changed_extant_attributes.any? end end
each_extant_attribute() { |v| ... }
click to toggle source
# File lib/extant/attributes.rb, line 71 def each_extant_attribute extant_attributes.values.each do |v| yield v end end
extant_attributes()
click to toggle source
# File lib/extant/attributes.rb, line 51 def extant_attributes @extant_attributes ||= {} end
original_extant_attributes()
click to toggle source
# File lib/extant/attributes.rb, line 93 def original_extant_attributes @original_extant_attributes ||= {} end
set_attributes()
click to toggle source
# File lib/extant/attributes.rb, line 55 def set_attributes self.class.attribute_definitions.each_with_object({}) do |(k, _), attrs| if extant_attributes[k].set? attrs[k] = send(k) end end end
update_attributes(attrs={})
click to toggle source
# File lib/extant/attributes.rb, line 77 def update_attributes(attrs={}) attrs.each do |k, v| send("#{k}=".to_sym, v) if respond_to?("#{k}=".to_sym) end self end
with(attrs={}, metadata={})
click to toggle source
# File lib/extant/attributes.rb, line 97 def with(attrs={}, metadata={}) new_instance = self.class.new(self.attributes) new_instance.update_attributes(attrs) new_instance.send(:with_metadata=, metadata) new_instance.send(:original_extant_attributes=, original_extant_attributes) new_instance end
with_metadata()
click to toggle source
# File lib/extant/attributes.rb, line 106 def with_metadata @with_metadata || {} end
Private Instance Methods
changed_extant_attributes()
click to toggle source
# File lib/extant/attributes.rb, line 114 def changed_extant_attributes self.class.attribute_definitions.each_with_object({}) do |(k, _), attrs| if extant_attributes[k].has_changed? attrs[k] = extant_attributes[k] end end end
original_extant_attributes=(attrs={})
click to toggle source
# File lib/extant/attributes.rb, line 122 def original_extant_attributes=(attrs={}) attrs.each do |k, v| original_extant_attributes[k] = v.dup end end