lib/solve/version.rb in solve-0.3.1 vs lib/solve/version.rb in solve-0.4.0.rc1
- old
+ new
@@ -1,20 +1,25 @@
module Solve
# @author Jamie Winsor <jamie@vialstudios.com>
+ # @author Thibaud Guillaume-Gentil <thibaud@thibaud.me>
class Version
class << self
# @param [#to_s] version_string
#
# @raise [InvalidVersionFormat]
#
# @return [Array]
def split(version_string)
- major, minor, patch = case version_string.to_s
+ case version_string.to_s
+ when /^(\d+)\.(\d+)\.(\d+)(-([0-9a-z\-\.]+))?(\+([0-9a-z\-\.]+))?$/i
+ [ $1.to_i, $2.to_i, $3.to_i, $5, $7 ]
when /^(\d+)\.(\d+)\.(\d+)$/
[ $1.to_i, $2.to_i, $3.to_i ]
when /^(\d+)\.(\d+)$/
[ $1.to_i, $2.to_i, nil ]
+ when /^(\d+)$/
+ [ $1.to_i, nil, nil ]
else
raise Errors::InvalidVersionFormat.new(version_string)
end
end
end
@@ -22,34 +27,36 @@
include Comparable
attr_reader :major
attr_reader :minor
attr_reader :patch
+ attr_reader :pre_release
+ attr_reader :build
# @overload initialize(version_array)
# @param [Array] version_array
#
# @example
- # Version.new([1, 2, 3]) => #<Version: @major=1, @minor=2, @patch=3>
+ # Version.new([1, 2, 3, 'rc.1', 'build.1']) => #<Version: @major=1, @minor=2, @patch=3, @pre_release='rc.1', @build='build.1'>
#
# @overload initialize(version_string)
# @param [#to_s] version_string
#
# @example
- # Version.new("1.2.3") => #<Version: @major=1, @minor=2, @patch=3>
+ # Version.new("1.2.3-rc.1+build.1") => #<Version: @major=1, @minor=2, @patch=3, @pre_release='rc.1', @build='build.1'>
#
# @overload initialize(version)
# @param [Solve::Version] version
#
# @example
- # Version.new(Version.new("1.2.3")) => #<Version: @major=1, @minor=2, @patch=3>
+ # Version.new(Version.new("1.2.3-rc.1+build.1")) => #<Version: @major=1, @minor=2, @pre_release='rc.1', @build='build.1'>
#
def initialize(*args)
if args.first.is_a?(Array)
- @major, @minor, @patch = args.first
+ @major, @minor, @patch, @pre_release, @build = args.first
else
- @major, @minor, @patch = self.class.split(args.first.to_s)
+ @major, @minor, @patch, @pre_release, @build = self.class.split(args.first.to_s)
end
@major ||= 0
@minor ||= 0
@patch ||= 0
@@ -57,28 +64,69 @@
# @param [Solve::Version] other
#
# @return [Integer]
def <=>(other)
- [:major, :minor, :patch].each do |method|
- ans = (self.send(method) <=> other.send(method))
+ [:major, :minor, :patch].each do |release|
+ ans = self.send(release) <=> other.send(release)
return ans if ans != 0
end
+ ans = pre_release_and_build_presence_score <=> other.pre_release_and_build_presence_score
+ return ans if ans != 0
+ ans = identifiers_comparaison(other, :pre_release)
+ return ans if ans != 0
+ if build && other.build
+ return identifiers_comparaison(other, :build)
+ else
+ return build.to_s <=> other.build.to_s
+ end
0
end
+ # @return [Array]
+ def identifiers(release)
+ send(release).to_s.split('.').map do |str|
+ str.to_i.to_s == str ? str.to_i : str
+ end
+ end
+
+ # @return [Integer]
+ def pre_release_and_build_presence_score
+ pre_release ? 0 : (build.nil? ? 1 : 2)
+ end
+
# @param [Solve::Version] other
#
+ # @return [Integer]
+ def identifiers_comparaison(other, release)
+ [identifiers(release).length, other.identifiers(release).length].max.times do |i|
+ if identifiers(release)[i].class == other.identifiers(release)[i].class
+ ans = identifiers(release)[i] <=> other.identifiers(release)[i]
+ return ans if ans != 0
+ elsif identifiers(release)[i] && other.identifiers(release)[i]
+ return identifiers(release)[i].class.to_s <=> other.identifiers(release)[i].class.to_s
+ elsif identifiers(release)[i] || other.identifiers(release)[i]
+ return other.identifiers(release)[i].class.to_s <=> identifiers(release)[i].class.to_s
+ end
+ end
+ 0
+ end
+
+ # @param [Solve::Version] other
+ #
# @return [Boolean]
def eql?(other)
other.is_a?(Version) && self == other
end
def inspect
to_s
end
def to_s
- "#{major}.#{minor}.#{patch}"
+ str = "#{major}.#{minor}.#{patch}"
+ str += "-#{pre_release}" if pre_release
+ str += "+#{build}" if build
+ str
end
end
end