Class: Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/xregistry.rb

Instance Method Summary collapse

Constructor Details

#initialize(app_key = '', vendor_key = '') ⇒ Registry

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

Instance Method Details

#appKeyObject Also known as: app_key

Return application key



46
47
48
# File 'lib/xregistry.rb', line 46

def appKey
  @app_key
end

#deleteEntry(section, key) ⇒ Object Also known as: delete_entry

Delete the registry entry for the specified section and key.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

#deleteSection(section) ⇒ Object Also known as: delete_section

Delete a specified section from the registry.



78
79
80
81
82
83
84
85
86
87
88
89
90
# 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

#each_keyObject

Iterate over the section keys of the registry.



94
95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object

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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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) ⇒ Object Also known as: read_bool_entry

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.

Raises:

  • (TypeError)


137
138
139
140
141
# 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

#readIntEntry(section, key, default = 0) ⇒ Object Also known as: read_int_entry

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



146
147
148
# File 'lib/xregistry.rb', line 146

def readIntEntry(section, key, default = 0)
  readEntry(section, key, default).to_i
end

#readRealEntry(section, key, default = 0.0) ⇒ Object Also known as: read_real_entry

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



153
154
155
# File 'lib/xregistry.rb', line 153

def readRealEntry(section, key, default = 0.0)
  readEntry(section, key, default).to_f
end

#readStringEntry(section, key, default = '') ⇒ Object Also known as: read_string_entry

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



160
161
162
# File 'lib/xregistry.rb', line 160

def readStringEntry(section, key, default = '')
  readEntry(section, key, default).to_s
end

#vendorKeyObject Also known as: vendor_key

Return vendor key



52
53
54
# File 'lib/xregistry.rb', line 52

def vendorKey
  @vendor_key
end

#writeBoolEntry(section, key, value) ⇒ Object Also known as: write_bool_entry

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



166
167
168
# File 'lib/xregistry.rb', line 166

def writeBoolEntry(section, key, value)
  writeEntry(section, key, value ? 1 : 0)
end

#writeIntEntry(section, key, value) ⇒ Object Also known as: write_int_entry

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



172
173
174
# File 'lib/xregistry.rb', line 172

def writeIntEntry(section, key, value)
  writeEntry(section, key, value.to_i)
end

#writeRealEntry(section, key, value) ⇒ Object Also known as: write_real_entry

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



179
180
181
# File 'lib/xregistry.rb', line 179

def writeRealEntry(section, key, value)
  writeEntry(section, key, value.to_f)
end

#writeStringEntry(section, key, value) ⇒ Object Also known as: write_string_entry

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



185
186
187
# File 'lib/xregistry.rb', line 185

def writeStringEntry(section, key, value)
  writeEntry(section, key, value.to_s)
end