class VMLib::Version

This is the primary version number class for the version manager library

This is the primary version number class for the version manager library

Constants

NAME_REGEX

Regular expression format to retrieve the name. Names may consist of any combination of any alphanumeric character, underscores and hyphens.

SPECIAL_REGEX

Regular expression format to understand the release and build formats.

Acceptable formats:

alpha.1.4
beta.1.4
6.2.8
1
build-256.2013-01-04T16-40Z

These fields consist of dot-separated identifiers, and these identifiers may contain only alphanumeric characters and hyphens.

Fields consisting of only digits will be interpreted as numeric and leading zeroes will be stripped.

TAG_FORMAT

Default tag format for version control systems

VER_REGEX

Regular expression to understand the version format.

Acceptable formats:

1.078.2
v1.30.4908
version 2.4.875

Leading zeroes will be stripped in any numeric field, therefore, the version 1.078.2 would be treated the same as 1.78.2

Attributes

major[RW]

The major version number

minor[RW]

The minor version number

name[RW]

The program or project name

patch[RW]

The patch version number

Public Class Methods

compare_build() click to toggle source

Specify whether to compare build metadata or not between two versions

# File lib/vmlib/class.rb, line 29
def self.compare_build
  @@compare_build
end
enable_prerelease_parser() click to toggle source

Specify whether to use the prerelease parser or not. Disabling this will cause an error to be thrown in the following cases:

# File lib/vmlib/class.rb, line 50
def self.enable_prerelease_parser
  @@enable_prerelease_parser
end
new(name = '', major = 0, minor = 0, patch = 0, prerelease = '0', build = '') click to toggle source

Create a new version instance and set it to the specified parameters

# File lib/vmlib/base.rb, line 34
def initialize(name = '', major = 0, minor = 0, patch = 0, prerelease = '0', build = '')
  reset
  set_name name
  set_major major
  set_minor minor
  set_patch patch
  set_prerelease prerelease
  set_build build
end

Public Instance Methods

<=>(other) click to toggle source

Compare two version structures

# File lib/vmlib/compare.rb, line 68
def <=> (other)
  # Check major version
  cmp = (@major <=> other.major)
  return cmp unless cmp == 0

  # Check minor version
  cmp = (@minor <=> other.minor)
  return cmp unless cmp == 0

  # Check patch version
  cmp = (@patch <=> other.patch)
  return cmp unless cmp == 0

  # Check prerelease arrays
  myown_pre = self.prerelease.split('.')
  convert_to_integer(myown_pre)
  other_pre = other.prerelease.split('.')
  convert_to_integer(other_pre)
  cmp = compare_arrays(myown_pre, other_pre)
  
  # Make sure that the prerelease is compared correctly
  cmp = 1 if cmp == -1 and myown_pre.length == 0
  cmp = -1 if cmp == 1 and other_pre.length == 0
  return cmp unless cmp == 0

  # Check build arrays, but only if specified
  # As with the parser, leave identifiers as strings so they can
  # be compared lexically
  if (@@compare_build)
    myown_bld = self.build.split('.')
    other_bld = other.build.split('.')
    cmp = compare_arrays(myown_bld, other_bld)
    return cmp unless cmp == 0
  end

  return 0
end
build() click to toggle source

Get the build information

# File lib/vmlib/base.rb, line 191
def build
  format '%b'
end
build=(build) click to toggle source

Set the build information

# File lib/vmlib/base.rb, line 196
def build=(build)
  set_build build
end
bump_major() click to toggle source

Bump the major release by 1 and reset the minor and patch releases to 0.

# File lib/vmlib/bump.rb, line 16
def bump_major
  @major = @major + 1
  @minor = 0
  @patch = 0

  true
end
bump_minor() click to toggle source

Bump the minor release by 1 and reset the patch release to 0

# File lib/vmlib/bump.rb, line 25
def bump_minor
  @minor = @minor + 1
  @patch = 0

  true
end
bump_patch() click to toggle source

Bump the patch release by 1

# File lib/vmlib/bump.rb, line 33
def bump_patch
  @patch = @patch + 1

  true
end
bump_prerelease() click to toggle source

Bump the prerelease version by 1

# File lib/vmlib/bump.rb, line 40
def bump_prerelease
  case @reltype
  when :rel_type_dev
    @devnum += 1
  when :rel_type_alpha
    @alphanum += 1
  when :rel_type_beta
    @betanum += 1
  when :rel_type_rc
    @rcnum += 1
  when :rel_type_final
    raise Errors::BumpError, "cannot bump prerelease for a final version"
  when :rel_type_custom
    lastfield = @relcustom.pop
    if lastfield.kind_of? Integer
      @relcustom.push lastfield + 1
    else
      @relcustom.push lastfield
      raise Errors::BumpError, "cannot bump a non-numeric prerelease field"
    end
  end
end
bump_release_type() click to toggle source

Bump the prerelease type

# File lib/vmlib/bump.rb, line 64
def bump_release_type
  case @reltype
  when :rel_type_dev # development -> alpha
    @reltype = :rel_type_alpha
    @alphanum = 1

  when :rel_type_alpha # alpha -> beta
    @reltype = :rel_type_beta
    @betanum = 1

  when :rel_type_beta # beta -> rc
    @reltype = :rel_type_rc
    @rcnum = 1

  when :rel_type_rc # rc -> final
    @reltype = :rel_type_final

  when :rel_type_final # ERROR!
    raise Errors::BumpError, "cannot bump from final version"

  when :rel_type_custom # ERROR!
    raise Errors::BumpError, "cannot bump from custom prerelease"

  end
end
format(fstr) click to toggle source

Display the version in the specified format Input: Format string

