Xregistry

Check-in [5a7b383dcf]
Login

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

Overview
Comment:Add appKey and vendorKey methods.
Timelines: family | ancestors | descendants | both | trunk | version-1.5.0
Files: files | file ages | folders
SHA1:5a7b383dcfd8c0db2fcb1f45920b21b97d6fed5c
User & Date: wbp 2016-03-11 21:50:51
Context
2016-03-19
21:15
Raise error in readBoolEntry if value is a string and add additional tests of reading entries. check-in: 024e690d87 user: wbp tags: trunk, version-1.5.1
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to VERSION.

1
1.4.0
|
1
1.5.0

Changes to lib/xregistry.rb.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


25
26
27
28
29
30
31
..
36
37
38
39
40
41
42












43
44
45
46
47
48
49
#  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 = '')


    @on_windows = RUBY_PLATFORM =~ /w(in)?32/

    if @on_windows
      require 'win32/registry'
      @root_key = ['SOFTWARE', vendor_key, app_key].join('\\')
    else
      require 'sqlite3'
................................................................................
      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|







|







>
>







 







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







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
..
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
#  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:  11-Mar-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 = '')
    @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'
................................................................................
      regdir += '/' + vendor_key
      File.directory?(regdir) || Dir.mkdir(regdir)

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

  # Return application key
  def appKey
    @app_key
  end
  alias app_key appKey

  # Return vendor key
  def vendorKey
    @vendor_key
  end
  alias vendor_key vendorKey

  # 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|

Changes to test/test_xregistry.rb.

1
2
3
4
5
6
7



8
9
10
11
12
13
14
15
16
..
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
...
118
119
120
121
122
123
124




125






# 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')
  end

  def test_new
    assert_equal(Registry, @@reg.class)
  rescue
    flunk "error encountered opening database: #{$!}"
  end
................................................................................
    string = @@reg.readStringEntry('Settings', 'string_key1', 'default value')
    assert_match(/ value/, string)
  end

  # test of writeBoolEntry
  def test_write_bool_entry
    @@reg.writeBoolEntry('Settings', 'bool_key2', true)
    value = @@reg.readBoolEntry('Settings', 'bool_key2', nil)
    assert_equal(true, value)

    @@reg.writeBoolEntry('Settings', 'bool_key2', false)
    value = @@reg.readBoolEntry('Settings', 'bool_key2', nil)
    assert_equal(false, value)
  end

  # test of writeIntEntry
  def test_write_int_entry
    @@reg.writeIntEntry('Settings', 'int_key2', 456)
    value = @@reg.readIntEntry('Settings', 'int_key2', nil)
    assert_equal(456, value)
  end

  # test of writeRealEntry
  def test_write_real_entry
    @@reg.writeRealEntry('Settings', 'real_key2', 25.5)
    value = @@reg.readRealEntry('Settings', 'real_key2', nil)
    assert_equal(25.5, value)
  end

  # 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')
................................................................................
    assert_equal('no value', string)

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




end







|




|
>
>
>

|







 







|



|






|






|






|






|


|







 







>
>
>
>
|
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
..
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# test program for wregistry gem
# Last modified:  11-Mar-2016

require 'xregistry'
require 'test/unit'

class TestXregistry < Test::Unit::TestCase
  @@app_key    = 'testxreg'
  @@vendor_key = 'Wikareia'

  def setup
    @@reg ||= Registry.new(@@app_key, @@vendor_key)
  end

  def test_new
    assert_equal(Registry, @@reg.class)
  rescue
    flunk "error encountered opening database: #{$!}"
  end
................................................................................
    string = @@reg.readStringEntry('Settings', 'string_key1', 'default value')
    assert_match(/ value/, string)
  end

  # test of writeBoolEntry
  def test_write_bool_entry
    @@reg.writeBoolEntry('Settings', 'bool_key2', true)
    value = @@reg.readBoolEntry('Settings', 'bool_key2')
    assert_equal(true, value)

    @@reg.writeBoolEntry('Settings', 'bool_key2', false)
    value = @@reg.readBoolEntry('Settings', 'bool_key2')
    assert_equal(false, value)
  end

  # test of writeIntEntry
  def test_write_int_entry
    @@reg.writeIntEntry('Settings', 'int_key2', 456)
    value = @@reg.readIntEntry('Settings', 'int_key2')
    assert_equal(456, value)
  end

  # test of writeRealEntry
  def test_write_real_entry
    @@reg.writeRealEntry('Settings', 'real_key2', 25.5)
    value = @@reg.readRealEntry('Settings', 'real_key2')
    assert_equal(25.5, value)
  end

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

  # test of deleteEntry
  def test_delete_entry
    @@reg.writeStringEntry('Settings', 'key1', 'dummy value')
    string = @@reg.readStringEntry('Settings', 'key1')
    assert_equal('dummy value', string)
    @@reg.writeStringEntry('Settings', 'key2', 'dummy value')
    string = @@reg.readStringEntry('Settings', 'key2')
    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')
................................................................................
    assert_equal('no value', string)

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

  # test of appKey
  def test_app_key
    assert_equal(@@app_key, @@reg.appKey)
  end

  # test of vendorKey
  def test_vendor_key
    assert_equal(@@vendor_key, @@reg.vendorKey)
  end
end