module Eco::API::UseCases::GraphQL::Helpers::Location class TagsRemap def self.correct_pair?(pair) correct_pair = pair.is_a?(Array) correct_pair && pair.length == 2 end attr_reader :src_maps attr_reader :from, :to def each(&block) return to_enum(:each) unless block src_maps.each(&block) end def to_csv(filename) CSV.open(filename, "w") do |fd| fd << ["src_tags", "dst_tags"] each do |tags_map| fd << tags_map.to_csv_row end end end def to_s "".tap do |str| each.map do |tags_map| from, to = tags_map.to_csv_row str << " • from: #{from}\n" str << " • to: #{to}\n" end end end # Sorts the maps in the correct order # @note it assumes that: # 1. renames go before moving nodes # 2. moving nodes does not include a rename # 3. moving nodes includes all the path on both, source and destination tags # @note DISABLED: the order should be that in what the operations happened! # An important thing is that all the path is always included in the mappings. # * The only way that this would work is to apply upper (previous) tags_maps # to subsequent ones. But the order should still be kept! # def sorted_maps # [].tap do |sorted| # src_maps.each do |curr_map| # pos = nil # sorted.each_with_index.reverse_each do |prev_map, idx| # pos = idx + 1 if prev_map.goes_before?(curr_map) # break if pos # end # sorted.insert(pos || 0, curr_map) # end # end # end def empty? src_maps.empty? end def <<(pair) raise ArgumentError, "Expecting pair of Array in Array. Given: #{pair}" unless self.class.correct_pair?(pair) add(*pair) end def add(from, to) raise ArgumentError, "Expecting Array. Given: #{from.class}" unless from.is_a?(Array) raise ArgumentError, "Expecting Array. Given: #{to.class}" unless to.is_a?(Array) new_src TagsMap.new(from, to) end private attr_reader :src_maps attr_reader :from_key, :to_key def new_src(tags_map) src_maps << tags_map end def src_maps @src_maps ||= [] end end end require_relative 'tags_remap/tags_map' require_relative 'tags_remap/tags_set'