Class: Udb::VersionSpec

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Comparable
Defined in:
lib/udb/version_spec.rb

Overview

Represents an RVI version specifier

Version specs have the form:

MAJOR[.MINOR[.PATCH[-pre]]]

Where MAJOR, MINOR, and PATCH are integers and “pre” is an optional string

Notably, these DO NOT represent a Semantic Version (semver.og).

Rather, versions are treated as follows:

* Versions are assumed to be backward compatible by default.
   For example,
      - 2.0 is compatible with 1.0
      - 1.1 is compatible with 1.0
      - 0.9 is *not* compatible with 1.0
* A version can be explicitly marked as "breaking" in the architecture definition
    Breaking versions are not backward compatible with any smaller versions
    For example, if version 2.2 is Breaking,
      - 3.0 is compatible with 2.2
      - 2.3 is compatible with 2.2
      - 3.0 is *not* compatible with 2.0
      - 2.2 is *not* compatible with 2.0
      - 2.1 is compatible with 2.0

Constant Summary collapse

VERSION_REGEX =
/([0-9]+)(?:\.([0-9]+)(?:\.([0-9]+)(?:-(pre))?)?)?/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_str)

Parameters:

  • version_str (String)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/udb/version_spec.rb', line 56

def initialize(version_str)
  if version_str =~ /^\s*#{VERSION_REGEX}\s*$/
    m = T.must(::Regexp.last_match)
    @major = m[1].to_i
    @minor_given = !m[2].nil?
    @minor = @minor_given ? m[2].to_i : 0
    @patch_given = !m[3].nil?
    @patch = @patch_given ? m[3].to_i : 0
    @pre = !m[4].nil?
    @major.freeze
    @minor.freeze
    @patch.freeze
    @pre.freeze
  else
    raise ArgumentError, "#{version_str} is not a valid Version spec"
  end
  @version_str = version_str
end

Instance Attribute Details

#majorInteger (readonly)

Returns Major version number.

Returns:

  • (Integer)

    Major version number



44
45
46
# File 'lib/udb/version_spec.rb', line 44

def major
  @major
end

#minorInteger (readonly)

Returns Minor version number.

Returns:

  • (Integer)

    Minor version number



47
48
49
# File 'lib/udb/version_spec.rb', line 47

def minor
  @minor
end

#patchInteger (readonly)

Returns Patch version number.

Returns:

  • (Integer)

    Patch version number



50
51
52
# File 'lib/udb/version_spec.rb', line 50

def patch
  @patch
end

#preBoolean (readonly)

Returns Whether or not this is a pre-release.

Returns:

  • (Boolean)

    Whether or not this is a pre-release



53
54
55
# File 'lib/udb/version_spec.rb', line 53

def pre
  @pre
end

Instance Method Details

#<=>(other) ⇒ Integer?

Parameters:

  • other (T.untyped)

Returns:

  • (Integer, nil)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/udb/version_spec.rb', line 104

def <=>(other)
  if other.is_a?(String)
    VersionSpec.new(other) <=> self
  elsif other.is_a?(VersionSpec)
    if @major != other.major
      @major <=> other.major
    elsif @minor != other.minor
      @minor <=> other.minor
    elsif @patch != other.patch
      @patch <=> other.patch
    elsif @pre != other.pre
      @pre ? 1 : -1
    else
      0
    end
  else
    nil
  end
end

#canonicalString

Returns The version, in canonical form.

Returns:

  • (String)

    The version, in canonical form



82
83
84
# File 'lib/udb/version_spec.rb', line 82

def canonical
  "#{@major}.#{@minor}.#{@patch}#{@pre ? '-pre' : ''}"
end

#decrement_patchVersionSpec

Returns:



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/udb/version_spec.rb', line 146

def decrement_patch
  copy = dup
  if @patch > 0
    copy.instance_variable_set(:@patch, @patch - 1)
  elsif @minor > 0
    copy.instance_variable_set(:@minor, @minor - 1)
    copy.instance_variable_set(:@patch, 9999)
  elsif @major > 0
    copy.instance_variable_set(:@major, @major - 1)
    copy.instance_variable_set(:@minor, 9999)
    copy.instance_variable_set(:@patch, 9999)
  else
    raise "Cannot decrement version 0"
  end
  copy
end

#eql?(other) ⇒ Boolean

Parameters:

  • other (T.untyped)

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/udb/version_spec.rb', line 125

def eql?(other)
  if other.is_a?(VersionSpec)
    self.hash == other.hash
  else
    false
  end
end

#hashInteger

Returns:

  • (Integer)


134
135
136
# File 'lib/udb/version_spec.rb', line 134

def hash
  [@major, @minor, @patch, @pre].hash
end

#increment_patchVersionSpec

Returns:



139
140
141
142
143
# File 'lib/udb/version_spec.rb', line 139

def increment_patch
  copy = dup
  copy.instance_variable_set(:@patch, @patch + 1)
  copy
end

#inspectString

Returns:

  • (String)


76
77
78
# File 'lib/udb/version_spec.rb', line 76

def inspect
  "VersionSpec[str: #{@version_str}; major: #{@major}, minor: #{@minor}, patch: #{@patch}, pre: #{@pre}]"
end

#to_rvi_sString

Returns The version formatted like RVI docs.

Examples:

VersionSpec.new("2.2").to_rvi_s #=> "2p2"

Returns:

  • (String)

    The version formatted like RVI docs



91
92
93
94
95
96
97
# File 'lib/udb/version_spec.rb', line 91

def to_rvi_s
  s = @major.to_s
  s += "p#{@minor}" if @minor_given
  s += "p#{@patch}" if @patch_given
  s += "-pre" if @pre
  s
end

#to_sString

Returns The exact string used during construction.

Returns:

  • (String)

    The exact string used during construction



101
# File 'lib/udb/version_spec.rb', line 101

def to_s = @version_str