cconfig 1.1.1 → 1.2.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: 5ea447e81e322d1bf22dddca71e18e2eab21760d
4
- data.tar.gz: 003941dd51eebfa38fea464be15f861bcc49f28c
3
+ metadata.gz: 5f377c3a860f4c0bcb82f5f8cc4601921d0a1536
4
+ data.tar.gz: 0016898111f22120de5fd23ff97215388a44596b
5
5
  SHA512:
6
- metadata.gz: 841ebc1ec251e9490865f5e7f918ef38051402898e604b3b8e2c93ed42fbb2c74609a1b6f39b344d367ad779892e29440840eb218c3a11f122adcea105d98aeb
7
- data.tar.gz: 2a18d68a5ea16c626e1d245549d67425c4fe6a0c301e6476f032f562730c3e7484e36acfb8935a5c8d4f8c90a604951013866f9d25e47a4620b3368f33af60bc
6
+ metadata.gz: fabee37df15fa57f9ee82b408348a11829bac6616bd5a63e9b76b36052ce6e3ee23e7fed3301c2f55add43223a67bb571c0fcd64f2440cb92fd4cb8cb671724c
7
+ data.tar.gz: 288e140df8d6b47f3d7efa3d5980b96c3881c8f36aa17ed888824f7e9f443eb0da6f3fac42934a6b0a5dd512ee37c20379c2ba38994f1f4453efdcced5f27113
@@ -1,5 +1,14 @@
1
1
  # Changelog
2
2
 
3
+ ## Next version
4
+
5
+ ## 1.2.0
6
+
7
+ - Added the `#default_of` method for the returned hash (hence `APP_CONFIG` as
8
+ well). This method returns the default value for a given key. It can be useful
9
+ to check whether a configuration value has been set by either environment
10
+ variables or the local configuration. See [commit](50d638c8d81bab6b17164a1a5661dc2ca730cf92).
11
+
3
12
  ## 1.1.1
4
13
 
