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
# 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?
  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:

Returns:

  • (Integer, nil)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/udb/version_spec.rb', line 100

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
    T.absurd(other)
  end
end

#canonicalString

Returns The version, in canonical form.

Returns:

  • (String)

    The version, in canonical form



78
79
80
# File 'lib/udb/version_spec.rb', line 78

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

#eql?(other) ⇒ Boolean

Returns Whether or not other is an VersionSpec with the same canonical version.

Parameters:

Returns:

  • (Boolean)

    Whether or not other is an VersionSpec with the same canonical version



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/udb/version_spec.rb', line 123

def eql?(other)
  if other.is_a?(String)
    eql?(VersionSpec.new(other))
  elsif other.is_a?(VersionSpec)
    other.major == @major && \
      other.minor == @minor && \
      other.patch == @patch && \
      other.pre == @pre
  else
    T.absurd(other)
  end
end

#inspectString

Returns:

  • (String)


72
73
74
# File 'lib/udb/version_spec.rb', line 72

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



87
88
89
90
91
92
93
# File 'lib/udb/version_spec.rb', line 87

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



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

def to_s = @version_str