class VersionNumber

VersionNumber

VersionNumber is a simplified form of a Tuple class desgined specifically for dealing with version numbers.

Public Class Methods

constraint_lambda( constraint ) click to toggle source

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
new( *args ) click to toggle source
# File lib/more/facets/version.rb, line 40
def initialize( *args )
  args = args.join('.').split(/\W+/)
  @self = args.collect { |i| i.to_i }
end
parse_constraint( constraint ) click to toggle source
# 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

Public Instance Methods

<=>( other ) click to toggle source

“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
=~( other ) click to toggle source

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
[](i) click to toggle source
# File lib/more/facets/version.rb, line 55
def [](i)
  @self.fetch(i,0)
end
bump(which=:teeny) click to toggle source
# 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
inspect() click to toggle source
# File lib/more/facets/version.rb, line 51
def inspect
  @self.to_s
end
major() click to toggle source

Major is the first number in the version series.

# File lib/more/facets/version.rb, line 81
def major ; @self[0] ; end
method_missing( sym, *args, &blk ) click to toggle source

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() click to toggle source

Minor is the second number in the version series.

# File lib/more/facets/version.rb, line 85
def minor ; @self[1] || 0 ; end
teeny() click to toggle source

Teeny is third number in the version series.

# File lib/more/facets/version.rb, line 89
def teeny ; @self[2] || 0 ; end
to_s() click to toggle source
# File lib/more/facets/version.rb, line 45
def to_s ; @self.join('.') ; end
to_str() click to toggle source

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