Class: SemiSemantic::VersionSegment

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/semi_semantic/version_segment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components) ⇒ VersionSegment

Construction can throw ArgumentError, but does no parsing or type-conversion



27
28
29
30
31
# File 'lib/semi_semantic/version_segment.rb', line 27

def initialize(components)
  raise ArgumentError.new 'Invalid Version Components: nil' if components.nil?
  raise ArgumentError.new "Invalid Version Components: #{components}" if components.empty? || components.include?('')
  @components = components
end

Instance Attribute Details

#componentsObject (readonly)

TODO: immutable?



8
9
10
# File 'lib/semi_semantic/version_segment.rb', line 8

def components
  @components
end

Class Method Details

.parse(component_string) ⇒ Object

Converts a string into a VersionCluster Raises a ParseError if the string format is invalid Raises an ArgumentError if version_string is nil



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/semi_semantic/version_segment.rb', line 13

def self.parse(component_string)
  raise ArgumentError.new 'Invalid Version Component String: nil' if component_string.nil?
  self.new(component_string.split('.').map do |v|
    if v.match(/\A[0-9]+\z/)
      v.to_i
    elsif v.match(/\A[0-9A-Za-z_\-]+\z/)
      v
    else
      raise ParseError.new 'Invalid Version Component Format: Requires alphanumerics and hyphens only'
    end
  end)
end

Instance Method Details

#<=>(other) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/semi_semantic/version_segment.rb', line 33

def <=>(other)
  a = @components
  b = other.components
  if a.size > b.size
    comparison = a[0...b.size] <=> b
    return comparison unless comparison == 0
    a[b.size..-1].each {|v| return 1 unless v == 0 }
    0
  elsif a.size < b.size
    comparison = a <=> b[0...a.size]
    return comparison unless comparison == 0
    b[a.size..-1].each {|v| return -1 unless v == 0 }
    0
  else
    a <=> b
  end
end

#decrement(index = -1)) ⇒ Object

Returns a copy of the VersionCluster with the integer at the provided index decremented by one. Raises a TypeError if the value at that index is not an integer. Raises a RangeError if the value is zero or less



74
75
76
77
78
79
80
81
82
# File 'lib/semi_semantic/version_segment.rb', line 74

def decrement(index=-1)
  value = @components[index]
  raise TypeError.new "'#{value}' is not an integer" unless value.is_a? Integer
  raise RangeError.new "'#{value}' is zero or less" unless value > 0

  copy = Array.new @components
  copy[index] = value - 1
  self.class.new copy
end

#increment(index = -1)) ⇒ Object

Returns a copy of the VersionCluster with the integer at the provided index incremented by one. Raises a TypeError if the value at that index is not an integer.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/semi_semantic/version_segment.rb', line 53

def increment(index=-1)
  value = @components[index]
  raise TypeError.new "'#{value}' is not an integer" unless value.is_a? Integer

  copy = Array.new @components
  copy[index] = value + 1

  while index < copy.size && index != -1
    index += 1
    value = copy[index]
    if value.is_a? Integer
      copy[index] = 0
    end
  end

  self.class.new copy
end

#to_sObject



84
85
86
# File 'lib/semi_semantic/version_segment.rb', line 84

def to_s
  @components.join('.')
end