econfig 1.0.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/econfig.rb +0 -6
- data/lib/econfig/backend_collection.rb +5 -0
- data/lib/econfig/configuration.rb +10 -6
- data/lib/econfig/env.rb +4 -0
- data/lib/econfig/memory.rb +4 -0
- data/lib/econfig/rails.rb +0 -2
- data/lib/econfig/redis.rb +4 -0
- data/lib/econfig/version.rb +1 -1
- data/lib/econfig/yaml.rb +17 -9
- data/spec/configuration_spec.rb +30 -10
- data/spec/env_spec.rb +19 -1
- data/spec/memory_spec.rb +12 -0
- data/spec/redis_spec.rb +12 -0
- data/spec/spec_helper.rb +0 -1
- data/spec/yaml_spec.rb +18 -2
- 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: f4ba24341ee1636314ae2fa645edd20fddacedf8
|
4
|
+
data.tar.gz: 8741ff1333019ae2ef21773bedf700beb497a3e0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cdb9f56898c683166eb2fa29dc107fcba153e2daa0ea8358419504be251e39013339f9fd438835a368db05385bddc531fe391baac13923f3abff052f0c48f55
|
7
|
+
data.tar.gz: 6c1e783e2e0c2ba72ec2df956370da81269ee185faf6f1cec7bf2c92cb245bef4939eab20accaf7456e847a58b971c4c450170f770e980004579ef61b9b2f92e
|
data/lib/econfig.rb
CHANGED
@@ -17,12 +17,6 @@ module Econfig
|
|
17
17
|
attr_accessor :root, :env, :instance
|
18
18
|
|
19
19
|
def_delegators :instance, :backends, :default_write_backend, :default_write_backend=
|
20
|
-
|
21
|
-
def init
|
22
|
-
backends.each do |backend|
|
23
|
-
backend.init if backend.respond_to?(:init)
|
24
|
-
end
|
25
|
-
end
|
26
20
|
end
|
27
21
|
end
|
28
22
|
|
@@ -7,15 +7,19 @@ module Econfig
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def fetch(key)
|
10
|
-
|
10
|
+
key = key.to_s
|
11
|
+
backend = backends.backend_for(key)
|
12
|
+
if backend
|
13
|
+
backend.get(key)
|
14
|
+
else
|
15
|
+
raise Econfig::NotFound, "configuration key '#{key}' is not set"
|
16
|
+
end
|
11
17
|
end
|
12
18
|
|
13
19
|
def [](key)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
18
|
-
nil
|
20
|
+
key = key.to_s
|
21
|
+
backend = backends.backend_for(key)
|
22
|
+
backend.get(key) if backend
|
19
23
|
end
|
20
24
|
|
21
25
|
def []=(backend_name = default_write_backend, key, value)
|
data/lib/econfig/env.rb
CHANGED
data/lib/econfig/memory.rb
CHANGED
data/lib/econfig/rails.rb
CHANGED
data/lib/econfig/redis.rb
CHANGED
data/lib/econfig/version.rb
CHANGED
data/lib/econfig/yaml.rb
CHANGED
@@ -1,31 +1,39 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require "erb"
|
3
|
+
|
1
4
|
module Econfig
|
2
5
|
class YAML
|
3
6
|
def initialize(path)
|
4
7
|
@path = path
|
8
|
+
@mutex = Mutex.new
|
9
|
+
@options = nil
|
5
10
|
end
|
6
11
|
|
7
12
|
def get(key)
|
8
13
|
options[key]
|
9
14
|
end
|
10
15
|
|
11
|
-
def
|
12
|
-
|
13
|
-
require "erb"
|
14
|
-
if File.exist?(path)
|
15
|
-
@options = ::YAML.load(::ERB.new(File.read(path)).result)[Econfig.env]
|
16
|
-
else
|
17
|
-
@options = {}
|
18
|
-
end
|
16
|
+
def has_key?(key)
|
17
|
+
options.has_key?(key)
|
19
18
|
end
|
20
19
|
|
21
20
|
private
|
22
21
|
|
23
22
|
def path
|
23
|
+
raise Econfig::UninitializedError, "Econfig.root is not set" unless Econfig.root
|
24
24
|
File.expand_path(@path, Econfig.root)
|
25
25
|
end
|
26
26
|
|
27
27
|
def options
|
28
|
-
@options
|
28
|
+
return @options if @options
|
29
|
+
|
30
|
+
@mutex.synchronize do
|
31
|
+
@options ||= if File.exist?(path)
|
32
|
+
::YAML.load(::ERB.new(File.read(path)).result)[Econfig.env] || {}
|
33
|
+
else
|
34
|
+
{}
|
35
|
+
end
|
36
|
+
end
|
29
37
|
end
|
30
38
|
end
|
31
39
|
end
|
data/spec/configuration_spec.rb
CHANGED
@@ -10,24 +10,27 @@ describe Econfig::Configuration do
|
|
10
10
|
|
11
11
|
describe "#[]" do
|
12
12
|
it "returns response from first backend" do
|
13
|
+
backend.stub(:has_key?).with("foobar").and_return(true)
|
13
14
|
backend.stub(:get).with("foobar").and_return("elephant")
|
14
15
|
config["foobar"].should == "elephant"
|
15
16
|
end
|
16
17
|
|
17
18
|
it "casts key to string" do
|
19
|
+
backend.stub(:has_key?).with("foobar").and_return(true)
|
18
20
|
backend.stub(:get).with("foobar").and_return("elephant")
|
19
21
|
config[:foobar].should == "elephant"
|
20
22
|
end
|
21
23
|
|
22
24
|
it "tries multiple backends until it finds a response" do
|
23
|
-
backend.stub(:
|
25
|
+
backend.stub(:has_key?).with("foobar").and_return(false)
|
26
|
+
other_backend.stub(:has_key?).with("foobar").and_return(true)
|
24
27
|
other_backend.stub(:get).with("foobar").and_return("elephant")
|
25
28
|
config["foobar"].should == "elephant"
|
26
29
|
end
|
27
30
|
|
28
31
|
it "returns nil if the key can't be found in any backend" do
|
29
|
-
backend.stub(:
|
30
|
-
other_backend.stub(:
|
32
|
+
backend.stub(:has_key?).with("foobar").and_return(false)
|
33
|
+
other_backend.stub(:has_key?).with("foobar").and_return(false)
|
31
34
|
config["foobar"].should == nil
|
32
35
|
end
|
33
36
|
end
|
@@ -61,21 +64,30 @@ describe Econfig::Configuration do
|
|
61
64
|
|
62
65
|
describe "#fetch" do
|
63
66
|
it "returns response from first backend" do
|
67
|
+
backend.stub(:has_key?).with("foobar").and_return(true)
|
64
68
|
backend.stub(:get).with("foobar").and_return("elephant")
|
65
69
|
config.fetch("foobar").should == "elephant"
|
66
70
|
end
|
67
71
|
|
68
72
|
it "tries multiple backends until it finds a response" do
|
69
|
-
backend.stub(:
|
73
|
+
backend.stub(:has_key?).with("foobar").and_return(false)
|
74
|
+
other_backend.stub(:has_key?).with("foobar").and_return(true)
|
70
75
|
other_backend.stub(:get).with("foobar").and_return("elephant")
|
71
76
|
config.fetch("foobar").should == "elephant"
|
72
77
|
end
|
73
78
|
|
74
79
|
it "raises error if the key can't be found in any backend" do
|
75
|
-
backend.stub(:
|
76
|
-
other_backend.stub(:
|
80
|
+
backend.stub(:has_key?).with("foobar").and_return(false)
|
81
|
+
other_backend.stub(:has_key?).with("foobar").and_return(false)
|
77
82
|
expect { config.fetch("foobar") }.to raise_error(Econfig::NotFound)
|
78
83
|
end
|
84
|
+
|
85
|
+
it "allows falsy responses" do
|
86
|
+
backend.stub(:has_key?).with("foobar").and_return(false)
|
87
|
+
other_backend.stub(:has_key?).with("foobar").and_return(true)
|
88
|
+
other_backend.stub(:get).with("foobar").and_return(false)
|
89
|
+
config.fetch("foobar").should == false
|
90
|
+
end
|
79
91
|
end
|
80
92
|
|
81
93
|
describe "#method_missing" do
|
@@ -84,23 +96,31 @@ describe Econfig::Configuration do
|
|
84
96
|
end
|
85
97
|
|
86
98
|
it "calls fetch for normal methods" do
|
99
|
+
backend.stub(:has_key?).with("foobar").and_return(true)
|
87
100
|
backend.stub(:get).with("foobar").and_return("elephant")
|
88
101
|
config.foobar.should == "elephant"
|
89
102
|
end
|
90
103
|
|
91
104
|
it "raises error if the key can't be found in any backend" do
|
92
|
-
backend.stub(:
|
93
|
-
other_backend.stub(:
|
105
|
+
backend.stub(:has_key?).with("foobar").and_return(false)
|
106
|
+
other_backend.stub(:has_key?).with("foobar").and_return(false)
|
94
107
|
expect { config.foobar }.to raise_error(Econfig::NotFound)
|
95
108
|
end
|
96
109
|
|
97
110
|
it "returns nil if environment variable bypass is set" do
|
98
111
|
ENV["ECONFIG_PERMISSIVE"] = "true"
|
99
|
-
backend.stub(:
|
100
|
-
other_backend.stub(:
|
112
|
+
backend.stub(:has_key?).with("foobar").and_return(false)
|
113
|
+
other_backend.stub(:has_key?).with("foobar").and_return(false)
|
101
114
|
config.foobar.should be_nil
|
102
115
|
end
|
103
116
|
|
117
|
+
it "allows falsy responses" do
|
118
|
+
backend.stub(:has_key?).with("foobar").and_return(false)
|
119
|
+
other_backend.stub(:has_key?).with("foobar").and_return(true)
|
120
|
+
other_backend.stub(:get).with("foobar").and_return(false)
|
121
|
+
config.foobar.should == false
|
122
|
+
end
|
123
|
+
|
104
124
|
it "raises NoMethodError for bang methods" do
|
105
125
|
expect { config.foobar! }.to raise_error(NoMethodError)
|
106
126
|
end
|
data/spec/env_spec.rb
CHANGED
@@ -1,9 +1,27 @@
|
|
1
1
|
describe Econfig::ENV do
|
2
2
|
let(:backend) { Econfig::ENV.new }
|
3
|
+
|
4
|
+
before do
|
5
|
+
ENV["FOO_BAR"] = "monkey"
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#has_key?" do
|
9
|
+
it "returns true if key exists" do
|
10
|
+
backend.has_key?("foo_bar").should eq(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns false if key is not set" do
|
14
|
+
backend.has_key?("does_not_exist").should eq(false)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
3
18
|
describe "#get" do
|
4
19
|
it "fetches option from environment variables" do
|
5
|
-
ENV["FOO_BAR"] = "monkey"
|
6
20
|
backend.get("foo_bar").should == "monkey"
|
7
21
|
end
|
22
|
+
|
23
|
+
it "returns nil when not set" do
|
24
|
+
backend.get("does_not_exist").should == nil
|
25
|
+
end
|
8
26
|
end
|
9
27
|
end
|
data/spec/memory_spec.rb
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
describe Econfig::Memory do
|
2
2
|
let(:backend) { Econfig::Memory.new }
|
3
|
+
|
4
|
+
describe "#has_key?" do
|
5
|
+
it "returns true if key exists" do
|
6
|
+
backend.set("foo", "bar")
|
7
|
+
backend.has_key?("foo").should eq(true)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns false if key is not set" do
|
11
|
+
backend.has_key?("foo").should eq(false)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
describe "#get" do
|
4
16
|
it "fetches a previously set option" do
|
5
17
|
backend.set("foo", "bar")
|
data/spec/redis_spec.rb
CHANGED
@@ -7,6 +7,18 @@ describe Econfig::Redis do
|
|
7
7
|
after do |example|
|
8
8
|
redis.flushdb
|
9
9
|
end
|
10
|
+
|
11
|
+
describe "#has_key?" do
|
12
|
+
it "returns true if key exists" do
|
13
|
+
backend.set("foo", "bar")
|
14
|
+
backend.has_key?("foo").should eq(true)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns false if key is not set" do
|
18
|
+
backend.has_key?("foo").should eq(false)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
10
22
|
describe "#get" do
|
11
23
|
it "fetches a previously set option" do
|
12
24
|
backend.set("foo", "bar")
|
data/spec/spec_helper.rb
CHANGED
data/spec/yaml_spec.rb
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
describe Econfig::YAML do
|
2
|
-
let(:backend) { Econfig::YAML.new("config/app.yml")
|
2
|
+
let(:backend) { Econfig::YAML.new("config/app.yml") }
|
3
|
+
|
4
|
+
describe "#has_key?" do
|
5
|
+
it "returns true if option exists" do
|
6
|
+
expect(backend.has_key?("quox")).to eq(true)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns false when the key does not exist" do
|
10
|
+
expect(backend.has_key?("does_not_exist")).to eq(false)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns false when there is no config file" do
|
14
|
+
backend = Econfig::YAML.new("/does/not/exist")
|
15
|
+
expect(backend.has_key?("does_not_exist")).to eq(false)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
3
19
|
describe "#get" do
|
4
20
|
it "fetches option from yaml config file" do
|
5
21
|
backend.get("quox").should == "baz"
|
@@ -10,7 +26,7 @@ describe Econfig::YAML do
|
|
10
26
|
end
|
11
27
|
|
12
28
|
it "returns nil when there is no config file" do
|
13
|
-
backend = Econfig::YAML.new("/does/not/exist")
|
29
|
+
backend = Econfig::YAML.new("/does/not/exist")
|
14
30
|
backend.get("does_not_exist").should be_nil
|
15
31
|
end
|
16
32
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: econfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Nicklas
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-02-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|