Module: VestalVersions::Changes::HashMethods
- Defined in:
- lib/vestal_versions/changes.rb
Overview
Instance methods included into Hash for dealing with manipulation of hashes in the specific format of ActiveRecord::Base#changes.
Instance Method Summary collapse
-
#append_changes(changes) ⇒ Object
When called on a hash of changes and given a second hash of changes as an argument,
append_changes
will run the second hash on top of the first, updating the last element of each array value with its own, or creating its own key/value pair for missing keys. -
#append_changes!(changes) ⇒ Object
Destructively appends a given hash of changes onto an existing hash of changes.
-
#prepend_changes(changes) ⇒ Object
Appends the existing hash of changes onto a given hash of changes.
-
#prepend_changes!(changes) ⇒ Object
Destructively prepends a given hash of changes onto an existing hash of changes.
-
#reverse_changes ⇒ Object
Reverses the array values of a hash of changes.
-
#reverse_changes! ⇒ Object
Destructively reverses the array values of a hash of changes.
Instance Method Details
#append_changes(changes) ⇒ Object
When called on a hash of changes and given a second hash of changes as an argument, append_changes
will run the second hash on top of the first, updating the last element of each array value with its own, or creating its own key/value pair for missing keys. Resulting non-unique array values are removed.
Example
first =
"first_name" => ["Steve", "Stephen"],
"age" => [25, 26]
second =
"first_name" => ["Stephen", "Steve"],
"last_name" => ["Richert", "Jobs"],
"age" => [26, 54]
first.append_changes(second) # =>
"last_name" => ["Richert", "Jobs"],
"age" => [25, 54]
83 84 85 86 87 88 89 90 |
# File 'lib/vestal_versions/changes.rb', line 83 def append_changes(changes) changes.inject(self) do |new_changes, (attribute, change)| new_change = [new_changes.fetch(attribute, change).first, change.last] new_changes.merge(attribute => new_change) end.reject do |attribute, change| change.first == change.last end end |
#append_changes!(changes) ⇒ Object
Destructively appends a given hash of changes onto an existing hash of changes.
93 94 95 |
# File 'lib/vestal_versions/changes.rb', line 93 def append_changes!(changes) replace(append_changes(changes)) end |
#prepend_changes(changes) ⇒ Object
Appends the existing hash of changes onto a given hash of changes. Relates to the append_changes
method in the same way that Hash#reverse_merge relates to Hash#merge.
100 101 102 |
# File 'lib/vestal_versions/changes.rb', line 100 def prepend_changes(changes) changes.append_changes(self) end |
#prepend_changes!(changes) ⇒ Object
Destructively prepends a given hash of changes onto an existing hash of changes.
105 106 107 |
# File 'lib/vestal_versions/changes.rb', line 105 def prepend_changes!(changes) replace(prepend_changes(changes)) end |
#reverse_changes ⇒ Object
Reverses the array values of a hash of changes. Useful for reversion both backward and forward through a record’s history of changes.
111 112 113 |
# File 'lib/vestal_versions/changes.rb', line 111 def reverse_changes inject({}){|nc,(a,c)| nc.merge!(a => c.reverse) } end |
#reverse_changes! ⇒ Object
Destructively reverses the array values of a hash of changes.
116 117 118 |
# File 'lib/vestal_versions/changes.rb', line 116 def reverse_changes! replace(reverse_changes) end |