Class: SemanticVersion

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/epitools/semantic_version.rb

Constant Summary collapse

A_NEWER =
1
B_NEWER =
-1
A_EQ_B =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ SemanticVersion

Returns a new instance of SemanticVersion.



11
12
13
# File 'lib/epitools/semantic_version.rb', line 11

def initialize(val)
  @val = val
end

Instance Attribute Details

#valObject (readonly)

Returns the value of attribute val.



9
10
11
# File 'lib/epitools/semantic_version.rb', line 9

def val
  @val
end

Class Method Details

.compare(a, b) ⇒ Object



15
16
17
# File 'lib/epitools/semantic_version.rb', line 15

def self.compare(a,b)
  new(a) <=> new(b)
end

Instance Method Details

#<=>(other) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/epitools/semantic_version.rb', line 19

def <=>(other)
  version_a, version_b = val, other.val

  return A_EQ_B if version_a == version_b

  chars_a, chars_b = version_a.chars, version_b.chars

  while chars_a.size != 0 and chars_b.size != 0
    # logger.debug('starting loop comparing %s '
    #        'to %s', chars_a, chars_b)
    check_leading(chars_a, chars_b)

    if chars_a.first == '~' and chars_b.first == '~'
      chars_a.shift
      chars_b.shift
    elsif chars_a.first == '~'
      return B_NEWER
    elsif chars_b.first == '~'
      return A_NEWER
    end

    break if chars_a.size == 0 or chars_b.size == 0

    block_res = get_block_result(chars_a, chars_b)
    return block_res if block_res != A_EQ_B
  end

  if chars_a.size == chars_b.size
    # logger.debug('versions are equal')
    return A_EQ_B
  else
    # logger.debug('versions not equal')
    chars_a.size > chars_b.size ? A_NEWER : B_NEWER
  end
end