module VMLib

VMLib version information

Copyright (C) 2013 Nirenjan Krishnan All rights reserved.

Public Instance Methods

create(name = '', dir = nil) click to toggle source

Create a new version file for the current (or specified) folder. If it already exists, then throw an error

# File lib/vmlib/file.rb, line 81
def create(name = '', dir = nil)
  # Make sure that the directory isn't versioned already
  begin
    path = find_file(dir)
  rescue Errors::NoVersionError
    # We are good to go
  else
    # Raise error that it's already versioned
    raise Errors::VersionFileError, "version already exists at #{path}"
  end

  dir ||= ::Dir.pwd
  raise Errors::PathError unless ::File.directory?(dir)
  path = ::File.join(dir, @filename)

  ::File.open(path, "w") do |file|
    version = Version.new(name)
    file.write version.to_s + "\n"
  end

end
read(dir = nil) click to toggle source

Read the version file and return the parsed version data

# File lib/vmlib/file.rb, line 49
def read(dir = nil)
  path = find_file(dir)

  if ::File.readable?(path)
    # Read
    verdata = Version.new
    verdata.parse ::File.read(path)
  else
    raise Errors::VersionFileError, "unable to read #{path}"
  end

  return verdata
end
write(version, dir = nil) click to toggle source

Write the specfied version into the version file

# File lib/vmlib/file.rb, line 64
def write(version, dir = nil)
  path = find_file(dir)

  unless version.is_a? Version
    raise Errors::ParameterError, "invalid version #{version}"
  end

  if ::File.writable?(path)
    # Write
    ::File.write(path, version.to_s + "\n")
  else
    raise Errors::VersionFileError, "unable to write #{path}"
  end
end