2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
10 # The Version class processes string versions into comparable values
21 # Returns true if +version+ is a valid version string.
23 def self.correct?(version)
25 when Integer, /\A\s*(\d+(\.\d+)*)*\s*\z/ then true
31 # Factory method to create a Version object. Input may be a Version or a
32 # String. Intended to simplify client code.
34 # ver1 = Version.create('1.3.17') # -> (Version object)
35 # ver2 = Version.create(ver1) # -> (ver1)
36 # ver3 = Version.create(nil) # -> nil
38 def self.create(input)
39 if input.respond_to? :version then
49 # Constructs a Version from the +version+ string. A version string is a
50 # series of digits separated by dots.
52 def initialize(version)
53 raise ArgumentError, "Malformed version number string #{version}" unless
54 self.class.correct?(version)
56 self.version = version
60 "#<#{self.class} #{@version.inspect}>"
63 # Dump only the raw version string, not the complete object
68 # Load custom marshal format
69 def marshal_load(array)
70 self.version = array[0]
74 # Strip ignored trailing zeros.
77 @ints = build_array_from_version_string
79 return if @ints.length == 1
81 @ints.pop while @ints.last == 0
83 @ints = [0] if @ints.empty?
87 # Returns the text representation of the version
89 # return:: [String] version as string
96 # Returns an integer array representation of this Version.
99 normalize unless @ints
103 def to_yaml_properties
107 def version=(version)
108 @version = version.to_s.strip
112 def yaml_initialize(tag, values)
113 self.version = values['version']
117 # Compares this version with +other+ returning -1, 0, or 1 if the other
118 # version is larger, the same, or smaller than this one.
121 return nil unless self.class === other
122 return 1 unless other
127 # A Version is only eql? to another version if it has the same version
128 # string. "1.0" is not the same version as "1".
131 self.class === other and @version == other.version
138 # Return a new version object where the next to the last revision
139 # number is one greater. (e.g. 5.3.1 => 5.4)
141 ints = build_array_from_version_string
142 ints.pop if ints.size > 1
144 self.class.new(ints.join("."))
147 def build_array_from_version_string
148 @version.to_s.scan(/\d+/).map { |s| s.to_i }
150 private :build_array_from_version_string
154 require 'rubygems/requirement'
156 # Gem::Requirement's original definition is nested in Version.
157 # Although an inappropriate place, current gems specs reference the nested
158 # class name explicitly. To remain compatible with old software loading
159 # gemspecs, we leave a copy of original definition in Version, but define an
160 # alias Gem::Requirement for use everywhere else.
162 Requirement = ::Gem::Requirement