Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
244 lines (198 sloc) 6.29 KB

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:

  • Unmerge should be used before merge to properly transform one hash into another. It will still work in most cases if you don't, but some edge cases will break.

  • The order of elements within an array may be different when transforming one hash into another using unmerge and merge. There is currently no way to guarantee the order is untouched.

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
  }
}

# Diff the two hashes to get what was removed and what was added.
removed, added = old_data.easy_diff(new_data)

# removed will equal
# {
#   :name => "Foo",
#   :tags => [
#     "pretend",
#     "at",
#     "examples"
#   ],
#   :config => {
#     :go_fast => false,
#     :actually_work => false
#   }
# }
#
#
# added will equal
# {
#   :name => "Bar",
#   :description => "An awesome thingy I made.",
#   :tags => [
#     "really"
#   ],
#   :config => {
#     :go_fast => true,
#     :actually_work => true
#   }
# }

# Now that we have the removed and added hashes, we can transform the old data into the new data or vice versa
transformed_old_data = old_data.easy_unmerge(removed).easy_merge(added)
transformed_new_data = new_data.easy_unmerge(added).easy_merge(removed)

# Let's see if there are any diffs between the transformed hashes and their respective counterparts

transformed_old_data.easy_diff(new_data)
#   => [{}, {}] # Two empty hashes, indicating no diffs.

transformed_new_data.easy_diff(old_data)
#   => [{}, {}] # Two empty hashes, indicating no diffs.

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

# The removed and added hashes should contain the following:
# removed = {
#     :tags => ['a'],
#     :pos => {:x => '1'},
#     :some_int => 1,
#     :some_bool => false,
#     :extra_removed => "bye"
#   }
#
#   added = {
#     :tags => ['d'],
#     :pos => {:x => '3'},
#     :some_int => 2,
#     :some_bool => true,
#     :extra_added => "hi"
#   }

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

# The merged hash should look like this:
# merged = {
#   :tags => ['a', 'b', 'c', 'd'],
#   :pos => {:x => '3', :y => '2'},
#   :some_str => "bla",
#   :some_int => 2,
#   :some_bool => true,
#   :extra_removed => "bye",
#   :extra_added => "hi"
# }

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

# The unmerged hash should look like this:
# unmerged =  {
#     :tags => ['b', 'c'],
#     :pos => {:y => '2'},
#     :some_str => "bla"
#   }

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'

# The original hash will still look like this:
# original = {
#   :tags => ['b', 'c', 'd'],
#   :pos => {:x => '3', :y => '2'}
# }

Contributing to easy_diff

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet

  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright © 2011 Abner Qian. See LICENSE.txt for further details.