rock_config 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/rock_config/config.rb +22 -8
- data/lib/rock_config/manager.rb +1 -1
- data/lib/rock_config/version.rb +1 -1
- data/spec/config_spec.rb +13 -0
- data/spec/manager_spec.rb +13 -20
- metadata +2 -2
data/README.md
CHANGED
@@ -33,7 +33,7 @@ RockConfig allows you to read these settings with an API like this:
|
|
33
33
|
elastic_config = RockConfig.for "elastic_search"
|
34
34
|
elastic_config.host # > 127.0.0.1
|
35
35
|
|
36
|
-
RockConfig automatically chooses the current application environment. You select the environment yourself
|
36
|
+
RockConfig automatically chooses the current application environment. You can select the environment yourself
|
37
37
|
with:
|
38
38
|
|
39
39
|
RockConfig.for "elastic_search", "production"
|
data/lib/rock_config/config.rb
CHANGED
@@ -4,18 +4,32 @@ module RockConfig
|
|
4
4
|
@hash = hash
|
5
5
|
end
|
6
6
|
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
7
|
+
def [](key)
|
8
|
+
fetch(key)
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(name, *args, &block)
|
12
|
+
fetch(name.to_s)
|
15
13
|
end
|
16
14
|
|
17
15
|
def raw
|
18
16
|
@hash.dup
|
19
17
|
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def fetch(key, default = nil)
|
22
|
+
if value = @hash[key.to_s]
|
23
|
+
return value_or_config(value)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def value_or_config(value)
|
28
|
+
if Hash === value
|
29
|
+
Config.new(value)
|
30
|
+
else
|
31
|
+
value
|
32
|
+
end
|
33
|
+
end
|
20
34
|
end
|
21
35
|
end
|
data/lib/rock_config/manager.rb
CHANGED
@@ -7,7 +7,7 @@ module RockConfig
|
|
7
7
|
|
8
8
|
def fetch(config_name, environment)
|
9
9
|
if config = @configs[config_name]
|
10
|
-
if config_for_environment = config
|
10
|
+
if config_for_environment = config[environment]
|
11
11
|
return config_for_environment
|
12
12
|
else
|
13
13
|
raise EnvironmentNotFoundError,
|
data/lib/rock_config/version.rb
CHANGED
data/spec/config_spec.rb
CHANGED
@@ -50,5 +50,18 @@ module RockConfig
|
|
50
50
|
config = Config.new(hash)
|
51
51
|
config.development.elastic.host.should eq("localhost")
|
52
52
|
end
|
53
|
+
|
54
|
+
it "supports accessing the values by brackets" do
|
55
|
+
hash = {
|
56
|
+
"development" => {
|
57
|
+
"elastic" => {
|
58
|
+
"host" => "localhost"
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
config = Config.new(hash)
|
64
|
+
config["development"]["elastic"]["host"].should eq("localhost")
|
65
|
+
end
|
53
66
|
end
|
54
67
|
end
|
data/spec/manager_spec.rb
CHANGED
@@ -2,33 +2,26 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
module RockConfig
|
4
4
|
describe Manager do
|
5
|
-
let(:configuration)
|
5
|
+
let(:configuration) do
|
6
|
+
configuration = Configuration.new
|
7
|
+
configuration.scanned_directories << File.join(Dir.pwd, "spec", "fixtures")
|
8
|
+
configuration.config_loaders << YamlLoader.new
|
6
9
|
|
7
|
-
|
8
|
-
|
9
|
-
result.should_receive(:send).with("development") { "yay" }
|
10
|
-
|
11
|
-
scanner = mock("Scanner")
|
12
|
-
scanner.should_receive(:new) .with(configuration) { scanner }
|
13
|
-
scanner.should_receive(:find).with("sample") { result }
|
14
|
-
|
15
|
-
manager = Manager.new(configuration, scanner)
|
16
|
-
manager_result = manager.fetch "sample", "development"
|
10
|
+
configuration
|
11
|
+
end
|
17
12
|
|
18
|
-
|
13
|
+
it "returns config if found" do
|
14
|
+
manager = Manager.new(configuration)
|
15
|
+
manager_result = manager.fetch "database", "test"
|
16
|
+
manager_result.should_not be_nil
|
19
17
|
end
|
20
18
|
|
21
19
|
it "raises error if the config doesnt have the environment" do
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
scanner = mock("Scanner")
|
26
|
-
scanner.should_receive(:new) .with(configuration) { scanner }
|
27
|
-
scanner.should_receive(:find).with("sample") { result }
|
20
|
+
manager = Manager.new(configuration)
|
21
|
+
manager_result = manager.fetch "database", "development"
|
28
22
|
|
29
|
-
manager = Manager.new(configuration, scanner)
|
30
23
|
expect do
|
31
|
-
manager_result = manager.fetch "
|
24
|
+
manager_result = manager.fetch "database", "me no exist yo"
|
32
25
|
end.to raise_error(EnvironmentNotFoundError)
|
33
26
|
end
|
34
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rock_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|