class VersionParser

Attributes

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

Public Class Methods

new(major, minor, patch) click to toggle source
# File lib/version_parser.rb, line 15
def initialize(major, minor, patch)
  @major = major
  @minor = minor
  @patch = patch
end
parse(version) click to toggle source
# File lib/version_parser.rb, line 6
def self.parse(version)
  parsed = version.match(/(\d+).(\d+).(\d+)/)
  if parsed.nil?
    VersionParser.new(0, 0, 0)
  else
    VersionParser.new(parsed[1].to_i, parsed[2].to_i, parsed[3].to_i)
  end
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/version_parser.rb, line 21
def <=>(other)
  return unless other.is_a? VersionParser
  return unless valid? && other.valid?

  if other.major < @major
    1
  elsif @major < other.major
    -1
  elsif other.minor < @minor
    1
  elsif @minor < other.minor
    -1
  elsif other.patch < @patch
    1
  elsif @patch < other.patch
    -1
  else
    0
  end
end
valid?() click to toggle source
# File lib/version_parser.rb, line 42
def valid?
  @major >= 0 && @minor >= 0 && @patch >= 0 && !(@major + @minor + @patch).negative?
end