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
43 44 45 |
# File 'lib/friendly/document/attributes.rb', line 43 def assign(name, value) send(:"#{name}=", value) end |
#assign_default_values ⇒ Object
39 40 41 |
# File 'lib/friendly/document/attributes.rb', line 39 def assign_default_values self.class.attributes.values.each { |a| a.assign_default_value(self) } end |
#attribute_changed?(attribute) ⇒ Boolean
Has this attribute changed?
68 69 70 |
# File 'lib/friendly/document/attributes.rb', line 68 def attribute_changed?(attribute) changed.include?(attribute) end |
#attribute_was(attribute) ⇒ Object
Get the original value of an attribute that has changed.
60 61 62 |
# File 'lib/friendly/document/attributes.rb', line 60 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?
80 81 82 |
# File 'lib/friendly/document/attributes.rb', line 80 def changed @changed ||= Set.new end |
#changed? ⇒ Boolean
Have any of the attributes that are being tracked changed since last reset?
74 75 76 |
# File 'lib/friendly/document/attributes.rb', line 74 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.
92 93 94 95 |
# File 'lib/friendly/document/attributes.rb', line 92 def not_changed(attribute) instance_variable_set(:"@#{attribute}_was", nil) changed.delete(attribute) end |
#reset_changes ⇒ Object
Reset all the changes to this object.
86 87 88 |
# File 'lib/friendly/document/attributes.rb', line 86 def reset_changes changed.each { |c| not_changed(c) }.clear end |
#save ⇒ Object
Override #save to reset changes afterwards
101 102 103 104 |
# File 'lib/friendly/document/attributes.rb', line 101 def save super reset_changes end |
#to_hash ⇒ Object
35 36 37 |
# File 'lib/friendly/document/attributes.rb', line 35 def to_hash Hash[*self.class.attributes.keys.map { |n| [n, send(n)] }.flatten] end |
#will_change(attribute) ⇒ Object
Notify the object that an attribute is about to change.
51 52 53 54 |
# File 'lib/friendly/document/attributes.rb', line 51 def will_change(attribute) changed << attribute instance_variable_set(:"@#{attribute}_was", send(attribute)) end |