Sha256: ccd18ffa37aed3f6bc885b14ae1fd708fccf7684bd07767fac319ba556dd380e

Contents?: true

Size: 1.74 KB

Versions: 40

Compression:

Stored size: 1.74 KB

Contents

module Eco::API::UseCases::GraphQL::Helpers::Location
  class TagsRemap
    class TagsMap
      attr_reader :from, :to

      def initialize(from, to)
        @from = TagsSet.new(from)
        @to   = TagsSet.new(to)
      end

      def to_csv_row
        [from.to_s, to.to_s]
      end

      # @note to create a stable sort we assume `self` is a sorted element
      def <=>(other) # rubocop:disable Metrics/AbcSize
        return -1 if maps?   && !other.maps?
        return  1 if !maps?  && other.maps?
        return -1 if rename? && other.move?
        return  1 if move?   && other.rename?
        return -1 if rename? && other.rename?
        # both are being moved (specific/long mappings first)
        return  1 if from.subset_of?(other.from)
        return -1 if from.superset_of?(other.from)
        return -1 if (from & other.from).empty?
        return -1 if from.length >= other.from.length
        return  1 if from.length <  other.from.length
        -1
      end

      # @note to create a stable sort we assume `self` is a sorted element
      def goes_before?(other)
        (self <=> other) == -1
      end

      # @note to create a stable sort we assume `self` is a sorted element
      def goes_after?(other)
        (self <=> other) == 1
      end

      def both?(&block)
        [from, to].all?(&block)
      end

      def any?(&block)
        [from, to].any?(&block)
      end

      def maps?
        return false if any?(&:empty?)
        return false if from == to
        true
      end

      def rename?
        return false unless maps?
        both? {|set| set.length == 1}
      end

      def move?
        return false unless maps?
        !rename?
      end
    end
  end
end

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.20 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.19 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.18 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.17 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.16 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.15 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.14 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.13 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.12 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.11 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.10 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.9 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.8 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.7 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.6 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.5 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.4 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.3 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-3.0.2 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb