Skip to content

Instantly share code, notes, and snippets.

@bokuo-okubo
Created March 30, 2017 10:27
Show Gist options
  • Save bokuo-okubo/19221395cebd68c16aa788a781232f0a to your computer and use it in GitHub Desktop.
Save bokuo-okubo/19221395cebd68c16aa788a781232f0a to your computer and use it in GitHub Desktop.
class InvalidSemanticVersionError < StandardError; end
class SemanticVersion
def initialize(sem_str)
raise InvalidSemanticVersionError unless _valid_schema? sem_str
@version_ary = sem_str.split('.').freeze
end
def version_ary
@version_ary
end
def version
@version_ary.join('')
end
def compare(right, method)
right_ary = right.version_ary
result = false
@version_ary.
zip(right_ary).
each {|l, r| next result = true if l.to_i.send(method, r.to_i) }
result
end
def method_missing(method, *args)
case method
when :>, :<, :>=, :<=, :==
compare args[0], method
else
raise NoMethodError
end
end
private
def _valid_schema?(sem_str)
!!(sem_str =~ /^(\d+\.)*\d+$/)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment