Module: Extant::Attributes
- Defined in:
- lib/extant/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
- #with_metadata ⇒ Object readonly
Class Method Summary collapse
Instance Method Summary collapse
- #attribute_definitions ⇒ Object
- #attributes ⇒ Object
- #changed_attributes ⇒ Object
- #dirty?(attr = nil) ⇒ Boolean
- #each_extant_attribute ⇒ Object
- #extant_attributes ⇒ Object
- #initialize(attrs = {}) ⇒ Object
- #original_extant_attributes ⇒ Object
- #set_attributes ⇒ Object
- #update_attributes(attrs = {}) ⇒ Object
- #with(attrs = {}, metadata = {}) ⇒ Object
Instance Attribute Details
#with_metadata ⇒ Object
106 107 108 |
# File 'lib/extant/attributes.rb', line 106 def @with_metadata || {} end |
Class Method Details
.included(base) ⇒ Object
6 7 8 |
# File 'lib/extant/attributes.rb', line 6 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#attribute_definitions ⇒ Object
47 48 49 |
# File 'lib/extant/attributes.rb', line 47 def attribute_definitions self.class.attribute_definitions end |
#attributes ⇒ Object
41 42 43 44 45 |
# 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 ⇒ Object
63 64 65 66 67 68 69 |
# 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) ⇒ Boolean
85 86 87 88 89 90 91 |
# 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 ⇒ Object
71 72 73 74 75 |
# File 'lib/extant/attributes.rb', line 71 def each_extant_attribute extant_attributes.values.each do |v| yield v end end |
#extant_attributes ⇒ Object
51 52 53 |
# File 'lib/extant/attributes.rb', line 51 def extant_attributes @extant_attributes ||= {} end |
#initialize(attrs = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# 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 |
#original_extant_attributes ⇒ Object
93 94 95 |
# File 'lib/extant/attributes.rb', line 93 def original_extant_attributes @original_extant_attributes ||= {} end |
#set_attributes ⇒ Object
55 56 57 58 59 60 61 |
# 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 = {}) ⇒ Object
77 78 79 80 81 82 83 |
# 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 = {}) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/extant/attributes.rb', line 97 def with(attrs={}, ={}) new_instance = self.class.new(self.attributes) new_instance.update_attributes(attrs) new_instance.send(:with_metadata=, ) new_instance.send(:original_extant_attributes=, original_extant_attributes) new_instance end |