Sha256: 8744aff67503b4304ff13df7ed81ec507338c70fa1e5f8bedda960023225e2c9

Contents?: true

Size: 1.71 KB

Versions: 10

Compression:

Stored size: 1.71 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)
        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

10 entries across 10 versions & 1 rubygems

Version Path
eco-helpers-2.6.4 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.6.3 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.6.2 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.6.1 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.6.0 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.5.10 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.5.9 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.5.8 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.5.7 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb
eco-helpers-2.5.6 lib/eco/api/usecases/graphql/helpers/location/tags_remap/tags_map.rb