summaryrefslogtreecommitdiff
path: root/lib/gitlab/relative_positioning/range.rb
blob: 3214c72eb8b5b04ce2d1d8beea4c8912ee22a70b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true

module Gitlab
  module RelativePositioning
    class Range
      attr_reader :lhs, :rhs

      def open_on_left?
        lhs.nil?
      end

      def open_on_right?
        rhs.nil?
      end

      def cover?(item_context)
        return false unless item_context
        return false unless item_context.positioned?
        return true if item_context.object == lhs&.object
        return true if item_context.object == rhs&.object

        pos = item_context.relative_position

        return lhs.relative_position < pos if open_on_right?
        return pos < rhs.relative_position if open_on_left?

        lhs.relative_position < pos && pos < rhs.relative_position
      end

      def ==(other)
        other.is_a?(RelativePositioning::Range) && lhs == other.lhs && rhs == other.rhs
      end
    end
  end
end