Class: Semantic::Version

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

Constant Summary collapse

SemVerRegexp =
/\A(\d+\.\d+\.\d+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\+([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)


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

def initialize version_str
  raise ArgumentError.new("#{version_str} is not a valid SemVer Version (http://semver.org)") unless version_str =~ SemVerRegexp

  version, parts = version_str.split '-'
  if not parts.nil? and parts.include? '+'
    @pre, @build = parts.split '+'
  elsif version.include? '+'
    version, @build = version.split '+'
  else
    @pre = parts
  end


  @major, @minor, @patch = version.split('.').map(&:to_i)
end

Instance Attribute Details

#buildObject

Returns the value of attribute build.



5
6
7
# File 'lib/semantic/version.rb', line 5

def build
  @build
end

#majorObject

Returns the value of attribute major.



5
6
7
# File 'lib/semantic/version.rb', line 5

def major
  @major
end

#minorObject

Returns the value of attribute minor.



5
6
7
# File 'lib/semantic/version.rb', line 5

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



5
6
7
# File 'lib/semantic/version.rb', line 5

def patch
  @patch
end

#preObject

Returns the value of attribute pre.



5
6
7
# File 'lib/semantic/version.rb', line 5

def pre
  @pre
end

Instance Method Details

#<(other_version) ⇒ Object



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

def < other_version
  (self <=> other_version) == -1
end

#<=(other_version) ⇒ Object



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

def <= other_version
  (self <=> other_version) <= 0
end

#<=>(other_version) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/semantic/version.rb', line 48

def <=> other_version
  other_version = Version.new(other_version) if other_version.is_a? String

  v1 = self.dup
  v2 = other_version.dup

  # The build must be excluded from the comparison, so that e.g. 1.2.3+foo and 1.2.3+bar are semantically equal.
  # "Build metadata SHOULD be ignored when determining version precedence".
  # (SemVer 2.0.0-rc.2, paragraph 10 - http://www.semver.org)
  v1.build = nil
  v2.build = nil

  compare_recursively(v1.to_a, v2.to_a)
end

#==(other_version) ⇒ Object



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

def == other_version
  (self <=> other_version) == 0
end

#>(other_version) ⇒ Object



63
64
65
# File 'lib/semantic/version.rb', line 63

def > other_version
  (self <=> other_version) == 1
end

#>=(other_version) ⇒ Object



71
72
73
# File 'lib/semantic/version.rb', line 71

def >= other_version
  (self <=> other_version) >= 0
end

#increment!(term) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/semantic/version.rb', line 105

def increment!(term)
  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

#satisfies(other_version) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/semantic/version.rb', line 83

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)
    else
      tilde_matches? other_version_string
    end
  end
end

#to_aObject Also known as: to_array



27
28
29
# File 'lib/semantic/version.rb', line 27

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

#to_hObject Also known as: to_hash



39
40
41
42
# File 'lib/semantic/version.rb', line 39

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

#to_sObject Also known as: to_string



31
32
33
34
35
36
37
# File 'lib/semantic/version.rb', line 31

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

  str
end