VersionNumber is a simplified form of a Tuple class desgined specifically for dealing with version numbers.
Parses a string constraint returning the operation as a lambda.
# File lib/more/facets/version.rb, line 113 def self.constraint_lambda( constraint ) op, val = *parse_constraint( constraint ) lambda { |t| t.send(op, val) } end
# File lib/more/facets/version.rb, line 40 def initialize( *args ) args = args.join('.').split(/\W+/) @self = args.collect { |i| i.to_i } end
# File lib/more/facets/version.rb, line 118 def self.parse_constraint( constraint ) constraint = constraint.strip re = %r{^(=~|~>|<=|>=|==|=|<|>)?\s*(\d+(:?[-.]\d+)*)$} if md = re.match( constraint ) if op = md[1] op = '=~' if op == '~>' op = '==' if op == '=' val = new( *md[2].split(/\W+/) ) else op = '==' val = new( *constraint.split(/\W+/) ) end else raise ArgumentError, "invalid constraint" end return op, val end
“Spaceship” comparsion operator.
# File lib/more/facets/version.rb, line 61 def <=>( other ) #other = other.to_t [@self.size, other.size].max.times do |i| c = @self[i] <=> other[i] return c if c != 0 end 0 end
For pessimistic constraint (like '~>' in gems)
# File lib/more/facets/version.rb, line 72 def =~( other ) #other = other.to_t upver = other.dup upver[0] += 1 @self >= other and @self < upver end
# File lib/more/facets/version.rb, line 55 def [](i) @self.fetch(i,0) end
# File lib/more/facets/version.rb, line 92 def bump(which=:teeny) case which when :major self.class.new(major+1) when :minor self.class.new(major, minor+1) when :teeny self.class.new(major, minor, teeny+1) else # ??? end end
# File lib/more/facets/version.rb, line 51 def inspect @self.to_s end
Major is the first number in the version series.
# File lib/more/facets/version.rb, line 81 def major ; @self[0] ; end
Delegate to the array.
# File lib/more/facets/version.rb, line 107 def method_missing( sym, *args, &blk ) @self.send(sym, *args, &blk ) rescue super end
Minor is the second number in the version series.
# File lib/more/facets/version.rb, line 85 def minor ; @self[1] || 0 ; end
Teeny is third number in the version series.
# File lib/more/facets/version.rb, line 89 def teeny ; @self[2] || 0 ; end
# File lib/more/facets/version.rb, line 45 def to_s ; @self.join('.') ; end
This is here only becuase File.join calls it instead of to_s.
# File lib/more/facets/version.rb, line 49 def to_str ; @self.join('.') ; end