nested_config 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c3e61291df4a84b9d787f55e4e25950916b47491
4
- data.tar.gz: 753fdf2d4c08633e1db24ba88c6a5e275e0c0372
3
+ metadata.gz: a357315a32e5b011e722c36ca2e97bf0f45d86d0
4
+ data.tar.gz: f60e835020536810df8a3eda0c5de853c1a54da6
5
5
  SHA512:
6
- metadata.gz: 9c4b5c1204481ed4d1971c17a5141bf2a1b0caffea9194aa48794bbc41fd1cea5ef6b3c9104cf13c9e49cd2fb2692a0e3f898175158ba4c47bc3a0521080db8b
7
- data.tar.gz: 0d72f78de137a1775d6fe5ea786225ce71fd22c52e44771ca3f4bee16d40f68f8a0f9fe44cc333f17033ea9f4dd90deafe9e3b3fbcc154e0eff513b93cc566df
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 __with_cloned__
23
- backup = Marshal.load(Marshal.dump(@hash))
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 respond_to?(name, include_private=false)
72
+ def respond_to_missing?(name, include_private=false)
48
73
  __hash__.key?(name.to_s) || super
49
74
  end
50
75
  end
@@ -1,3 +1,3 @@
1
1
  class NestedConfig
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  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?(:__with_cloned__) or raise ValueNotCloneable.new(current)
48
+ current.respond_to?(:__hash__) or raise ValueNotCloneable.new(current)
49
49
 
50
- current.__with_cloned__(&block)
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
@@ -15,9 +15,16 @@ class NestedConfigTest < NestedConfigSpec
15
15
  end
16
16
  end
17
17
 
18
- test "inspect retrives __hash__" do
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
- assert_same c.inspect, c.__hash__
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 "respond_to?" do
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.1
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-07-16 00:00:00.000000000 Z
11
+ date: 2014-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake