lib/eco/data/hashes/array_diff.rb in eco-helpers-2.5.3 vs lib/eco/data/hashes/array_diff.rb in eco-helpers-2.5.4

- old
+ new

@@ -1,51 +1,20 @@ module Eco module Data module Hashes class ArrayDiff extend Eco::Language::Models::ClassHelpers + # We can change the `DiffResult` class (items) + class_resolver :diff_result_class, "Eco::Data::Hash::DiffResult" - class << self - def key(value = nil) - return @key unless value - @key = value.to_s - end - - def key? - !!@key - end - - def compare(*attrs) - compared_attrs.push(*attrs.map(&:to_s)).uniq! - end - - def case_sensitive(value = nil) - @case_sensitive = false unless instance_variable_defined?(:@case_sensitive) - return @case_sensitive unless value - @case_sensitive = !!value - end - - def case_sensitive? - !!@case_sensitive - end - - def compared_attrs - @compared_attrs ||= [] - @compared_attrs - end - end - include Eco::Language::AuxiliarLogger attr_reader :source1, :source2 attr_reader :src_h1, :src_h2 - class_resolver :diff_result_class, "Eco::Data::Hash::DiffResult" - - def initialize(source1, source2, logger: nil, **kargs) + def initialize(source1, source2, logger: nil) @logger = logger if logger - @options = kargs @source1 = source1 @source2 = source2 @src_h1 = by_key(source1) @src_h2 = by_key(source2) raise "Missing source1" unless !!self.src_h1 @@ -65,18 +34,15 @@ # @return [Boolean] wheter or not there are differences. def diffs? !diffs.empty? end + # All the items that contain the diff of a node. + # @return [Array<Eco::Data::Hash::DiffResult>] def source_results @source_results ||= paired_sources.each_with_object([]) do |(src1, src2), res| - args = { - key: key, - compare: compared_attrs, - case_sensitive: case_sensitive? - } - res << diff_result_class.new(src1, src2, **args) + res << diff_result_class.new(src1, src2) end end protected @@ -89,37 +55,25 @@ keys2 = src_h2.keys all_keys = keys1 | keys2 all_keys.map {|key| [src_h1[key], src_h2[key]]} end + # @return [String] the `key` attribute of `diff_result_class` def key - @key ||= options_or(:key) do - self.class.key - end.tap do |k| - raise "missing main key attr to pair records. Given: #{k}" unless k.is_a?(String) + diff_result_class.key.tap do |k| + raise "#{diff_result_class}: missing main key attr to pair records. Given: #{k}" unless k.is_a?(String) end end def case_sensitive? - @case_sensitive ||= options_or(:case_sensitive) { self.class.case_sensitive? } + diff_result_class.case_sensitive? end def compared_attrs - @compared_attrs ||= options_or(:compared_attrs) do - self.class.compared_attrs - end.yield_self do |attrs| - raise "compared_attrs should be an array" unless attrs.is_a?(Array) - attrs.map(&:to_s) - end + diff_result_class.compared_attrs.map(&:to_s) end private - - def options_or(opt) - opt = opt.to_sym - return @options[opt] if @options.key?(opt) - yield - end def symbolize_keys(hash) hash.each_with_object({}) do |(k, v), h| h[k.to_sym] = v end