Class: Liferaft::Version

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_string) ⇒ Version

Returns a new instance of Version.



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

def initialize(version_string)
  components = version_string.downcase.split(/[a-z]/)
  character = version_string.downcase.gsub(/[^a-z]/, '')

  if character.length > 2 || character.empty?
    @major = @minor = @patch = @build = 0
    return
  end

  @major = components[0].to_i
  @minor = character.ord - 'a'.ord
  @patch = components[1].to_i / 1000
  @build = (character.length == 2 ? character[-1].ord : 0) + components[1].to_i % 1000
end

Instance Attribute Details

#buildObject (readonly)

Returns the value of attribute build.



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

def build
  @build
end

#majorObject (readonly)

Returns the value of attribute major.



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

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



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

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



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

def patch
  @patch
end

Instance Method Details

#<=>(other) ⇒ Object



30
31
32
33
34
# File 'lib/liferaft/version.rb', line 30

def <=>(other)
  %i(major minor patch build).lazy.map do |component|
    send(component) <=> other.send(component)
  end.find(&:nonzero?) || 0
end

#to_sObject



26
27
28
# File 'lib/liferaft/version.rb', line 26

def to_s
  "#{@major}.#{@minor}.#{@patch} Build #{@build}"
end