Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. class InvalidSemanticVersionError < StandardError; end
  2.  
  3. class SemanticVersion
  4.  
  5. def initialize(sem_str)
  6. raise InvalidSemanticVersionError unless _valid_schema? sem_str
  7. @version_ary = sem_str.split('.').freeze
  8. end
  9.  
  10. def version_ary
  11. @version_ary
  12. end
  13.  
  14. def version
  15. @version_ary.join('')
  16. end
  17.  
  18. def compare(right, method)
  19. right_ary = right.version_ary
  20. result = false
  21. @version_ary.
  22. zip(right_ary).
  23. each {|l, r| next result = true if l.to_i.send(method, r.to_i) }
  24. result
  25. end
  26.  
  27. def method_missing(method, *args)
  28. case method
  29. when :>, :<, :>=, :<=, :==
  30. compare args[0], method
  31. else
  32. raise NoMethodError
  33. end
  34. end
  35.  
  36. private
  37.  
  38. def _valid_schema?(sem_str)
  39. !!(sem_str =~ /^(\d+\.)*\d+$/)
  40. end
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement