lib/eco/data/locations/node_diff/nodes_diff.rb in eco-helpers-2.6.4 vs lib/eco/data/locations/node_diff/nodes_diff.rb in eco-helpers-2.7.0

- old
+ new

@@ -1,92 +1,107 @@ class Eco::Data::Locations::NodeDiff # Adjusts ArrayDiff for Nodes diffs in a location structure class NodesDiff < Eco::Data::Hashes::ArrayDiff - extend Eco::Data::Locations::NodeDiff::Selectors + require_relative 'nodes_diff/selectors' + require_relative 'nodes_diff/diffs_tree' + require_relative 'nodes_diff/clustered_treeify' - SELECTORS = %i[id name id_name insert unarchive update move archive] - selector *SELECTORS + extend Eco::Data::Locations::NodeDiff::NodesDiff::Selectors + SELECTORS = %i[ + id name id_name classifications + insert unarchive + update move + archive + ].freeze + selector(*SELECTORS) + class_resolver :diff_result_class, Eco::Data::Locations::NodeDiff attr_reader :original_tree def initialize(*args, original_tree:, **kargs, &block) super(*args, **kargs, &block) @original_tree = original_tree end def diffs - @diffs ||= super.select do |res| - res.unarchive? || res.id_name? || res.insert? || res.move? || res.archive? + @diffs ||= super.select do |dff| + # discard entries that are to be inserted and archived at the same time + next false if dff.insert? && dff.archive?(validate: false) + dff.unarchive? || dff.id_name? || dff.insert? || + dff.move? || dff.archive? end end - def diffs_details + def diffs_details # rubocop:disable Metrics/AbcSize section = '#' * 10 - msg = '' + msg = '' + if insert? msg << " #{section} I N S E R T S #{section}\n" - msg << insert.map {|d| d.diff_hash.pretty_inspect}.join('') + msg << insert.map {|dff| dff.diff_hash.pretty_inspect}.join end if update? msg << "\n #{section} U P D A T E S #{section}\n" - update.each do |d| + update.each do |dff| flags = '' - #flags << 'i' if d.id? - flags << 'n' if d.diff_name? - flags << 'm' if d.move? - flags << 'u' if d.unarchive? + flags << 'i' if dff.id? + flags << 'n' if dff.name? + flags << 'c' if dff.classifications? + flags << 'm' if dff.move? + flags << 'u' if dff.unarchive? msg << "<< #{flags} >> " - msg << d.diff_hash.pretty_inspect + msg << dff.diff_hash.pretty_inspect end end if archive? msg << "\n #{section} A R C H I V E S #{section}\n" - msg << archive.map {|d| d.diff_hash.pretty_inspect}.join('') + msg << archive.map {|dff| dff.diff_hash.pretty_inspect}.join end msg end - def diffs_summary - comp = "(#{source2.count} vs #{source1.count} nodes)" + def diffs_summary # rubocop:disable Metrics/AbcSize + comp = "(#{source_2.count} input nodes VS #{source_1.count} live nodes)" return "There were no differences identified #{comp}" if diffs.empty? msg = [] - msg << "Identified #{diffs.count} differences #{comp}:" - msg << when_present(insert, '') do |count| + msg << "Identified #{diffs.count} differences #{comp}:" + msg << when_present(insert) do |count| " • #{count} nodes to insert" end - msg << when_present(update, '') do |count| + msg << when_present(update) do |count| " • #{count} nodes to update" end - # msg << when_present(id, '') do |count| - # " • #{count} nodes to change id\n" - # end - msg << when_present(name, '') do |count| + msg << when_present(unarchive) do |count| + " • #{count} nodes to unarchive (includes ancestors of target nodes)" + end + msg << when_present(id) do |count| + " • #{count} nodes to change id\n" + end + msg << when_present(name) do |count| " • #{count} nodes to change name" end - msg << when_present(move, '') do |count| + msg << when_present(classifications) do |count| + " • #{count} nodes to change classifications" + end + msg << when_present(move) do |count| " • #{count} nodes to move" end - msg << when_present(unarchive, '') do |count| - " • #{count} nodes to unarchive" - end - msg << when_present(archive, '') do |count| + msg << when_present(archive) do |count| " • #{count} nodes to archive" end - msg.join("\n") + msg.compact.join("\n") end private def when_present(list, default = nil) + raise ArgumentError, "Expecting block but not given" unless block_given? count = list.count - if count > 0 - yield(count) - else - default - end + return yield(count) if count&.positive? + default end end end