Last active
August 29, 2015 13:56
-
-
Save kornypoet/9239526 to your computer and use it in GitHub Desktop.
Dumb attribute diff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'json' | |
| class DiffOMatic | |
| attr_reader :info | |
| def name(*_) ; end | |
| def description(*_) ; end | |
| def run_list(*_) ; end | |
| def override_attributes(*_) ; end | |
| def default_attributes info | |
| @info = info | |
| end | |
| def compare other | |
| puts " Default attributes is a {}" | |
| puts " Left has keys #{@info.keys} right has keys #{other.info.keys}" | |
| puts " DIFF: #{((@info.keys - other.info.keys) + (other.info.keys - @info.keys)).inspect}\n\n" | |
| @info.keys.each do |key| | |
| puts " Comparing #{key.to_sym.inspect}" | |
| case @info[key] | |
| when Array | |
| puts " Object is an []" | |
| puts " Left has #{@info[key]} right has #{other.info[key]}" | |
| difference = (@info[key] - other.info[key]) + (other.info[key] - @info[key]) | |
| puts " DIFF: #{difference.inspect}" | |
| puts " Arrays are different!" unless difference.empty? | |
| puts "\n" | |
| when Hash | |
| puts " Object is a {}" | |
| puts " Is left {} == right {} ?" | |
| same = @info[key] == other.info[key] | |
| puts " ANS: #{same}" | |
| if !same | |
| puts " Hashes are different!" | |
| puts " Left has keys #{@info[key].keys} right has keys #{other.info[key].keys}" | |
| difference = (@info[key].keys - other.info[key].keys) + (other.info[key].keys - @info[key].keys) | |
| puts " DIFF: #{difference.inspect}" | |
| puts " Arrays are different!" unless difference.empty? | |
| puts "\n" | |
| puts " Comparing key by key:" | |
| @info[key].each_pair do |nested_key, nested_value| | |
| if nested_value != other.info[key][nested_key] | |
| puts " DIFF: #{nested_key.to_sym.inspect}" | |
| puts " Left: #{JSON.pretty_generate nested_value}" | |
| puts " Right #{JSON.pretty_generate(other.info[key][nested_key] || {})}" | |
| end | |
| end | |
| else | |
| puts "\n" | |
| end | |
| end | |
| end | |
| end | |
| end | |
| left = DiffOMatic.new | |
| left.instance_eval File.read(ARGV[0]) | |
| right = DiffOMatic.new | |
| right.instance_eval File.read(ARGV[1]) | |
| puts "Comparing left #{ARGV[0]} => to right #{ARGV[1]}" | |
| left.compare right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment