class Registry

xregistry.rb

This file defines a cross-platform Registry class for storing application configuration parameters.

The application programming interface (API) is based on the FOX registry class, which is part of the FOX GUI toolkit (www.fox-toolkit.org/). This file defines a class that can be used in the same way as the FOX registry, but without needing the FOX toolkit.

The original FOX registry uses the Windows registry if running under Windows, and an .INI type file if running under Unix-type systems. This class also uses the Windows registry if running under Windows, but uses an SQLite3 database if running under a Unix-type system.

Last revised: 03-Aug-2016 Wm. Parsons

Public Class Methods

new(app_key = '', vendor_key = '') click to toggle source

Construct registry object; app_key and vendor_key must be string constants. Regular applications SHOULD set a vendor key!

# File lib/xregistry.rb, line 24
def initialize(app_key = '', vendor_key = '')
  @app_key    = app_key
  @vendor_key = vendor_key
  @on_windows = RUBY_PLATFORM =~ /w(in)?32/

  if @on_windows
    require 'win32/registry'
    @root_key = ['SOFTWARE', vendor_key, app_key].join('\\')
  else
    require 'sqlite3'

    # create registry directory if necessary
    regdir = ENV['HOME'] + '/.registry'
    File.directory?(regdir) || Dir.mkdir(regdir)
    regdir += '/' + vendor_key
    File.directory?(regdir) || Dir.mkdir(regdir)

    @db = SQLite3::Database.new("#{regdir}/#{app_key}.db3")
  end
end

Public Instance Methods

appKey() click to toggle source

Return application key

# File lib/xregistry.rb, line 46
def appKey
  @app_key
end
Also aliased as: app_key
app_key()
Alias for: appKey
deleteEntry(section, key) click to toggle source

Delete the registry entry for the specified section and key.

# File lib/xregistry.rb, line 58
def deleteEntry(section, key)
  if @on_windows
    begin
      pkey = [@root_key, section].join('\\')
      access = Win32::Registry::KEY_ALL_ACCESS
      Win32::Registry::HKEY_CURRENT_USER.open(pkey, access) do |reg|
        reg.delete_value(key)
      end
    rescue Win32::Registry::Error
    end
  else
    begin
      @db.execute %Q[delete from "#{section}" where key=?], key
    rescue SQLite3::SQLException
    end
  end
end
Also aliased as: delete_entry
deleteSection(section) click to toggle source

Delete a specified section from the registry.

# File lib/xregistry.rb, line 78
def deleteSection(section)
  if @on_windows
    begin
      access = Win32::Registry::KEY_ALL_ACCESS
      Win32::Registry::HKEY_CURRENT_USER.open(@root_key, access) do |reg|
        reg.delete_key(section, true)
      end
    rescue Win32::Registry::Error
    end
  else
    @db.execute "drop table if exists \"#{section}\""
  end
end
Also aliased as: delete_section
delete_entry(section, key)
Alias for: deleteEntry
delete_section(section)
Alias for: deleteSection
each_key() { |subkey| ... } click to toggle source

Iterate over the section keys of the registry.

# File lib/xregistry.rb, line 94
  def each_key
    if @on_windows
      Win32::Registry::HKEY_CURRENT_USER.open(@root_key) do |reg|
        reg.each_key { |subkey, wtime| yield subkey }
      end
    else
      @db.execute <<-EOS do |name|
          select name from sqlite_master where type='table' order by name
          EOS
        yield name[0]
      end
    end
  end
find(section) click to toggle source

Find a section in the registry. The properties of the value returned are undefined except that it must support an “each_key” to iterate thru the keys and a “find” method to locate the value associated with a key.

# File lib/xregistry.rb, line 112
def find(section)
  hash = {}
  if @on_windows
    pkey = [@root_key, section].join('\\')
    Win32::Registry::HKEY_CURRENT_USER.open(pkey) do |reg|
      reg.each do |subkey, type, value|
        hash[subkey] = value
      end
    end
  else
    @db.execute %Q[select key, value from "#{section}" order by key] do |row|
      hash[row[0]] = row[1]
    end
  end
  class << hash
    def find(key)
      self[key]
    end
  end
  hash
end
readBoolEntry(section, key, default = false) click to toggle source

Read a boolean registry entry from the specified section and key. If no value is found, the default value is returned. A type error is raised if the retrieved value is real or a string.

# File lib/xregistry.rb, line 137
def readBoolEntry(section, key, default = false)
  value = readEntry(section, key, default)
  raise TypeError if value.class == Float || value.class == String
  value && value != 0
