Xregistry

Check-in [633de1ab62]
Login

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

Overview
Comment:Add each_key and find methods.
Timelines: family | ancestors | descendants | both | trunk | version-1.6.0
Files: files | file ages | folders
SHA1:633de1ab62e8f4d9997264240f75fc2e4a815bb1
User & Date: wbp 2016-08-03 21:16:11
Context
2017-04-28
23:21
Delete dist target from Rakefile. Leaf check-in: 3dc3aaf940 user: wbp tags: trunk
2016-08-03
21:16
Add each_key and find methods. check-in: 633de1ab62 user: wbp tags: trunk, version-1.6.0
2016-08-01
21:43
Make separate "userinstall" target to Rakefile. check-in: efa5af7568 user: wbp tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to VERSION.

1
1.5.2
|
1
1.6.0

Changes to lib/xregistry.rb.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
85
86
87
88
89
90
91









































92
93
94
95
96
97
98
#  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:  19-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 = '')
................................................................................
      rescue Win32::Registry::Error
      end
    else
      @db.execute "drop table if exists \"#{section}\""
    end
  end
  alias delete_section deleteSection










































  # 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.
  def readBoolEntry(section, key, default = false)
    value = readEntry(section, key, default)
    raise TypeError if value.class == Float || value.class == String







|







 







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







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
..
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#  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
################################################################################


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 = '')
................................................................................
      rescue Win32::Registry::Error
      end
    else
      @db.execute "drop table if exists \"#{section}\""
    end
  end
  alias delete_section deleteSection

  # Iterate over the section keys of the registry.
  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.
  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.
  def readBoolEntry(section, key, default = false)
    value = readEntry(section, key, default)
    raise TypeError if value.class == Float || value.class == String

Changes to test/test_xregistry.rb.

1
2
3
4
5
6
7
8
9
..
13
14
15
16
17
18
19
























20
21
22
23
24
25
26
# test program for wregistry gem
# Last modified:  19-Mar-2016

require 'xregistry'
require 'test/unit'

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

  def test_new
    assert_equal(Registry, @@reg.class)
  rescue
    flunk "error encountered opening database: #{$!}"
  end

























  # test of readBoolEntry
  def test_read_bool_entry
    value = @@reg.readBoolEntry('Settings', 'bool_key1')
    assert_equal(false, value)

    value = @@reg.readBoolEntry('Settings', 'bool_key1', true)

|







 







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







1
2
3
4
5
6
7
8
9
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# test program for wregistry gem
# Last modified:  02-Aug-2016

require 'xregistry'
require 'test/unit'

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

  def test_new
    assert_equal(Registry, @@reg.class)
  rescue
    flunk "error encountered opening database: #{$!}"
  end

  # test of each_key
  def test_each_key
    keys = []
    @@reg.each_key { |k| keys << k }
    keys.each { |k| @@reg.deleteSection(k) }
    @@reg.writeIntEntry('Section1', 'key', 1)
    @@reg.writeIntEntry('Section2', 'key', 2)
    @@reg.writeIntEntry('Section3', 'key', 3)
    value = []
    @@reg.each_key { |k| value << k }
    assert_equal(%w[Section1 Section2 Section3], value)
  end

  # test of find
  def test_find
    @@reg.writeIntEntry('Section', 'key1', 1)
    @@reg.writeIntEntry('Section', 'key2', 2)
    @@reg.writeIntEntry('Section', 'key3', 3)
    value = []
    section = @@reg.find('Section')
    section.each_key { |k| value << [k, section.find(k)] }
    assert_equal([['key1', 1], ['key2', 2], ['key3', 3]], value)
  end

  # test of readBoolEntry
  def test_read_bool_entry
    value = @@reg.readBoolEntry('Settings', 'bool_key1')
    assert_equal(false, value)

    value = @@reg.readBoolEntry('Settings', 'bool_key1', true)