econfig 1.0.2 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2cea87052bc286ddf6358f088fa55f43496f8781
4
- data.tar.gz: 884f074a2bde2b167ba897b809bb10e02f2239d9
3
+ metadata.gz: f4ba24341ee1636314ae2fa645edd20fddacedf8
4
+ data.tar.gz: 8741ff1333019ae2ef21773bedf700beb497a3e0
5
5
  SHA512:
6
- metadata.gz: 435d78b9d71c5bcbb41d8869887afe8f661e8f6ffe7747374cfef3dd6e8b3e565b0a4a9c949f701c316e47540c6a40a66e2ce335cd8e224da206e04198409dda
7
- data.tar.gz: 8f63c476e95c66e8b064a0e63c01c471950b3ec0bdb9220d10c6265abb5698110f3745a6c918570418af2de0294c6ab6662bd161c1cb24d357fa29f3fa4afc87
6
+ metadata.gz: 8cdb9f56898c683166eb2fa29dc107fcba153e2daa0ea8358419504be251e39013339f9fd438835a368db05385bddc531fe391baac13923f3abff052f0c48f55
7
+ data.tar.gz: 6c1e783e2e0c2ba72ec2df956370da81269ee185faf6f1cec7bf2c92cb245bef4939eab20accaf7456e847a58b971c4c450170f770e980004579ef61b9b2f92e
@@ -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
 
@@ -44,6 +44,11 @@ module Econfig
44
44
  @backends.delete_at(index_of!(name))
45
45
  end
46
46
 
47
+ def backend_for(key)
48
+ result = @backends.find { |(n, b)| b.has_key?(key) }
49
+ result[1] if result
50
+ end
51
+
47
52
  private
48
53
 
49
54
  def exists‽(name)
@@ -7,15 +7,19 @@ module Econfig
7
7
  end
8
8
 
9
9
  def fetch(key)
10
- self[key] or raise Econfig::NotFound, "configuration key '#{key}' is not set"
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
- backends.each do |backend|
15
- value = backend.get(key.to_s)
16
- return value if value
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)
@@ -1,5 +1,9 @@
1
1
  module Econfig
2
2
  class ENV
3
+ def has_key?(key)
4
+ ::ENV.has_key?(key.upcase)
5
+ end
6
+
3
7
  def get(key)
4
8
  ::ENV[key.upcase]
5
9
  end
@@ -5,6 +5,10 @@ module Econfig
5
5
  @options = {}
6
6
  end
7
7
 
8
+ def has_key?(key)
9
+ @options.has_key?(key)
10
+ end
11
+
8
12
  def get(key)
9
13
  @options[key]
10
14
  end
@@ -7,8 +7,6 @@ module Econfig
7
7
  Econfig.root = Rails.root
8
8
  Econfig.env = Rails.env
9
9
  Rails.application.config.app = Econfig.instance
10
-
11
- Econfig.init
12
10
  end
13
11
  end
14
12
  end
@@ -4,6 +4,10 @@ module Econfig
4
4
  @redis = redis
5
5
  end
6
6
 
7
+ def has_key?(key)
8
+ @redis.exists(key)
9
+ end
10
+
7
11
  def get(key)
8
12
  @redis.get(key)
9
13
  end
@@ -1,3 +1,3 @@
1
1
  module Econfig
2
- VERSION = "1.0.2"
2
+ VERSION = "2.0.0"
3
3
  end
@@ -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 init
12
- require "yaml"
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 or raise Econfig::UninitializedError
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
@@ -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(:get).with("foobar").and_return(nil)
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(:get).with("foobar").and_return(nil)
30
- other_backend.stub(:get).with("foobar").and_return(nil)
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(:get).with("foobar").and_return(nil)
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(:get).with("foobar").and_return(nil)
76
- other_backend.stub(:get).with("foobar").and_return(nil)
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(:get).with("foobar").and_return(nil)
93
- other_backend.stub(:get).with("foobar").and_return(nil)
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(:get).with("foobar").and_return(nil)
100
- other_backend.stub(:get).with("foobar").and_return(nil)
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
@@ -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
@@ -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")
@@ -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")
@@ -2,4 +2,3 @@ require "econfig"
2
2
 
3
3
  Econfig.root = File.dirname(__FILE__)
4
4
  Econfig.env = "test"
5
- Econfig.init
@@ -1,5 +1,21 @@
1
1
  describe Econfig::YAML do
2
- let(:backend) { Econfig::YAML.new("config/app.yml").tap(&:init) }
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").tap(&:init)
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: 1.0.2
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: 2014-11-24 00:00:00.000000000 Z
12
+ date: 2015-02-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake