Class: Kuby::Utils::SemVer::Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/kuby/utils/sem_ver/version.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, patch) ⇒ Version

Returns a new instance of Version.



19
20
21
22
23
# File 'lib/kuby/utils/sem_ver/version.rb', line 19

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

Instance Attribute Details

#majorObject (readonly)

Returns the value of attribute major.



7
8
9
# File 'lib/kuby/utils/sem_ver/version.rb', line 7

def major
  @major
end

#minorObject (readonly)

Returns the value of attribute minor.



7
8
9
# File 'lib/kuby/utils/sem_ver/version.rb', line 7

def minor
  @minor
end

#patchObject (readonly)

Returns the value of attribute patch.



7
8
9
# File 'lib/kuby/utils/sem_ver/version.rb', line 7

def patch
  @patch
end

Class Method Details

.parse(str, default: 0) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/kuby/utils/sem_ver/version.rb', line 9

def self.parse(str, default: 0)
  major, minor, patch = str.split('.')

  new(
    major ? major.to_i : default,
    minor ? minor.to_i : default,
    patch ? patch.to_i : default
  )
end

Instance Method Details

#<=>(other) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kuby/utils/sem_ver/version.rb', line 33

def <=>(other)
  other_arr = other.to_a

  to_a.each_with_index do |digit, idx|
    other_digit = other_arr[idx] || 0

    if digit != other_digit
      return digit <=> other_digit
    end
  end

  0
end

#to_aObject



29
30
31
# File 'lib/kuby/utils/sem_ver/version.rb', line 29

def to_a
  @arr ||= [major, minor, patch]
end

#to_sObject



25
26
27
# File 'lib/kuby/utils/sem_ver/version.rb', line 25

def to_s
  @str ||= [major, minor, patch].compact.join('.')
end