File: interval_set.rb

package info (click to toggle)
ruby-semver-dialects 3.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 172 kB
  • sloc: ruby: 1,632; makefile: 4
file content (133 lines) | stat: -rw-r--r-- 2,324 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: true

# IntervalSet is a disjunction of version intervals.
# It can express a range like "[1.0,2.0],[3.0,4.0]" (Maven syntax),
# that is between 1.0 and 2.0 (included) OR between 3.0 and 4.0 (included).
module SemverDialects
  class IntervalSet # rubocop:todo Style/Documentation
    attr_reader :intervals

    def initialize
      @intervals = []
      @interval_set = Set.new
    end

    def add(interval)
      @intervals << interval
      @interval_set.add(interval)
    end

    def <<(item)
      add(item)
    end

    def size
      @intervals.size
    end

    def to_s
      @intervals.map(&:to_s).join(',')
    end

    def to_description_s
      @intervals.map(&:to_description_s).join(', ').capitalize
    end

    def to_npm_s
      @intervals.map(&:to_npm_s).join('||')
    end

    def to_conan_s
      to_npm_s
    end

    def to_nuget_s
      to_maven_s
    end

    def to_maven_s
      @intervals.map(&:to_maven_s).join(',')
    end

    def to_gem_s
      @intervals.map(&:to_gem_s).join('||')
    end

    def to_pypi_s
      @intervals.map(&:to_pypi_s).join('||')
    end

    def to_go_s
      @intervals.map(&:to_go_s).join('||')
    end

    def to_packagist_s
      @intervals.map(&:to_packagist_s).join('||')
    end

    def to_cargo_s
      to_npm_s
    end

    def to_swift_s
      to_npm_s
    end

    def to_pub_s
      to_npm_s
    end

    def to_version_s(package_type)
      case package_type
      when 'npm'
        to_npm_s
      when 'nuget'
        to_nuget_s
      when 'maven'
        to_maven_s
      when 'gem'
        to_gem_s
      when 'pypi'
        to_pypi_s
      when 'packagist'
        to_packagist_s
      when 'go'
        to_go_s
      when 'conan'
        to_conan_s
      else
        ''
      end
    end

    def includes?(other)
      @interval_set.include?(other)
    end

    def overlaps_with?(other)
      @interval_set.each do |interval|
        return true unless interval.intersect(other).instance_of?(EmptyInterval)
      end
      false
    end

    def first
      @intervals.first
    end

    def empty?
      @intervals.empty?
    end

    def any?
      @intervals.any?
    end

    def universal?
      @intervals.each do |interval|
        return true if interval.universal?
      end
      false
    end
  end
end