Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement