Module: Friendly::Document::Attributes
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #assign(name, value) ⇒ Object
- #assign_default_values ⇒ Object
-
#attribute_changed?(attribute) ⇒ Boolean
Has this attribute changed?.
-
#attribute_was(attribute) ⇒ Object
Get the original value of an attribute that has changed.
- #attributes=(attrs) ⇒ Object
-
#changed ⇒ Object
Which attributes that are being tracked have changed since last reset?.
-
#changed? ⇒ Boolean
Have any of the attributes that are being tracked changed since last reset?.
- #initialize(opts = {}) ⇒ Object
-
#not_changed(attribute) ⇒ Object
Reset the changed-ness of one attribute.
-
#reset_changes ⇒ Object
Reset all the changes to this object.
-
#save ⇒ Object
Override #save to reset changes afterwards.
- #to_hash ⇒ Object
-
#will_change(attribute) ⇒ Object
Notify the object that an attribute is about to change.
Methods included from Mixin
Instance Method Details
#assign(name, value) ⇒ Object
46 47 48 |
# File 'lib/friendly/document/attributes.rb', line 46 def assign(name, value) send(:"#{name}=", value) end |
#assign_default_values ⇒ Object
42 43 44 |
# File 'lib/friendly/document/attributes.rb', line 42 def assign_default_values self.class.attributes.values.each { |a| a.assign_default_value(self) } end |
#attribute_changed?(attribute) ⇒ Boolean
Has this attribute changed?
71 72 73 |
# File 'lib/friendly/document/attributes.rb', line 71 def attribute_changed?(attribute) changed.include?(attribute) end |
#attribute_was(attribute) ⇒ Object
Get the original value of an attribute that has changed.
63 64 65 |
# File 'lib/friendly/document/attributes.rb', line 63 def attribute_was(attribute) instance_variable_get(:"@#{attribute}_was") end |
#attributes=(attrs) ⇒ Object
30 31 32 33 |
# File 'lib/friendly/document/attributes.rb', line 30 def attributes=(attrs) assert_no_duplicate_keys(attrs) attrs.each { |name, value| assign(name, value) } end |
#changed ⇒ Object
Which attributes that are being tracked have changed since last reset?
83 84 85 |
# File 'lib/friendly/document/attributes.rb', line 83 def changed @changed ||= Set.new end |
#changed? ⇒ Boolean
Have any of the attributes that are being tracked changed since last reset?
77 78 79 |
# File 'lib/friendly/document/attributes.rb', line 77 def changed? !changed.empty? end |
#initialize(opts = {}) ⇒ Object
25 26 27 28 |
# File 'lib/friendly/document/attributes.rb', line 25 def initialize(opts = {}) assign_default_values self.attributes = opts end |
#not_changed(attribute) ⇒ Object
Reset the changed-ness of one attribute.
95 96 97 98 |
# File 'lib/friendly/document/attributes.rb', line 95 def not_changed(attribute) instance_variable_set(:"@#{attribute}_was", nil) changed.delete(attribute) end |
#reset_changes ⇒ Object
Reset all the changes to this object.
89 90 91 |
# File 'lib/friendly/document/attributes.rb', line 89 def reset_changes changed.each { |c| not_changed(c) }.clear end |
#save ⇒ Object
Override #save to reset changes afterwards
104 105 106 107 |
# File 'lib/friendly/document/attributes.rb', line 104 def save super reset_changes end |
#to_hash ⇒ Object
35 36 37 38 39 40 |
# File 'lib/friendly/document/attributes.rb', line 35 def to_hash self.class.attributes.keys.inject({}) do |memo, key| memo[key] = send(key) memo end end |
#will_change(attribute) ⇒ Object
Notify the object that an attribute is about to change.
54 55 56 57 |
# File 'lib/friendly/document/attributes.rb', line 54 def will_change(attribute) changed << attribute instance_variable_set(:"@#{attribute}_was", send(attribute)) end |