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
32
33
34
35
36
37
38
39
# 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: Empty Array' if components.empty?
  components.each do |component|
    unless component.is_a?(String) || component.is_a?(Integer)
      raise ArgumentError.new "Invalid Version Component Type: #{component.class}"
    end
    if component == ''
      raise ArgumentError.new 'Invalid Version Component: Empty String'
    end
  end
  @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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/semi_semantic/version_segment.rb', line 41

def <=>(other)
  a = @components
  b = other.components
  if a.size > b.size
    comparison = compare_arrays(a[0...b.size], b)
    return comparison unless comparison == 0
    return 1 unless is_all_zeros?(a[b.size..-1])
    0
  elsif a.size < b.size
    comparison = compare_arrays(a, b[0...a.size])
    return comparison unless comparison == 0
    return -1 unless is_all_zeros?(b[a.size..-1])
    0
  else
    compare_arrays(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



82
83
84
85
86
87
88
89
90
# File 'lib/semi_semantic/version_segment.rb', line 82

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/semi_semantic/version_segment.rb', line 61

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



92
93
94
# File 'lib/semi_semantic/version_segment.rb', line 92

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