Recursive diff, merge, and unmerge for hashes and arrays.
= easy_diff
Easy Diff enhances the functionality of Hash, allowing recursive diff, merge, and unmerge of arbitrarily constructed hashes.
This is perfect for people who need to do diffs on not only plain text files but also data as Hash or JSON objects.
Unmerge is included with diff and merge to more easily allow versioning of arbitrary data.
There are a few caveats when using this gem:
== Quick Start Example
old_data = {
:id => 1,
:name => “Foo”,
:tags => [
“pretend”,
“i”,
“am”,
“good”,
“at”,
“examples”
],
:config => {
:awesome => true,
:go_fast => false,
:actually_work => false
}
}
new_data = {
:id => 1,
:name => “Bar”,
:description => “An awesome thingy I made.”,
:tags => [
“i”,
“am”,
“really”,
“good”
],
:config => {
:awesome => true,
:go_fast => true,
:actually_work => true
}
}
removed, added = old_data.easy_diff(new_data)
transformed_old_data = old_data.easy_unmerge(removed).easy_merge(added)
transformed_new_data = new_data.easy_unmerge(added).easy_merge(removed)
transformed_old_data.easy_diff(new_data)
transformed_new_data.easy_diff(old_data)
== Install
gem install easy_diff
== Methods
=== Hash#easy_diff
Takes another hash and recursively determines the differences between self and the other hash. This method returns two hashes.
The first contains what has to be removed from self to create the second hash. The second contains what has to be added.
When diffing arrays, the order of the arrays is ignored and only the contents are compared.
original = {
:tags => [‘a’, ‘b’, ‘c’],
:pos => {:x => ‘1’, :y => ‘2’},
:some_str => “bla”,
:some_int => 1,
:some_bool => false,
:extra_removed => “bye”
}
modified = {
:tags => [‘b’, ‘c’, ‘d’],
:pos => {:x => ‘3’, :y => ‘2’},
:some_str => “bla”,
:some_int => 2,
:some_bool => true,
:extra_added => “hi”
}
removed, added = original.easy_diff modified
=== Hash#easy_merge
Takes a hash and recursively merges it with self. Arrays are merged by concatenation.
Using Hash#easy_merge! will modify self instead of returning a new hash.
original = {
:tags => [‘a’, ‘b’, ‘c’],
:pos => {:x => ‘1’, :y => ‘2’},
:some_str => “bla”,
:some_int => 1,
:some_bool => false,
:extra_removed => “bye”
}
extra = {
:tags => [‘d’],
:pos => {:x => ‘3’},
:some_int => 2,
:some_bool => true,
:extra_added => “hi”
}
merged = original.easy_merge extra
=== Hash#easy_unmerge
Takes a hash and recursively unmerges it with self. Unmerging means it will remove all matching values from the hash.
Arrays will be unmerged by removing matching values in a non-greedy manner (i.e. [1, 1, 1] unmerged with [1] is [1, 1] instead of []).
Using Hash#easy_unmerge! will modify self instead of returning a new hash.
original = {
:tags => [‘b’, ‘c’, ‘d’],
:pos => {:x => ‘3’, :y => ‘2’},
:some_str => “bla”,
:some_int => 2,
:some_bool => true,
:extra_added => “hi”
}
extra = {
:tags => [‘d’],
:pos => {:x => ‘3’},
:some_int => 2,
:some_bool => true,
:extra_added => “hi”
}
unmerged = original.easy_unmerge extra
=== Hash#easy_clone
Performs a deep clone on a hash
original = {
:tags => [‘b’, ‘c’, ‘d’],
:pos => {:x => ‘3’, :y => ‘2’}
}
new = original.easy_clone
new[:tags] << ‘e’
new[:pos][:y] = ‘1’
== Contributing to easy_diff
== Copyright
Copyright © 2011 Abner Qian. See LICENSE.txt for
further details.