confuse 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,7 @@ module Confuse
4
4
  class ConfigItem
5
5
  attr_reader :key
6
6
  attr_writer :value
7
+ attr_reader :default_value
7
8
 
8
9
  def initialize(name, &block)
9
10
  @key = name
@@ -36,8 +36,8 @@ module Confuse
36
36
  end
37
37
 
38
38
  def []=(key, value)
39
- puts "WARNING: changing config after it has been set!"
40
- mixin_config!({key => value})
39
+ puts 'WARNING: changing config after it has been set!'
40
+ mixin_config!({ key => value })
41
41
  end
42
42
 
43
43
  def to_hash
@@ -49,6 +49,19 @@ module Confuse
49
49
  end
50
50
  end
51
51
 
52
+ def params_hash
53
+ namespaces.reduce({}) do |memo, (name, namespace)|
54
+ namespace.keys.each do |key|
55
+ item = namespace.get_item(key)
56
+ memo[:"#{name}_#{key}"] = {
57
+ :type => item.type,
58
+ :doc => item.description,
59
+ :default => item.default_value }
60
+ end
61
+ memo
62
+ end
63
+ end
64
+
52
65
  # We allow the namespace and the key to be concatenated with an '_', so this
53
66
  # method is to search the possible substrings that could make up the
54
67
  # namespace for a key.
@@ -98,9 +111,7 @@ module Confuse
98
111
  if value.respond_to?(:keys)
99
112
  # if its a hash, set each key in the hash as a config item in the
100
113
  # namespace
101
- value.each do |k, v|
102
- namespace[k] = v
103
- end
114
+ value.each { |k, v| namespace[k] = v }
104
115
  else
105
116
  # otherwise, set it directly in the namespace
106
117
  namespace[rest_of_key(key, namespace_name)] = value
@@ -1,3 +1,3 @@
1
1
  module Confuse
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -4,6 +4,8 @@ require 'confuse'
4
4
  class Foo < Confuse::ConfigBase
5
5
  define :foo do
6
6
  default 'foo'
7
+ type :string
8
+ description 'foo'
7
9
  end
8
10
  end
9
11
 
@@ -20,5 +22,13 @@ class TestConfigBase < MiniTest::Unit::TestCase
20
22
  assert_equal 'bar', config1[:foo]
21
23
  assert_equal 'foo', config2[:foo]
22
24
  end
25
+
26
+ # This is easier to test with an instance of a config object, rather than as
27
+ # part of the test for the module where other tests can modify the model.
28
+ def test_params_hash
29
+ assert_equal({ :default_foo => { :type => :string, :doc => 'foo',
30
+ :default => 'foo' } },
31
+ Foo.new.params_hash)
32
+ end
23
33
  end
24
34
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confuse
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tom Chipchase
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-10-14 00:00:00 Z
18
+ date: 2013-11-06 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: inifile