=begin rdoc = Version A class for manipulating and storing conventional version numbers in the form of "1.0.0". == Usage VERSION = Version("1.2.3") # same as Version.new() VERSION.major #=> 1 VERSION.minor #=> 2 VERSION.teeny #=> 3 VERSION.teeny += 1 #=> 4 VERSION > Version(1,2,3) #=> true VERSION > Version("1.2.4") #=> false == Author(s) * Minero Aoki * Thomas Sawyer == License Based on original by Minero Aoki. GPL Copyright (c) 2000-2001 Minero Aoki GPL Copyright (c) 2004-2005 Thomas Sawyer This program is free software. You can distribute/modify this program under the terms of the GNU Lesser General Public License version 2 or later. =end class Version VERSION_EXP = /\d+(?:\.\d+)*/ def self.[](*args) Version.new(*args) end include Comparable def initialize( arg, *nums ) set( arg, *nums ) end def set( arg, *nums ) if String === arg then unless m = VERSION_EXP.match( arg ) then raise ArgumentError, "wrong version string format '#{arg}'" end @num = m[0].split('.').collect {|i| i.to_i } else @num = [ arg, *(nums || []) ] #@num.unshift arg end end def dup self.class.new( *@nums ) end def ==( other ) @num == other.to_a end alias_method( :eql?, :== ) def hash ; @num.hash ; end def to_s ; @num.join '.' ; end def to_a ; @num.dup ; end def inspect "\#<#{self.class} #{to_s}>" end def major ; @num[0] ; end def major=( i ) ; @num[0] = i ; end def minor ; @num[1] ; end def minor=( i ) ; @num[1] = i ; end def teeny ; @num[2] ; end def teeny=( i ) ; @num[2] = i ; end def weeny ; @num[3] ; end def weeny=( i ) ; @num[3] = i ; end def next_major! (1..last_index).each{ |i| @num[i] = 0 } if last_index > 0 @num[0] += 1 end def next_minor! (2..last_index).each{ |i| @num[i] = 0 } if last_index > 1 if @num[1] @num[1] += 1 end end def next_teeny! (3..last_index).each{ |i| @num[i] = 0 } if last_index > 2 if @num[2] @num[2] += 1 end end def next_weeny! #(4..last_index).each{ |i| @num[i] = 0 } if last_index > 3 if @num[3] @num[3] += 1 end end def type case last_index when 0 then :major when 1 then :minor when 2 then :teeny when 3 then :weeny end end def <=>( other ) return false unless self.class === other n = @num ret = nil; other.instance_eval { ret = (n <=> @num) } ret end private def last_index @num.length - 1 end end # not really the ruby way #def Version(*args) ; Version.new(*args) ; end