File: boundary.rb

package info (click to toggle)
ruby-semver-dialects 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 156 kB
  • sloc: ruby: 1,537; makefile: 4
file content (74 lines) | stat: -rw-r--r-- 1,533 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

# Boundary is a boundary used in an interval.
# It can either be above all versions (infinity),
# below all versions (negative infinity), or any version.
module SemverDialects
  class Boundary
    include Comparable

    attr_accessor :semver

    def initialize(semver)
      @semver = semver
    end

    def to_s
      @semver.to_s
    end

    def <=>(other)
      return nil unless other.is_a?(Boundary)
      return -1 if other.instance_of?(AboveAll)
      return 1 if other.instance_of?(BelowAll)

      semver <=> other.semver
    end

    def is_initial_version?
      @semver.is_zero?
    end
  end

  # BelowAll represents a boundary below all possible versions.
  # When used as the lower boundary of an interval, any version
  # that is smaller than the upper boundary is in the interval.
  class BelowAll < Boundary
    def initialize; end

    def to_s
      '-inf'
    end

    def is_initial_version?
      false
    end

    def <=>(other)
      return 0 if other.instance_of?(BelowAll)

      -1 if other.is_a?(Boundary)
    end
  end

  # AboveAll represents a boundary above all possible versions.
  # When used as the upper boundary of an interval, any version
  # that is greater than the lower boundary is in the interval.
  class AboveAll < Boundary
    def initialize; end

    def to_s
      '+inf'
    end

    def is_initial_version?
      false
    end

    def <=>(other)
      return 0 if other.instance_of?(AboveAll)

      1 if other.is_a?(Boundary)
    end
  end
end