end
Also aliased as: read_bool_entry
readIntEntry(section, key, default = 0) click to toggle source

Read an integer registry entry from the specified section and key. If no value is found, the default value is returned.

# File lib/xregistry.rb, line 146
def readIntEntry(section, key, default = 0)
  readEntry(section, key, default).to_i
end
Also aliased as: read_int_entry
readRealEntry(section, key, default = 0.0) click to toggle source

Read a double-precision floating point registry entry from the specified section and key. If no value is found, the default value is returned.

# File lib/xregistry.rb, line 153
def readRealEntry(section, key, default = 0.0)
  readEntry(section, key, default).to_f
end
Also aliased as: read_real_entry
readStringEntry(section, key, default = '') click to toggle source

Read a string registry entry from the specified section and key. If no value is found, the default value is returned.

# File lib/xregistry.rb, line 160
def readStringEntry(section, key, default = '')
  readEntry(section, key, default).to_s
end
Also aliased as: read_string_entry
read_bool_entry(section, key, default = false)
Alias for: readBoolEntry
read_int_entry(section, key, default = 0)
Alias for: readIntEntry
read_real_entry(section, key, default = 0.0)
Alias for: readRealEntry
read_string_entry(section, key, default = '')
Alias for: readStringEntry
vendorKey() click to toggle source

Return vendor key

# File lib/xregistry.rb, line 52
def vendorKey
  @vendor_key
end
Also aliased as: vendor_key
vendor_key()
Alias for: vendorKey
writeBoolEntry(section, key, value) click to toggle source

Write a boolean registry value to the specified section and key.

# File lib/xregistry.rb, line 166
def writeBoolEntry(section, key, value)
  writeEntry(section, key, value ? 1 : 0)
end
Also aliased as: write_bool_entry
writeIntEntry(section, key, value) click to toggle source

Write an integer registry value to the specified section and key.

# File lib/xregistry.rb, line 172
def writeIntEntry(section, key, value)
  writeEntry(section, key, value.to_i)
end
Also aliased as: write_int_entry
writeRealEntry(section, key, value) click to toggle source

Write a double-precision floating point registry value to the specified section and key.

# File lib/xregistry.rb, line 179
def writeRealEntry(section, key, value)
  writeEntry(section, key, value.to_f)
end
Also aliased as: write_real_entry
writeStringEntry(section, key, value) click to toggle source

Write a string registry value to the specified section and key.

# File lib/xregistry.rb, line 185
def writeStringEntry(section, key, value)
  writeEntry(section, key, value.to_s)
end
Also aliased as: write_string_entry
write_bool_entry(section, key, value)
Alias for: writeBoolEntry
write_int_entry(section, key, value)
Alias for: writeIntEntry
write_real_entry(section, key, value)
Alias for: writeRealEntry
write_string_entry(section, key, value)
Alias for: writeStringEntry

Private Instance Methods

readEntry(section, key, default) click to toggle source

Read a registry entry from the specified section and key. If no value is found, the default value is returned.

# File lib/xregistry.rb, line 196
def readEntry(section, key, default)
  if @on_windows
    pkey = [@root_key, section].join('\\')
    reg  = Win32::Registry::HKEY_CURRENT_USER.open(pkey)
    reg.read(key)[1]
  else
    @db.get_first_value("select value from \"#{section}\" where key=?",
                        key) || default
  end
rescue
  default
end
writeEntry(section, key, value) click to toggle source

Write a registry value to the specified section and key.

# File lib/xregistry.rb, line 210
def writeEntry(section, key, value)
  if @on_windows
    # store real values as strings under Windows
    value = value.to_s if value.class == Float
    begin
      pkey = [@root_key, section].join('\\')
      access = Win32::Registry::KEY_WRITE
      Win32::Registry::HKEY_CURRENT_USER.open(pkey, access) do |reg|
        reg[key] = value
      end
    rescue
      Win32::Registry::HKEY_CURRENT_USER.create(pkey, access) do |reg|
        reg[key] = value
      end
    end
  else
    begin
      @db.execute("update \"#{section}\" set value=? where key=?",
                  [value, key])
      if @db.changes == 0
        # no row was updated so insert a new row
        @db.execute("insert into \"#{section}\"(key, value) values(?,?)",
                    [key, value])
      end
    rescue SQLite3::Exception => e
      if e.to_s =~ /no such table/
        @db.execute "create table \"#{section}\"(key text unique, value)"

        @db.execute("insert into \"#{section}\"(key, value) values(?,?)",
                    [key, value])
      else
        raise e.to_s
      end
    end
  end
end