5
14
  - Fixed crash on nested values on the `#enabled?` method. See [#3](https://github.com/mssola/cconfig/issues/3).
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cconfig (1.1.1)
4
+ cconfig (1.2.0)
5
5
  safe_yaml (~> 1.0.0, >= 1.0.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # CConfig [![Build Status](https://travis-ci.org/mssola/cconfig.svg?branch=master)](https://travis-ci.org/mssola/cconfig)
1
+ # CConfig [![Build Status](https://travis-ci.org/mssola/cconfig.svg?branch=master)](https://travis-ci.org/mssola/cconfig) [![Gem Version](https://badge.fury.io/rb/cconfig.svg)](https://badge.fury.io/rb/cconfig)
2
2
 
3
3
  CConfig (Container Config) is a container-aware configuration management
4
4
  gem. This is useful for applications that want to keep a reference configuration
@@ -24,7 +24,8 @@ require "yaml"
24
24
  module CConfig
25
25
  # Config is the main class of this library. It allows you to fetch the current
26
26
  # configuration (after merging the values from all sources) as a hash. This
27
- # has will have the special method `::CConfig::HashUtils::Extensions#enabled?`.
27
+ # has will have some specials methods: `::CConfig::HashUtils::Extensions#enabled?`,
28
+ # `::CConfig::HashUtils::Extensions#disabled?` and `::CConfig::HashUtils::Extensions#default_of`.
28
29
  class Config
29
30
  include ::CConfig::HashUtils
30
31
 
@@ -42,12 +43,13 @@ module CConfig
42
43
 
43
44
  # Returns a hash with the app configuration contained in it.
44
45
  def fetch
45
- cfg = {}
46
- cfg = YAML.load_file(@default) if File.file?(@default)
46
+ cfg = File.file?(@default) ? YAML.load_file(@default) : {}
47
47
  local = fetch_local
48
48
 
49
49
  hsh = strict_merge_with_env(default: cfg, local: local, prefix: @prefix)
50
50
  hsh.extend(::CConfig::HashUtils::Extensions)
51
+ hsh.defaults = cfg
52
+ hsh
51
53
  end
52
54
 
53
55
  # Returns a string representation of the evaluated configuration.
@@ -24,6 +24,8 @@ module CConfig
24
24
  # Extensions contains the methods to be provided for each hash object
25
25
  # produced by this gem.
26
26
  module Extensions
27
+ attr_accessor :defaults
28
+
27
29
  # Returns true if the given feature is enabled, false otherwise. This also
28
30
  # works in embedded configuration values. For example: enabled?("a.b")
29
31
  # will return true for:
@@ -48,6 +50,19 @@ module CConfig
48
50
  def disabled?(feature)
49
51
  !enabled?(feature)
50
52
  end
53
+
54
+ # Returns the default value of the given key. Note that this key can
55
+ # specify nested values with the period notation (e.g. "a.b").
56
+ def default_of(key)
57
+ cur = defaults
58
+
59
+ key.split(".").each do |part|
60
+ cur = cur[part]
61
+ return if cur.nil?
62
+ end
63
+
64
+ cur
65
+ end
51
66
  end
52
67
 
53
68
  protected
@@ -19,5 +19,5 @@
19
19
 
20
20
  module CConfig
21
21
  # The current version of CConfig.
22
- VERSION = "1.1.1".freeze
22
+ VERSION = "1.2.0".freeze
23
23
  end
@@ -30,64 +30,93 @@ end
30
30
  describe CConfig::Config do
31
31
  after do
32
32
  ["TEST_LOCAL_CONFIG_PATH"].each { |key| ENV[key] = nil }
33
+ ["TEST_LDAP_COUNT", "TEST_ANOTHER_ENABLED", "TEST_LDAP_STRING"].each do |key|
34
+ ENV[key] = nil
35
+ end
33
36
  end
34
37
 
35
- it "returns an empty config if neither the global nor the local were found" do
36
- cfg = get_config("", "").fetch
37
- expect(cfg).to be_empty
38
- end
38
+ it "selects the proper local file depending of the environment variable" do
39
+ # Instead of bad.yml (which will raise an error on `fetch`), we will pick up
40
+ # the local.yml file.
41
+ base = File.join(File.dirname(__FILE__), "fixtures")
42
+ local = File.join(base, "bad.yml")
43
+ ENV["CCONFIG_LOCAL_CONFIG_PATH"] = File.join(base, "local.yml")
39
44
 
40
- it "only uses the global if the local config was not found" do
41
- cfg = get_config("config.yml", "").fetch
42
- expect(cfg["gravatar"]["enabled"]).to be_truthy
45
+ # Passing nil to the prefix on purpose (see SUSE/Portus#1379)
46
+ cfg = ::CConfig::Config.new(default: "config.yml", local: local, prefix: nil)
47
+ expect { cfg.fetch }.not_to raise_error
43
48
  end
44
49
 
45
- it "merges both config files and work as expected" do
46
- cfg = get_config("config.yml", "local.yml").fetch
50
+ describe "Merging configuration values" do
51
+ it "returns an empty config if neither the global nor the local were found" do
52
+ cfg = get_config("", "").fetch
53
+ expect(cfg).to be_empty
54
+ end
47
55
 
48
- expect(cfg.enabled?("gravatar")).to be_truthy
49
- expect(cfg.enabled?("ldap")).to be_truthy
50
- expect(cfg["ldap"]["hostname"]).to eq "ldap.example.com"
51
- expect(cfg["ldap"]["port"]).to eq 389
52
- expect(cfg["ldap"]["base"]).to eq "ou=users,dc=example,dc=com"
53
- expect(cfg["unknown"]).to be nil
54
- end
56
+ it "only uses the global if the local config was not found" do
57
+ cfg = get_config("config.yml", "").fetch
58
+ expect(cfg["gravatar"]["enabled"]).to be_truthy
59
+ end
55
60
 
56
- it "raises an error when the local file is badly formatted" do
57
- bad = get_config("config.yml", "bad.yml")
58
- msg = "Wrong format for the config-local file!"
59
- expect { bad.fetch }.to raise_error(::CConfig::FormatError, msg)
60
- end
61
+ it "merges both config files and work as expected" do
62
+ cfg = get_config("config.yml", "local.yml").fetch
61
63
 
62
- it "returns the proper config while hiding passwords" do
63
- cfg = get_config("config.yml", "local.yml")
64
- fetched = cfg.fetch
65
- evaled = YAML.safe_load(cfg.to_s)
64
+ expect(cfg.enabled?("gravatar")).to be_truthy
65
+ expect(cfg.enabled?("ldap")).to be_truthy
66
+ expect(cfg["ldap"]["hostname"]).to eq "ldap.example.com"
67
+ expect(cfg["ldap"]["port"]).to eq 389
68
+ expect(cfg["ldap"]["base"]).to eq "ou=users,dc=example,dc=com"
69
+ expect(cfg["unknown"]).to be nil
70
+ end
66
71
 
67
- expect(fetched).not_to eq(evaled)
68
- fetched["ldap"]["authentication"]["password"] = "****"
69
- expect(fetched).to eq(evaled)
70
- end
72
+ it "raises an error when the local file is badly formatted" do
73
+ bad = get_config("config.yml", "bad.yml")
74
+ msg = "Wrong format for the config-local file!"
75
+ expect { bad.fetch }.to raise_error(::CConfig::FormatError, msg)
76
+ end
77
+
78
+ it "returns the proper config while hiding passwords" do
79
+ cfg = get_config("config.yml", "local.yml")
80
+ fetched = cfg.fetch
81
+ evaled = YAML.safe_load(cfg.to_s)
71
82
 
72
- it "works for nested options" do
73
- cfg = get_config("config.yml", "").fetch
74
- expect(cfg.enabled?("email.smtp")).to be true
83
+ expect(fetched).not_to eq(evaled)
84
+ fetched["ldap"]["authentication"]["password"] = "****"
85
+ expect(fetched).to eq(evaled)
86
+ end
75
87
  end
76
88
 
77
- it "offers the #disabled? method" do
78
- cfg = get_config("config.yml", "").fetch
79
- expect(cfg.disabled?("ldap")).to be_truthy
89
+ describe "#enabled?" do
90
+ it "works for nested options" do
91
+ cfg = get_config("config.yml", "").fetch
92
+ expect(cfg.enabled?("email.smtp")).to be true
93
+ end
94
+
95
+ it "offers the #disabled? method" do
96
+ cfg = get_config("config.yml", "").fetch
97
+ expect(cfg.disabled?("ldap")).to be_truthy
98
+ end
80
99
  end
81
100
 
82
- it "selects the proper local file depending of the environment variable" do
83
- # Instead of bad.yml (which will raise an error on `fetch`), we will pick up
84
- # the local.yml file.
85
- base = File.join(File.dirname(__FILE__), "fixtures")
86
- local = File.join(base, "bad.yml")
87
- ENV["CCONFIG_LOCAL_CONFIG_PATH"] = File.join(base, "local.yml")
101
+ describe "#default_of" do
102
+ it "returns the default when a local file has been added" do
103
+ cfg = get_config("config.yml", "local.yml").fetch
88
104
 
89
- # Passing nil to the prefix on purpose (see SUSE/Portus#1379)
90
- cfg = ::CConfig::Config.new(default: "config.yml", local: local, prefix: nil)
91
- expect { cfg.fetch }.not_to raise_error
105
+ expect(cfg["ldap"]["hostname"]).to eq("ldap.example.com")
106
+ expect(cfg.default_of("ldap.hostname")).to eq("ldap_hostname")
107
+ end
108
+
109
+ it "returns the default even if an environment variable was set" do
110
+ ENV["TEST_LDAP_AUTHENTICATION_BIND_DN"] = "2"
111
+ cfg = get_config("config.yml", "local.yml").fetch
112
+
113
+ expect(cfg["ldap"]["authentication"]["bind_dn"]).to eq(2)
114
+ expect(cfg.default_of("ldap.authentication.bind_dn")).to eq("")
115
+ end
116
+
117
+ it "returns nil for an unknown key" do
118
+ cfg = get_config("config.yml", "local.yml").fetch
119
+ expect(cfg.default_of("something.that.does.not.exist")).to be_nil
120
+ end
92
121
  end
93
122
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cconfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mssola
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-11 00:00:00.000000000 Z
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: safe_yaml
@@ -155,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
155
  version: '0'
156
156
  requirements: []
157
157
  rubyforge_project:
158
- rubygems_version: 2.4.8
158
+ rubygems_version: 2.6.12
159
159
  signing_key:
160
160
  specification_version: 4
161
161
  summary: Configuration management for container-aware applications.