lib/solve/version.rb in solve-0.2.1 vs lib/solve/version.rb in solve-0.3.0
- old
+ new
@@ -1,26 +1,76 @@
module Solve
+ # @author Jamie Winsor <jamie@vialstudios.com>
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
+ when /^(\d+)\.(\d+)\.(\d+)$/
+ [ $1.to_i, $2.to_i, $3.to_i ]
+ when /^(\d+)\.(\d+)$/
+ [ $1.to_i, $2.to_i, nil ]
+ else
+ raise Errors::InvalidVersionFormat.new(version_string)
+ end
+ end
+ end
+
include Comparable
attr_reader :major
attr_reader :minor
attr_reader :patch
- # @param [#to_s] ver_str
- def initialize(ver_str = String.new)
- @major, @minor, @patch = parse(ver_str)
+ # @overload initialize(version_array)
+ # @param [Array] version_array
+ #
+ # @example
+ # Version.new([1, 2, 3]) => #<Version: @major=1, @minor=2, @patch=3>
+ #
+ # @overload initialize(version_string)
+ # @param [#to_s] version_string
+ #
+ # @example
+ # Version.new("1.2.3") => #<Version: @major=1, @minor=2, @patch=3>
+ #
+ # @overload initialize(version)
+ # @param [Solve::Version] version
+ #
+ # @example
+ # Version.new(Version.new("1.2.3")) => #<Version: @major=1, @minor=2, @patch=3>
+ #
+ def initialize(*args)
+ if args.first.is_a?(Array)
+ @major, @minor, @patch = args.first
+ else
+ @major, @minor, @patch = self.class.split(args.first.to_s)
+ end
+
+ @major ||= 0
+ @minor ||= 0
+ @patch ||= 0
end
+ # @param [Solve::Version] other
+ #
+ # @return [Integer]
def <=>(other)
[:major, :minor, :patch].each do |method|
ans = (self.send(method) <=> other.send(method))
return ans if ans != 0
end
0
end
+ # @param [Solve::Version] other
+ #
+ # @return [Boolean]
def eql?(other)
other.is_a?(Version) && self == other
end
def inspect
@@ -28,20 +78,7 @@
end
def to_s
"#{major}.#{minor}.#{patch}"
end
-
- private
-
- def parse(ver_str = String.new)
- @major, @minor, @patch = case ver_str.to_s
- when /^(\d+)\.(\d+)\.(\d+)$/
- [ $1.to_i, $2.to_i, $3.to_i ]
- when /^(\d+)\.(\d+)$/
- [ $1.to_i, $2.to_i, 0 ]
- else
- raise InvalidVersionFormat.new(ver_str)
- end
- end
end
end