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
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
Return application key
# File lib/xregistry.rb, line 46 def appKey @app_key end
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
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
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 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
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
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
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
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
Return vendor key
# File lib/xregistry.rb, line 52 def vendorKey @vendor_key end
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
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
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
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
Private Instance Methods
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
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