File: pre_release.rb

package info (click to toggle)
ruby-semantic-range 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 100 kB
  • sloc: ruby: 731; makefile: 4
file content (92 lines) | stat: -rw-r--r-- 1,678 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
module SemanticRange
  class PreRelease
    attr_reader :parts

    def initialize(input)
      @parts = parse(input)
    end

    def parse(str)
      str.to_s.split('.').map { |id| convert(id) }
    end

    def convert(str)
      str.match(/^[0-9]+$/) ? str.to_i : str
    end

    def length
      parts.length
    end

    def empty?
      parts.empty?
    end

    def to_s
      parts.join '.'
    end

    def clear!
      @parts = []
    end

    def zero!
      @parts = [0]
    end

    def <=>(other)
      return unless other.is_a?(self.class)

      return -1 if parts.any? && !other.parts.any?
      return 1 if !parts.any? && other.parts.any?
      return 0 if !parts.any? && !other.parts.any?

      i = 0
      while true
        a = parts[i]
        b = other.parts[i]

        if a.nil? && b.nil?
          return 0
        elsif b.nil?
          return 1
        elsif a.nil?
          return -1
        elsif a == b

        else
          return Version.compare_identifiers(a, b)
        end
        i += 1
      end
    end

    def last_number_index
      parts.rindex { |e| e.is_a? Fixnum }
    end

    def increment!(identifier = nil)
      if empty?
        zero!
      else
        if last_number_index
          @parts[last_number_index] += 1
        else
          @parts << 0
        end
      end

      if identifier
        # 1.2.0-beta.1 bumps to 1.2.0-beta.2,
        # 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
        if parts[0] == identifier
          unless parts[1].kind_of?(Fixnum)
            @parts = [identifier, 0]
          end
        else
          @parts = [identifier, 0]
        end
      end
    end
  end
end