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 SELECTORS = %i[id name id_name insert unarchive update move archive] 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? end end def diffs_details section = '#' * 10 msg = '' if insert? msg << " #{section} I N S E R T S #{section}\n" msg << insert.map {|d| d.diff_hash.pretty_inspect}.join('') end if update? msg << "\n #{section} U P D A T E S #{section}\n" update.each do |d| flags = '' #flags << 'i' if d.id? flags << 'n' if d.diff_name? flags << 'm' if d.move? flags << 'u' if d.unarchive? msg << "<< #{flags} >> " msg << d.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('') end msg end def diffs_summary return "There were no differences identified" if diffs.empty? msg = "Identified #{diffs.count} differences:\n" msg << when_present(insert, '') do |count| " • #{count} nodes to insert\n" end msg << when_present(update, '') do |count| " • #{count} nodes to update\n" 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\n" end msg << when_present(move, '') do |count| " • #{count} nodes to move\n" end msg << when_present(unarchive, '') do |count| " • #{count} nodes to unarchive\n" end msg << when_present(archive, '') do |count| " • #{count} nodes to archive\n" end msg end private def when_present(list, default = nil) count = list.count if count > 0 yield(count) else default end end end end