Class: Kameleoon::SemVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/kameleoon/sem_version.rb

Overview

SemVersion is a utility class for semantic version comparison

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major = 0, minor = 0, patch = 0) ⇒ SemVersion

Returns a new instance of SemVersion.



12
13
14
15
16
# File 'lib/kameleoon/sem_version.rb', line 12

def initialize(major = 0, minor = 0, patch = 0)
  @major = major
  @minor = minor
  @patch = patch
end

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



10
11
12
# File 'lib/kameleoon/sem_version.rb', line 10

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



10
11
12
# File 'lib/kameleoon/sem_version.rb', line 10

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



10
11
12
# File 'lib/kameleoon/sem_version.rb', line 10

def patch
  @patch
end

Class Method Details

.from_string(version_string) ⇒ Object



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

def self.from_string(version_string)
  return nil unless version_string.is_a?(String)

  versions = [0, 0, 0]
  version_parts = version_string.split('.')
  return nil unless version_parts.length >= 1

  version_parts.each_with_index do |part, i|
    break if i >= 3

    begin
      versions[i] = Integer(part)
    rescue ArgumentError
      Logging::KameleoonLogger.error("Invalid version component, index: %s, value: '%s'", i, part)
      return nil
    end
  end

  SemVersion.new(versions[0], versions[1], versions[2])
end

Instance Method Details

#<=>(other) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kameleoon/sem_version.rb', line 18

def <=>(other)
  return nil unless other.is_a?(SemVersion)

  c = @major <=> other.major
  return c unless c.zero?

  c = @minor <=> other.minor
  return c unless c.zero?

  @patch <=> other.patch
end

#to_fObject



30
31
32
# File 'lib/kameleoon/sem_version.rb', line 30

def to_f
  "#{@major}.#{@minor}".to_f
end