Module: Hashdiff
- Defined in:
- lib/hashdiff/patch.rb,
lib/hashdiff/lcs.rb,
lib/hashdiff/diff.rb,
lib/hashdiff/util.rb,
lib/hashdiff/version.rb,
lib/hashdiff/compare_hashes.rb,
lib/hashdiff/lcs_compare_arrays.rb,
lib/hashdiff/linear_compare_array.rb
Overview
This module provides methods to diff two hash, patch and unpatch hash
Constant Summary collapse
- VERSION =
'1.1.2'.freeze
Class Method Summary collapse
-
.best_diff(obj1, obj2, options = {}) {|path, value1, value2| ... } ⇒ Array
Best diff two objects, which tries to generate the smallest change set using different similarity values.
-
.diff(obj1, obj2, options = {}) {|path, value1, value2| ... } ⇒ Array
Compute the diff of two hashes or arrays.
-
.patch!(obj, changes, options = {}) ⇒ Object
Apply patch to object.
- .prefix_append_array_index(prefix, array_index, opts) ⇒ Object
- .prefix_append_key(prefix, key, opts) ⇒ Object
-
.unpatch!(obj, changes, options = {}) ⇒ Object
Unpatch an object.
Class Method Details
.best_diff(obj1, obj2, options = {}) {|path, value1, value2| ... } ⇒ Array
Best diff two objects, which tries to generate the smallest change set using different similarity values.
Hashdiff.best_diff is useful in case of comparing two objects which include similar hashes in arrays.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hashdiff/diff.rb', line 32 def self.best_diff(obj1, obj2, = {}, &block) [:comparison] = block if block_given? opts = { similarity: 0.3 }.merge!() diffs1 = diff(obj1, obj2, opts) count1 = count_diff diffs1 opts = { similarity: 0.5 }.merge!() diffs2 = diff(obj1, obj2, opts) count2 = count_diff diffs2 opts = { similarity: 0.8 }.merge!() diffs3 = diff(obj1, obj2, opts) count3 = count_diff diffs3 count, diffs = count1 < count2 ? [count1, diffs1] : [count2, diffs2] count < count3 ? diffs : diffs3 end |
.diff(obj1, obj2, options = {}) {|path, value1, value2| ... } ⇒ Array
Compute the diff of two hashes or arrays
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/hashdiff/diff.rb', line 80 def self.diff(obj1, obj2, = {}, &block) opts = { prefix: '', similarity: 0.8, delimiter: '.', strict: true, ignore_keys: [], indifferent: false, strip: false, numeric_tolerance: 0, array_path: false, use_lcs: true }.merge!() opts[:prefix] = [] if opts[:array_path] && opts[:prefix] == '' opts[:ignore_keys] = [*opts[:ignore_keys]] opts[:comparison] = block if block_given? # prefer to compare with provided block result = custom_compare(opts[:comparison], opts[:prefix], obj1, obj2) return result if result return [] if obj1.nil? && obj2.nil? return [['~', opts[:prefix], obj1, obj2]] if obj1.nil? || obj2.nil? return [['~', opts[:prefix], obj1, obj2]] unless comparable?(obj1, obj2, opts[:strict]) return LcsCompareArrays.call(obj1, obj2, opts) if obj1.is_a?(Array) && opts[:use_lcs] return LinearCompareArray.call(obj1, obj2, opts) if obj1.is_a?(Array) && !opts[:use_lcs] return CompareHashes.call(obj1, obj2, opts) if obj1.is_a?(Hash) return [] if compare_values(obj1, obj2, opts) [['~', opts[:prefix], obj1, obj2]] end |
.patch!(obj, changes, options = {}) ⇒ Object
Apply patch to object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hashdiff/patch.rb', line 17 def self.patch!(obj, changes, = {}) delimiter = [:delimiter] || '.' changes.each do |change| parts = change[1] parts = decode_property_path(parts, delimiter) unless parts.is_a?(Array) last_part = parts.last parent_node = node(obj, parts[0, parts.size - 1]) if change[0] == '+' if parent_node.is_a?(Array) parent_node.insert(last_part, change[2]) else parent_node[last_part] = change[2] end elsif change[0] == '-' if parent_node.is_a?(Array) parent_node.delete_at(last_part) else parent_node.delete(last_part) end elsif change[0] == '~' parent_node[last_part] = change[3] end end obj end |
.prefix_append_array_index(prefix, array_index, opts) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/hashdiff/util.rb', line 137 def self.prefix_append_array_index(prefix, array_index, opts) if opts[:array_path] prefix + [array_index] else "#{prefix}[#{array_index}]" end end |
.prefix_append_key(prefix, key, opts) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/hashdiff/util.rb', line 129 def self.prefix_append_key(prefix, key, opts) if opts[:array_path] prefix + [key] else prefix.empty? ? key.to_s : "#{prefix}#{opts[:delimiter]}#{key}" end end |
.unpatch!(obj, changes, options = {}) ⇒ Object
Unpatch an object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hashdiff/patch.rb', line 58 def self.unpatch!(obj, changes, = {}) delimiter = [:delimiter] || '.' changes.reverse_each do |change| parts = change[1] parts = decode_property_path(parts, delimiter) unless parts.is_a?(Array) last_part = parts.last parent_node = node(obj, parts[0, parts.size - 1]) if change[0] == '+' if parent_node.is_a?(Array) parent_node.delete_at(last_part) else parent_node.delete(last_part) end elsif change[0] == '-' if parent_node.is_a?(Array) parent_node.insert(last_part, change[2]) else parent_node[last_part] = change[2] end elsif change[0] == '~' parent_node[last_part] = change[2] end end obj end |