Class: Semantic::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/semantic/version.rb

Constant Summary collapse

SemVerRegexp =
/\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*))*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_str) ⇒ Version

Returns a new instance of Version.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/semantic/version.rb', line 11

def initialize version_str
  v = version_str.match(SemVerRegexp)

  raise ArgumentError.new("#{version_str} is not a valid SemVer Version (http://semver.org)") if v.nil?
  @major = v[1].to_i
  @minor = v[2].to_i
  @patch = v[3].to_i
  @pre = v[4]
  @build = v[5]
  @version = version_str
end

Instance Attribute Details

#buildObject

Returns the value of attribute build.



9
10
11
# File 'lib/semantic/version.rb', line 9

def build
  @build
end

#majorObject

Returns the value of attribute major.



9
10
11
# File 'lib/semantic/version.rb', line 9

def major
  @major
end

#minorObject

Returns the value of attribute minor.



9
10
11
# File 'lib/semantic/version.rb', line 9

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



9
10
11
# File 'lib/semantic/version.rb', line 9

def patch
  @patch
end

#preObject

Returns the value of attribute pre.



9
10
11
# File 'lib/semantic/version.rb', line 9

def pre
  @pre
end

Instance Method Details

#<=>(other_version) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/semantic/version.rb', line 82

def <=> other_version
  other_version = Version.new(other_version) if other_version.is_a? String
  [:major, :minor, :patch].each do |part|
    c = (self.send(part) <=> other_version.send(part))
    if c != 0
      return c
    end
  end
  return compare_pre(self.pre, other_version.pre)
end

#compare_pre(prea, preb) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/semantic/version.rb', line 34

def compare_pre(prea, preb)
  if prea.nil? || preb.nil?
    return 0 if prea.nil? && preb.nil?
    return 1 if prea.nil?
    return -1 if preb.nil?
  end
  a = identifiers(prea)
  b = identifiers(preb)
  smallest = a.size < b.size ? a : b
  smallest.each_with_index do |e, i|
    c = a[i] <=> b[i]
    if c.nil?
      return a[i].is_a?(Integer) ? -1 : 1
    elsif c != 0
      return c
    end
  end
  return a.size <=> b.size
end

#eql?(other_version) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/semantic/version.rb', line 78

def eql? other_version
  self.hash == other_version.hash
end

#hashObject



74
75
76
# File 'lib/semantic/version.rb', line 74

def hash
  to_a.hash
end

#identifiers(pre) ⇒ Object



28
29
30
31
32
# File 'lib/semantic/version.rb', line 28

def identifiers(pre)
  array = pre.split(".")
  array.each_with_index {|e,i| array[i] = Integer(e) if /\A\d+\z/.match(e)}
  return array
end

#increment!(term) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/semantic/version.rb', line 122

def increment!(term)
  term = term.to_sym
  new_version = clone
  new_value = send(term) + 1

  new_version.send("#{term}=", new_value)
  new_version.minor = 0 if term == :major
  new_version.patch = 0 if term == :major || term == :minor
  new_version.build = new_version.pre = nil

  new_version
end

#satisfied_by?(versions) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


113
114
115
116
# File 'lib/semantic/version.rb', line 113

def satisfied_by? versions
  raise ArgumentError.new("Versions #{versions} should be an array of versions") unless versions.is_a? Array
  versions.all? { |version| satisfies?(version) }
end

#satisfies?(other_version) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/semantic/version.rb', line 93

def satisfies? other_version
  return true if other_version.strip == '*'
  parts = other_version.split(/(\d(.+)?)/, 2)
  comparator, other_version_string = parts[0].strip, parts[1].strip

  begin
    Version.new other_version_string
    comparator.empty? && comparator = '=='
    satisfies_comparator? comparator, other_version_string
  rescue ArgumentError
    if ['<', '>', '<=', '>='].include?(comparator)
      satisfies_comparator? comparator, pad_version_string(other_version_string)
    elsif comparator == '~>'
      pessimistic_match? other_version_string
    else
      tilde_matches? other_version_string
    end
  end
end

#to_aObject Also known as: to_array



54
55
56
# File 'lib/semantic/version.rb', line 54

def to_a
  [@major, @minor, @patch, @pre, @build]
end

#to_hObject Also known as: to_hash



65
66
67
68
# File 'lib/semantic/version.rb', line 65

def to_h
  keys = [:major, :minor, :patch, :pre, :build]
  Hash[keys.zip(self.to_a)]
end

#to_sObject Also known as: to_string



58
59
60
61
62
63
# File 'lib/semantic/version.rb', line 58

def to_s
  str = [@major, @minor, @patch].join '.'
  str << '-' << @pre unless @pre.nil?
  str << '+' << @build unless @build.nil?
  str
end