class Shaf::Upgrade::Version

Attributes

major[R]
minor[R]
patch[R]

Public Class Methods

new(version) click to toggle source
# File lib/shaf/upgrade/version.rb, line 16
def initialize(version)
  case version
  when Version
    @major, @minor, @patch = version.to_a
  when /\d+\.\d+(\.\d+)?/
    @major, @minor, @patch = split_version(version)
  else
    raise UpgradeVersionError, version
  end
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/shaf/upgrade/version.rb, line 27
def <=>(other)
  return unless other
  other = self.class.new(other)
  compare_version(other.major, other.minor, other.patch)
end
to_a() click to toggle source
# File lib/shaf/upgrade/version.rb, line 37
def to_a
  [major, minor, patch]
end
to_s() click to toggle source
# File lib/shaf/upgrade/version.rb, line 33
def to_s
  to_a.join('.')
end

Private Instance Methods

compare_version(other_major, other_minor, other_patch) click to toggle source
# File lib/shaf/upgrade/version.rb, line 49
def compare_version(other_major, other_minor, other_patch)
  [
    major <=> other_major,
    minor <=> other_minor,
    patch <=> other_patch
  ].find { |x| !x.zero? } || 0
end
split_version(str) click to toggle source
# File lib/shaf/upgrade/version.rb, line 43
def split_version(str)
  str.split('.').map(&:to_i).tap do |list|
    list << 0 while list.size < 3
  end
end