Xregistry

Check-in [cae5eea66c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add deleteEntry method, and store real values under Windows as strings.
Timelines: family | ancestors | descendants | both | trunk | version-1.4.0
Files: files | file ages | folders
SHA1:cae5eea66c028ec4c01361e7eb7a3ce3cfe80fa9
User & Date: wbp 2016-02-22 20:11:02
Context
2016-03-11
21:50
Add appKey and vendorKey methods. check-in: 5a7b383dcf user: wbp tags: trunk, version-1.5.0
2016-02-22
20:11
Add deleteEntry method, and store real values under Windows as strings. check-in: cae5eea66c user: wbp tags: trunk, version-1.4.0
16:53
Make corrections to gemspec and add additional tests. check-in: 083d4a0378 user: wbp tags: trunk, version-1.3.9
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to VERSION.

1
1.3.9
|
1
1.4.0

Changes to lib/xregistry.rb.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
35
36
37
38
39
40
41




















42
43
44
45
46
47
48
49
50
51
52
53
54
...
128
129
130
131
132
133
134


135
136
137
138
139
140
141
#  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:  13-Feb-2016  Wm. Parsons
################################################################################


class Registry
  # Construct registry object; app_key and vendor_key must be string constants.
  # Regular applications SHOULD set a vendor key!
  def initialize(app_key = '', vendor_key = '')
................................................................................
      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





















  # Delete a specified section from the registry.
  def deleteSection(section)
    if @on_windows
      begin
        pkey = [@root_key, section].join('\\')
        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
................................................................................
  rescue
    default
  end

  # Write a registry value to the specified section and key.
  def writeEntry(section, key, value)
    if @on_windows


      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







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





<







 







>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

67
68
69
70
71
72
73
...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#  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:  22-Feb-2016  Wm. Parsons
################################################################################


class Registry
  # Construct registry object; app_key and vendor_key must be string constants.
  # Regular applications SHOULD set a vendor key!
  def initialize(app_key = '', vendor_key = '')
................................................................................
      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

  # Delete the registry entry for the specified section and key.
  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
  alias delete_entry deleteEntry

  # Delete a specified section from the registry.
  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
................................................................................
  rescue
    default
  end

  # Write a registry value to the specified section and key.
  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

Changes to test/test_xregistry.rb.

1
2
3
4
5
6
7
8
9
..
81
82
83
84
85
86
87
























88
89
90
91
92
93
94
# test program for wregistry gem
# Last modified:  21-Feb-2016

require 'xregistry'
require 'test/unit'

class TestWregistry < Test::Unit::TestCase
  def setup
    @@reg ||= Registry.new('testxreg', 'Wikareia')
................................................................................

  # test of writeStringEntry
  def test_write_string_entry
    @@reg.writeStringEntry('Settings', 'string_key2', 'test value')
    string = @@reg.readStringEntry('Settings', 'string_key2', nil)
    assert_equal('test value', string)
  end

























  # test of deleteSection
  def test_delete_section
    @@reg.writeStringEntry('Settings', 'key', 'dummy value')
    @@reg.deleteSection('Settings')
    string = @@reg.readStringEntry('Settings', 'key', 'no value')
    assert_equal('no value', string)

|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1
2
3
4
5
6
7
8
9
..
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# test program for wregistry gem
# Last modified:  22-Feb-2016

require 'xregistry'
require 'test/unit'

class TestWregistry < Test::Unit::TestCase
  def setup
    @@reg ||= Registry.new('testxreg', 'Wikareia')
................................................................................

  # test of writeStringEntry
  def test_write_string_entry
    @@reg.writeStringEntry('Settings', 'string_key2', 'test value')
    string = @@reg.readStringEntry('Settings', 'string_key2', nil)
    assert_equal('test value', string)
  end

  # test of deleteEntry
  def test_delete_entry
    @@reg.writeStringEntry('Settings', 'key1', 'dummy value')
    string = @@reg.readStringEntry('Settings', 'key1', nil)
    assert_equal('dummy value', string)
    @@reg.writeStringEntry('Settings', 'key2', 'dummy value')
    string = @@reg.readStringEntry('Settings', 'key2', nil)
    assert_equal('dummy value', string)
    @@reg.deleteEntry('Settings', 'key1')
    string = @@reg.readStringEntry('Settings', 'key1', 'no value')
    assert_equal('no value', string)

    # test of deleteEntry when key doesn't exist
    @@reg.deleteEntry('Settings', 'key1')
    string = @@reg.readStringEntry('Settings', 'key1', 'no value')
    assert_equal('no value', string)

    # test of deleteEntry when section doesn't exist
    @@reg.deleteSection('Settings')
    @@reg.deleteEntry('Settings', 'key2')
    string = @@reg.readStringEntry('Settings', 'key2', 'no value')
    assert_equal('no value', string)
  end

  # test of deleteSection
  def test_delete_section
    @@reg.writeStringEntry('Settings', 'key', 'dummy value')
    @@reg.deleteSection('Settings')
    string = @@reg.readStringEntry('Settings', 'key', 'no value')
    assert_equal('no value', string)