Created
March 30, 2017 10:27
-
-
Save bokuo-okubo/19221395cebd68c16aa788a781232f0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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