Class: Semantic::Version

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

Overview

See: semver.org

Constant Summary collapse

SemVerRegexp =
/^(\d+\.\d+\.\d+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_str) ⇒ Version

Returns a new instance of Version.

Raises:

  • (ArgumentError)


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

def initialize version_str
  raise ArgumentError.new("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.



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

def build
  @build
end

#majorObject

Returns the value of attribute major.



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

def major
  @major
end

#minorObject

Returns the value of attribute minor.



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

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



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

def patch
  @patch
end

#preObject

Returns the value of attribute pre.



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

def pre
  @pre
end

Instance Method Details

#<(other_version) ⇒ Object



52
53
54
# File 'lib/semantic/version.rb', line 52

def < other_version
  if (self <=> other_version) == -1 then true else false end
end

#<=(other_version) ⇒ Object



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

def <= other_version
  if (self <=> other_version) <= 0 then true else false end
end

#<=>(other_version) ⇒ Object



43
44
45
46
# File 'lib/semantic/version.rb', line 43

def <=> other_version
  other_version = Version.new(other_version) if other_version.is_a? String
  compare_recursively self.to_a.dup, other_version.to_a.dup
end

#==(other_version) ⇒ Object



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

def == other_version
  if (self <=> other_version) == 0 then true else false end
end

#>(other_version) ⇒ Object



48
49
50
# File 'lib/semantic/version.rb', line 48

def > other_version
  if (self <=> other_version) == 1 then true else false end
end

#>=(other_version) ⇒ Object



56
57
58
# File 'lib/semantic/version.rb', line 56

def >= other_version
  if (self <=> other_version) >= 0 then true else false end
end

#to_aObject Also known as: to_array



22
23
24
# File 'lib/semantic/version.rb', line 22

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

#to_hObject Also known as: to_hash



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

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

#to_sObject Also known as: to_string



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

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

  str
end