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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_str) ⇒ VersionSpec

Returns a new instance of VersionSpec.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/udb/version_spec.rb', line 66

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
  @hash = [@major, @minor, @patch, @pre].hash
end

Instance Attribute Details

#majorInteger (readonly)

Returns Major version number.

Returns:

  • (Integer)

    Major version number



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

def major
  @major
end

#minorInteger (readonly)

Returns Minor version number.

Returns:

  • (Integer)

    Minor version number



57
58
59
# File 'lib/udb/version_spec.rb', line 57

def minor
  @minor
end

#patchInteger (readonly)

Returns Patch version number.

Returns:

  • (Integer)

    Patch version number



60
61
62
# File 'lib/udb/version_spec.rb', line 60

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



63
64
65
# File 'lib/udb/version_spec.rb', line 63

def pre
  @pre
end

Class Method Details

.new(version_str) ⇒ Object



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

def self.new(version_str)
  @intern_cache[version_str] ||= super
end

Instance Method Details

#<=>(other) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/udb/version_spec.rb', line 115

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

#canonicalObject



93
94
95
# File 'lib/udb/version_spec.rb', line 93

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

#decrement_patchObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/udb/version_spec.rb', line 157

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

Returns:

  • (Boolean)


136
137
138
139
140
141
142
# File 'lib/udb/version_spec.rb', line 136

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

#hashObject



145
146
147
# File 'lib/udb/version_spec.rb', line 145

def hash
  @hash
end

#increment_patchObject



150
151
152
153
154
# File 'lib/udb/version_spec.rb', line 150

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

#inspectObject



87
88
89
# File 'lib/udb/version_spec.rb', line 87

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

#to_rvi_sObject



102
103
104
105
106
107
108
# File 'lib/udb/version_spec.rb', line 102

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_sObject



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

def to_s = @version_str