class Semvruler::Order

Attributes

chain[R]

Public Class Methods

new(*chain) click to toggle source
# File lib/semvruler/order.rb, line 5
def initialize(*chain)
  @chain = chain
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/semvruler/order.rb, line 9
def <=>(other)
  index = (0..2).find { |idx| compare_at(other, idx) != 0 }
  return compare_at(other, index) if index
  return compare_missed_lenght(other) if compare_missed_lenght(other) != 0

  index = (3..[size, other.size].max).find { |idx| compare_at(other, idx) != 0 }
  return compare_at(other, index) if index

  no_difference_found
end

Protected Instance Methods

[](idx) click to toggle source
# File lib/semvruler/order.rb, line 45
def [](idx)
  chain[idx]
end
compare_at(other, idx) click to toggle source
# File lib/semvruler/order.rb, line 36
def compare_at(other, idx)
  is_numeric = int_at?(idx) || other.int_at?(idx)

  current = is_numeric ? read_int_at(idx) : self[idx].to_s
  nxt = is_numeric ? other.read_int_at(idx) : other[idx].to_s

  current <=> nxt
end
compare_missed_lenght(other) click to toggle source
# File lib/semvruler/order.rb, line 28
def compare_missed_lenght(other)
  if size == 3 || other.size == 3
    (size <=> other.size) * -1
  else
    no_difference_found
  end
end
int_at?(idx) click to toggle source
# File lib/semvruler/order.rb, line 57
def int_at?(idx)
  /^\d+/.match?(self[idx].to_s)
end
no_difference_found() click to toggle source
# File lib/semvruler/order.rb, line 24
def no_difference_found
  0
end
read_int_at(idx) click to toggle source
# File lib/semvruler/order.rb, line 49
def read_int_at(idx)
  if int_at?(idx)
    self[idx].to_i
  else
    self[idx].nil? ? -1 : Float::INFINITY
  end
end
size() click to toggle source
# File lib/semvruler/order.rb, line 61
def size
  chain.size
end