lib/semantic/version.rb in semantic-1.5.0 vs lib/semantic/version.rb in semantic-1.6.0

- old
+ new

@@ -1,8 +1,10 @@ # See: http://semver.org module Semantic class Version + include Comparable + SemVerRegexp = /\A(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][a-zA-Z0-9-]*))*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?\Z/ attr_accessor :major, :minor, :patch, :pre, :build @@ -86,31 +88,11 @@ end end return compare_pre(self.pre, other_version.pre) end - def > other_version - (self <=> other_version) == 1 - end - - def < other_version - (self <=> other_version) == -1 - end - - def >= other_version - (self <=> other_version) >= 0 - end - - def <= other_version - (self <=> other_version) <= 0 - end - - def == other_version - (self <=> other_version) == 0 - end - - def satisfies other_version + def satisfies? other_version return true if other_version.strip == '*' parts = other_version.split(/(\d(.+)?)/, 2) comparator, other_version_string = parts[0].strip, parts[1].strip begin @@ -118,16 +100,23 @@ comparator.empty? && comparator = '==' satisfies_comparator? comparator, other_version_string rescue ArgumentError if ['<', '>', '<=', '>='].include?(comparator) satisfies_comparator? comparator, pad_version_string(other_version_string) + elsif comparator == '~>' + pessimistic_match? other_version_string else tilde_matches? other_version_string end end end + def satisfied_by? versions + raise ArgumentError.new("Versions #{versions} should be an array of versions") unless versions.is_a? Array + versions.all? { |version| satisfies?(version) } + end + [:major, :minor, :patch].each do |term| define_method("#{term}!") { increment!(term) } end def increment!(term) @@ -157,15 +146,34 @@ this_parts = to_a.collect(&:to_s) other_parts = other_version_string.split('.').reject {|x| x == '*'} other_parts == this_parts[0..other_parts.length-1] end + def pessimistic_match? other_version_string + other_parts = other_version_string.split('.') + unless other_parts.size == 2 || other_parts.size == 3 + raise ArgumentError.new("Version #{other_version_string} should not be applied with a pessimistic operator") + end + other_parts.pop + other_parts << (other_parts.pop.to_i + 1).to_s + satisfies_comparator?('>=', semverified(other_version_string)) && satisfies_comparator?('<', semverified(other_parts.join('.'))) + end + def satisfies_comparator? comparator, other_version_string if comparator == '~' tilde_matches? other_version_string + elsif comparator == '~>' + pessimistic_match? other_version_string else self.send comparator, other_version_string end + end + + def semverified version_string + parts = version_string.split('.') + raise ArgumentError.new("Version #{version_string} not supported by semverified") if parts.size > 3 + (3 - parts.size).times { parts << '0' } + parts.join('.') end end end