%n - name
%M - major version number
%m - minor version number
%p - patch number
%r - prerelease
%b - build number
# File lib/vmlib/format.rb, line 25
def format(fstr)
  if @name.empty?
    fstr = fstr.gsub('%n', '')
  else
    fstr = fstr.gsub('%n', @name.to_s + ' ')
  end

  # Match the major version
  match = /%(\d*)M/.match(fstr)
  fstr = fstr.gsub(/%(\d*)M/, "%0#{$1}d" % @major)

  # Match the minor version
  match = /%(\d*)m/.match(fstr)
  fstr = fstr.gsub(/%(\d*)m/, "%0#{$1}d" % @minor)

  # Match the patch version
  match = /%(\d*)p/.match(fstr)
  fstr = fstr.gsub(/%(\d*)p/, "%0#{$1}d" % @patch)

  if (@reltype == :rel_type_final) or
     (@reltype == :rel_type_custom and @relcustom.length == 0)
    fstr = fstr.gsub('%r', '')
  else
    fstr = case @reltype
      when :rel_type_dev
        fstr.gsub('%r', "-#{@devnum}")
      when :rel_type_alpha
        fstr.gsub('%r', "-a.#{@alphanum}")
      when :rel_type_beta
        fstr.gsub('%r', "-b.#{@betanum}")
      when :rel_type_rc
        fstr.gsub('%r', "-rc.#{@rcnum}")
      when :rel_type_custom
        fstr.gsub('%r', '-' + @relcustom.join('.'))
      else
        fstr.gsub('%r', '')
      end
  end

  if (@buildtype == :bld_type_final) or
     (@buildtype == :bld_type_custom and @buildcustom.length == 0)
    fstr = fstr.gsub('%b', '')
  else
    fstr = case @buildtype
      when :bld_type_custom
        fstr.gsub('%b', '+' + @buildcustom.join('.'))
      else
        fstr.gsub('%b', '')
      end
  end

  fstr
end
inspect() click to toggle source

Inspect the version object

# File lib/vmlib/base.rb, line 45
def inspect
  str = "#<#{self.class}:"
  str += "0x%016x" % (self.object_id * 2)
  str += " @name='#{@name}'"
  str += " @major=#{@major}"
  str += " @minor=#{@minor}"
  str += " @patch=#{@patch}"

  case @reltype
  when :rel_type_dev
    str += " @devnum=#{@devnum}"
  when :rel_type_alpha
    str += " @alphanum=#{@alphanum}"
  when :rel_type_beta
    str += " @betanum=#{@betanum}"
  when :rel_type_rc
    str += " @rcnum=#{@rcnum}"
  when :rel_type_custom
    str += " @prerelease=#{@relcustom.inspect}"
  end

  case @buildtype
  when :bld_type_custom
    str += " @build=#{@buildcustom.inspect}"
  end

  str += ">"

  str
end
parse(ver) click to toggle source

Parse a string containing the project name and version number into its individual components

# File lib/vmlib/parse.rb, line 158
def parse(ver)
  unless ver.kind_of? String
    raise Errors::ParameterError, "expected a string to be parsed"
  end

  # Chop off any trailing newlines
  ver.chomp!

  # Match the name
  match = NAME_REGEX.match(ver)
  if match
    @name = match[:name]
    ver = ver.sub(NAME_REGEX, '')
  else
    # Sometimes we may not get a name to be parsed. If that's the case
    # then ensure we clear the name field.
    @name = ''
  end

  # Match the major, minor and patch versions
  match = VER_REGEX.match(ver)
  if match
    @major = match[:major].to_i
    @minor = match[:minor].to_i
    @patch = match[:patch].to_i
    ver = ver.sub(VER_REGEX, '')
  else
    raise Errors::ParseError, "unrecognized version format '#{ver}'"
  end

  # See if we have a prerelease version (begins with a -)
  if ver =~ /^-/
    ver = ver.sub(/^-/, '')

    match = parse_release(ver)
    if match
      # Delete the matched data
      ver = ver.sub(SPECIAL_REGEX, '')
    end
  else # if ver !~ /^-/
    @reltype = :rel_type_final
  end

  # See if we have a build version (begins with a +)
  if ver =~ /^\+/
    ver = ver.sub(/^\+/, '')

    match = parse_build(ver)
    if match
      # Delete the matched data
      ver = ver.sub(SPECIAL_REGEX, '')
    end
  else # if ver !~ /^\+/
    @buildtype = :bld_type_final
  end

  # By now, ver should be empty. Raise an error if this is not the case
  unless ver.empty?
    raise Errors::ParseError, "unrecognized version format '#{ver}'"
  end

  true
end
prerelease() click to toggle source

Get the prerelease information

# File lib/vmlib/base.rb, line 165
def prerelease #:attr:
  format '%r'
end
prerelease=(prerelease) click to toggle source

Set the prerelease information

# File lib/vmlib/base.rb, line 170
def prerelease=(prerelease)
  set_prerelease prerelease
end
reset() click to toggle source

Reset the version number to 0.0.0-0

# File lib/vmlib/base.rb, line 16
def reset
  @name = ''
  @major = 0
  @minor = 0
  @patch = 0
  @reltype = :rel_type_dev
  @devnum = 0
  @alphanum = 0
  @betanum = 0
  @rcnum = 0
  @relcustom = []
  @buildtype = :bld_type_final
  @buildcustom = []

  true
end
tag() click to toggle source

Display a version tag suitable for use in tagging releases in the user's version control system

# File lib/vmlib/format.rb, line 81
def tag
  format TAG_FORMAT
end
to_s() click to toggle source

Display the version information as a string

# File lib/vmlib/format.rb, line 86
def to_s
  format "%n%M.%m.%p%r%b"
end