nested_config 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/nested_config.rb +35 -10
- data/lib/nested_config/version.rb +1 -1
- data/lib/nested_config/with_config.rb +5 -2
- data/test/nested_config_test.rb +10 -43
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a357315a32e5b011e722c36ca2e97bf0f45d86d0
|
4
|
+
data.tar.gz: f60e835020536810df8a3eda0c5de853c1a54da6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a68a27481299c850f378c21e6e3ad0315d35307bf26a1134ff7a9ceace977f86ef6efbf283d5dd2255a121642ab6060bd76f01851132693f9a04ff6617650689
|
7
|
+
data.tar.gz: 95f6909ff632d697c5cc0deb443980e970a456e4bc808564182860a752df8c803472d7518fc0cd684d523926e54c8dd76f8d8db1861053de4ac4780bfe71d86d
|
data/lib/nested_config.rb
CHANGED
@@ -1,5 +1,37 @@
|
|
1
1
|
require 'nested_config/version'
|
2
2
|
|
3
|
+
# Simple, static, nested application configuration.
|
4
|
+
#
|
5
|
+
# == Example
|
6
|
+
#
|
7
|
+
# require 'nested_config'
|
8
|
+
#
|
9
|
+
# class MyApp
|
10
|
+
# def self.configure
|
11
|
+
# yield config
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# def self.config
|
15
|
+
# @config ||= MyConfig.new
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# class MyConfig < NestedConfig
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# MyApp.configure do |config|
|
23
|
+
# config.coins = 1000
|
24
|
+
# config.user do |user|
|
25
|
+
# user.max = 5
|
26
|
+
# end
|
27
|
+
# config.queue do |queue|
|
28
|
+
# queue.workers do |workers|
|
29
|
+
# workers.max = 2
|
30
|
+
# workers.timeout = 60.seconds
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
# end
|
34
|
+
#
|
3
35
|
class NestedConfig
|
4
36
|
autoload :EvaluateOnce, 'nested_config/evaluate_once'
|
5
37
|
|
@@ -19,15 +51,8 @@ class NestedConfig
|
|
19
51
|
@hash
|
20
52
|
end
|
21
53
|
|
22
|
-
def
|
23
|
-
|
24
|
-
yield(self)
|
25
|
-
ensure
|
26
|
-
@hash = backup if backup
|
27
|
-
end
|
28
|
-
|
29
|
-
def inspect
|
30
|
-
@hash
|
54
|
+
def __hash__=(hash)
|
55
|
+
@hash = hash
|
31
56
|
end
|
32
57
|
|
33
58
|
def method_missing(name, *args)
|
@@ -44,7 +69,7 @@ class NestedConfig
|
|
44
69
|
end
|
45
70
|
end
|
46
71
|
|
47
|
-
def
|
72
|
+
def respond_to_missing?(name, include_private=false)
|
48
73
|
__hash__.key?(name.to_s) || super
|
49
74
|
end
|
50
75
|
end
|
@@ -45,9 +45,12 @@ class NestedConfig
|
|
45
45
|
current = keys.inject(config) do |config, key|
|
46
46
|
config[key] or raise KeyNotFound.new(key, keys)
|
47
47
|
end
|
48
|
-
current.respond_to?(:
|
48
|
+
current.respond_to?(:__hash__) or raise ValueNotCloneable.new(current)
|
49
49
|
|
50
|
-
current.
|
50
|
+
backup = Marshal.load(Marshal.dump(current.__hash__))
|
51
|
+
yield current
|
52
|
+
ensure
|
53
|
+
current.__hash__ = backup if backup
|
51
54
|
end
|
52
55
|
|
53
56
|
class KeyNotFound < ArgumentError
|
data/test/nested_config_test.rb
CHANGED
@@ -15,9 +15,16 @@ class NestedConfigTest < NestedConfigSpec
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
test "
|
18
|
+
test "sets raw __hash__" do
|
19
|
+
config.__hash__ = { "foo" => 23 }
|
20
|
+
assert_equal 23, config.foo
|
21
|
+
end
|
22
|
+
|
23
|
+
test "inspect" do
|
19
24
|
c = config.tap { |c| c.foo = :bar }
|
20
|
-
|
25
|
+
|
26
|
+
hash = c.__hash__.inspect
|
27
|
+
assert_match %r{#<NestedConfig:0x[0-9a-z]+ @hash=#{hash}>}, c.inspect
|
21
28
|
end
|
22
29
|
|
23
30
|
test "cannot use defined method names as keys" do
|
@@ -79,7 +86,7 @@ class NestedConfigTest < NestedConfigSpec
|
|
79
86
|
assert_match /NilClass/, e.message
|
80
87
|
end
|
81
88
|
|
82
|
-
test "
|
89
|
+
test "respond_to_missing?" do
|
83
90
|
c = config.tap do |c|
|
84
91
|
c.foo = :bar
|
85
92
|
end
|
@@ -124,45 +131,5 @@ class NestedConfigTest < NestedConfigSpec
|
|
124
131
|
assert_equal :bar!, c.users.foo?
|
125
132
|
end
|
126
133
|
end
|
127
|
-
|
128
|
-
context "__with_cloned__" do
|
129
|
-
let(:config) do
|
130
|
-
NestedConfig.new.tap do |config|
|
131
|
-
config.top_level = 1
|
132
|
-
config.nest do |nest|
|
133
|
-
nest.level = 1
|
134
|
-
nest.deep do |deep|
|
135
|
-
deep.level = 2
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
test "top level" do
|
142
|
-
config.__with_cloned__ do |c|
|
143
|
-
c.top_level = 2
|
144
|
-
assert_equal 2, config.top_level
|
145
|
-
end
|
146
|
-
assert_equal 1, config.top_level
|
147
|
-
end
|
148
|
-
|
149
|
-
test "nested" do
|
150
|
-
config.__with_cloned__ do |c|
|
151
|
-
c.nest.deep.level = 23
|
152
|
-
assert_equal 23, config.nest.deep.level
|
153
|
-
end
|
154
|
-
assert_equal 2, config.nest.deep.level
|
155
|
-
end
|
156
|
-
|
157
|
-
test "still usable if it's undumpable" do
|
158
|
-
config.undumpable = proc {}
|
159
|
-
|
160
|
-
assert_raises TypeError do
|
161
|
-
config.__with_cloned__ {}
|
162
|
-
end
|
163
|
-
|
164
|
-
assert_equal 1, config.top_level
|
165
|
-
end
|
166
|
-
end
|
167
134
|
end
|
168
135
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nested_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Suschlik
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|