Class: JsonCompare::Comparer
- Inherits:
-
Object
- Object
- JsonCompare::Comparer
- Defined in:
- lib/json-compare/comparer.rb
Instance Attribute Summary collapse
-
#excluded_keys ⇒ Object
Returns the value of attribute excluded_keys.
Instance Method Summary collapse
- #compare_arrays(old_array, new_array) ⇒ Object
- #compare_elements(old, new) ⇒ Object
- #compare_hash_array(old_hash, new_array) ⇒ Object
- #compare_hashes(old_hash, new_hash) ⇒ Object
- #compare_strings(old_string, new_string) ⇒ Object
- #filter_results(result) ⇒ Object
-
#get_diffs_struct ⇒ Object
Returns diffs-hash with bare structure.
- #is_boolean(obj) ⇒ Object
Instance Attribute Details
#excluded_keys ⇒ Object
Returns the value of attribute excluded_keys.
4 5 6 |
# File 'lib/json-compare/comparer.rb', line 4 def excluded_keys @excluded_keys end |
Instance Method Details
#compare_arrays(old_array, new_array) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/json-compare/comparer.rb', line 47 def compare_arrays(old_array, new_array) old_array_length = old_array.count new_array_length = new_array.count inters = [old_array.count, new_array.count].min result = get_diffs_struct (0..inters).map do |n| res = compare_elements(old_array[n], new_array[n]) result[:update][n] = res unless res.empty? end # the rest of the larger array if inters == old_array_length (inters..new_array_length).each do |n| result[:append][n] = new_array[n] end else (inters..old_array_length).each do |n| result[:remove][n] = old_array[n] end end filter_results(result) end |
#compare_elements(old, new) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/json-compare/comparer.rb', line 10 def compare_elements(old, new) diff = {} if old.kind_of? Hash if new.kind_of? Array diff_hash = compare_hash_array(old, new) else diff_hash = compare_hashes(old, new) end diff = diff_hash if diff_hash.count > 0 elsif (!is_boolean(old) || !is_boolean(new)) && old.class != new.class diff = new elsif old.kind_of? Array diff_arr = compare_arrays(old, new) diff = diff_arr if diff_arr.count > 0 else string_diff = compare_strings(old, new) diff = string_diff unless string_diff.nil? end diff end |
#compare_hash_array(old_hash, new_array) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/json-compare/comparer.rb', line 73 def compare_hash_array(old_hash, new_array) result = get_diffs_struct (0..new_array.count).map do |n| next if new_array[n].nil? if n == 0 res = compare_elements(old_hash, new_array[0]) result[:update][n] = res unless res.empty? else result[:append][n] = new_array[n] end end filter_results(result) end |
#compare_hashes(old_hash, new_hash) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/json-compare/comparer.rb', line 31 def compare_hashes(old_hash, new_hash) keys = (old_hash.keys + new_hash.keys).uniq result = get_diffs_struct keys.each do |k| if !old_hash.has_key? k result[:append][k] = new_hash[k] elsif !new_hash.has_key? k result[:remove][k] = new_hash[k] else diff = compare_elements(old_hash[k], new_hash[k]) result[:update][k] = diff unless diff.empty? end end filter_results(result) end |
#compare_strings(old_string, new_string) ⇒ Object
89 90 91 |
# File 'lib/json-compare/comparer.rb', line 89 def compare_strings(old_string, new_string) (old_string != new_string) ? new_string.to_s : "" end |
#filter_results(result) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/json-compare/comparer.rb', line 98 def filter_results(result) return {} if result.nil? out_result = {} result.each_key do |change_type| next if result[change_type].nil? temp_hash = {} result[change_type].each_key do |key| next if result[change_type][key].nil? next if @excluded_keys.include? key temp_hash[key] = result[change_type][key] end out_result[change_type] = temp_hash if temp_hash.count > 0 end out_result end |
#get_diffs_struct ⇒ Object
Returns diffs-hash with bare structure
94 95 96 |
# File 'lib/json-compare/comparer.rb', line 94 def get_diffs_struct {:append => {}, :remove => {},:update => {}} end |
#is_boolean(obj) ⇒ Object
6 7 8 |
# File 'lib/json-compare/comparer.rb', line 6 def is_boolean(obj) !!obj == obj end |