lite_config 0.0.1 → 0.0.2
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.
- data/.travis.yml +6 -0
- data/README.md +85 -22
- data/lib/lite_config/version.rb +1 -1
- data/lib/lite_config.rb +11 -6
- data/lite_config.gemspec +2 -2
- data/spec/fixtures/empty_environment_override.yml +4 -0
- data/spec/fixtures/empty_environment_override_local.yml +2 -0
- data/spec/fixtures/empty_override.yml +4 -0
- data/spec/fixtures/empty_override_local.yml +0 -0
- data/spec/fixtures/missing_environment_override.yml +4 -0
- data/spec/fixtures/missing_environment_override_local.yml +2 -0
- data/spec/lite_config_spec.rb +40 -2
- data/spec/spec_helper.rb +2 -1
- metadata +19 -5
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,50 +1,113 @@
|
|
1
1
|
# LiteConfig
|
2
2
|
|
3
|
+
[](https://travis-ci.org/gtd/lite_config)
|
4
|
+
[](https://codeclimate.com/github/gtd/lite_config)
|
5
|
+
|
3
6
|
LiteConfig automates the loading of Rails-style YAML config files. With a single line the config file is lazily loaded
|
4
|
-
and cached, automatically choosing the current environment's hash. It also supports
|
5
|
-
|
7
|
+
and cached, automatically choosing the current environment's hash. It also supports local developer override via
|
8
|
+
a simple, easily git-ignoreable naming convention.
|
9
|
+
|
6
10
|
|
7
11
|
## Installation
|
8
12
|
|
9
|
-
|
13
|
+
It's just a regular gem, add with the usual Gemfile declaration:
|
10
14
|
|
11
15
|
gem 'lite_config'
|
12
16
|
|
13
|
-
|
17
|
+
Alternatively if you aren't using Bundler then you probably don't need me telling you what to do.
|
18
|
+
|
14
19
|
|
15
|
-
|
20
|
+
## Usage
|
16
21
|
|
17
|
-
|
22
|
+
By default LiteConfig follows the Rails convention for config file directory. For instance you might have a file
|
23
|
+
`config/options.yml`:
|
18
24
|
|
19
|
-
|
25
|
+
development:
|
26
|
+
stuff: Things
|
27
|
+
test:
|
28
|
+
stuff: Test Things
|
20
29
|
|
21
|
-
|
30
|
+
You can access the sub-hash of the current environment with a simple method call:
|
31
|
+
|
32
|
+
LiteConfig(:options)
|
33
|
+
=> {"stuff"=>"Things"}
|
34
|
+
|
35
|
+
Note that it is a HashWithIndifferentAccess:
|
36
|
+
|
37
|
+
LiteConfig(:options)[:stuff]
|
38
|
+
=> "Things"
|
39
|
+
|
40
|
+
It's worth mentioning that this applies to nested hashes as well, so given `config/options.yml`:
|
41
|
+
|
42
|
+
development:
|
43
|
+
outer:
|
44
|
+
inner: Stuff
|
45
|
+
|
46
|
+
you can do:
|
47
|
+
|
48
|
+
LiteConfig(:options)[:outer][:inner]
|
49
|
+
=> "Stuff"
|
50
|
+
|
51
|
+
Of course any valid YAML will work, but it works particularly nicely with simple hashes and arrays.
|
52
|
+
|
53
|
+
#### Flat Config Files
|
54
|
+
|
55
|
+
Do you ever cringe when your config file uses advanced YAML features do duplicate the exact same hash for every
|
56
|
+
environment? With LiteConfig you can just skip the environment keys at the top-level entirely if you want to. So for
|
57
|
+
instance a config file `config/options.yml` containing only:
|
58
|
+
|
59
|
+
root: This is tops
|
60
|
+
|
61
|
+
can be used as such:
|
62
|
+
|
63
|
+
LiteConfig(:options)[:root]
|
64
|
+
=> "This is tops"
|
65
|
+
|
66
|
+
In case you're wondering, this is based on a simple heuristic by checking for the presence of a `test`, `development`
|
67
|
+
or `production` at the root level, so if those are the keys for your config values than I'm sorry but you'll need to go
|
68
|
+
back to your byzantine YAML ampersand syntax hacks.
|
69
|
+
|
70
|
+
#### Local Overrides
|
71
|
+
|
72
|
+
Sometimes on your team individual developers have local configuration that you don't want in version control. Consider
|
73
|
+
`config/geoip.yml`:
|
74
|
+
|
75
|
+
foo: bar
|
76
|
+
data_file: /usr/share/geoip.dat
|
77
|
+
|
78
|
+
But developers may not all organize their systems exactly the same way, so you create a local config named
|
79
|
+
`config/geoip_local.yml`:
|
80
|
+
|
81
|
+
data_file: /Users/apple-fanboy/Documents/geoip.dat
|
22
82
|
|
23
|
-
|
83
|
+
LiteConfig automatically picks this up based on the naming convention and merges the values into main config file hash:
|
24
84
|
|
25
|
-
LiteConfig(:
|
85
|
+
LiteConfig(:geoip)[:data_file]
|
86
|
+
=> "/Users/apple-fanboy/Documents/geoip.dat"
|
26
87
|
|
27
|
-
|
88
|
+
LiteConfig(:geoip)[:foo]
|
89
|
+
=> "bar"
|
28
90
|
|
29
|
-
|
91
|
+
The naming convention also allows you to add the following to your `.gitignore`:
|
30
92
|
|
31
|
-
|
93
|
+
config/*_local.yml
|
32
94
|
|
33
|
-
|
95
|
+
#### LiteConfig Settings
|
34
96
|
|
35
|
-
|
97
|
+
LiteConfig is meant to work out of the box with Rails, but it also is configurable. Note that these settings can not be
|
98
|
+
changed after the first config file is loaded as that could lead to the wrong data being loaded and cached.
|
36
99
|
|
37
|
-
|
38
|
-
|
100
|
+
LiteConfig.config_path = '/usr/local/global_app_config' # Full path to the directory files should be loaded from
|
101
|
+
LiteConfig.app_env = 'production' # Explicitly override RAILS_ENV, RACK_ENV defaults
|
39
102
|
|
40
103
|
|
41
|
-
##
|
104
|
+
## Future Ideas
|
42
105
|
|
43
|
-
* Convert
|
44
|
-
*
|
106
|
+
* Convert LiteConfig to an insantiable class instead of a singleton
|
107
|
+
* Remove ActiveSupport dependency
|
108
|
+
* Ability to raise errors for undefined config keys
|
45
109
|
* Support TOML or other config formats
|
46
|
-
* Allow
|
47
|
-
* Allow multiple source directories
|
110
|
+
* Allow OpenStruct-like access to the config. I'm not 100% sure on the ROI of this feature.
|
48
111
|
|
49
112
|
|
50
113
|
## Contributing
|
data/lib/lite_config/version.rb
CHANGED
data/lib/lite_config.rb
CHANGED
@@ -16,12 +16,18 @@ module LiteConfig
|
|
16
16
|
@configs.key?(name) ? @configs[name] : (@configs[name] = HashWithIndifferentAccess.new(load(name)))
|
17
17
|
end
|
18
18
|
|
19
|
-
def
|
19
|
+
def config_path=(path)
|
20
20
|
raise ImmutableError, "config_path is frozen after the first file load" unless @configs.nil?
|
21
21
|
|
22
22
|
@config_path = path
|
23
23
|
end
|
24
24
|
|
25
|
+
def app_env=(app_env)
|
26
|
+
raise ImmutableError, "app_env is frozen after the first file load" unless @configs.nil?
|
27
|
+
|
28
|
+
@app_env = app_env
|
29
|
+
end
|
30
|
+
|
25
31
|
def reset
|
26
32
|
@configs = nil
|
27
33
|
end
|
@@ -32,7 +38,9 @@ module LiteConfig
|
|
32
38
|
config = load_single(config_filename(name))
|
33
39
|
|
34
40
|
if File.exist?(local_config_filename(name))
|
35
|
-
|
41
|
+
local_config = load_single(local_config_filename(name))
|
42
|
+
|
43
|
+
config.deep_merge!(local_config) if local_config
|
36
44
|
end
|
37
45
|
|
38
46
|
raise "Oops, no #{app_env} config found for #{name} in #{filename}" unless config
|
@@ -58,9 +66,6 @@ module LiteConfig
|
|
58
66
|
config_filename(name).gsub(/.yml$/, '_local.yml')
|
59
67
|
end
|
60
68
|
|
61
|
-
def active_config_filename(name)
|
62
|
-
end
|
63
|
-
|
64
69
|
def app_root
|
65
70
|
defined?(Rails) ? Rails.root : `pwd`.strip
|
66
71
|
end
|
@@ -79,7 +84,7 @@ module LiteConfig
|
|
79
84
|
end
|
80
85
|
|
81
86
|
def has_environmenty_key?(hash)
|
82
|
-
%w(development test production).any?{ |envy| hash.key?(envy) }
|
87
|
+
%w(development test production).any?{ |envy| hash.key?(envy) } if hash
|
83
88
|
end
|
84
89
|
end
|
85
90
|
|
data/lite_config.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = LiteConfig::VERSION
|
9
9
|
spec.authors = ["Gabe da Silveira"]
|
10
10
|
spec.email = ["gabe@websaviour.com"]
|
11
|
-
spec.description = %q{Lightweight configuration for your app.
|
12
|
-
spec.summary = %q{Lightweight configuration for your app}
|
11
|
+
spec.description = %q{Lightweight configuration for your ruby app. Reads Rails-style YAML files from your config directory. Features include optional environment namespacing, local convention-based override files, and indifferent (symbol vs string) access.}
|
12
|
+
spec.summary = %q{Lightweight configuration for your ruby app}
|
13
13
|
spec.homepage = ""
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
File without changes
|
data/spec/lite_config_spec.rb
CHANGED
@@ -5,11 +5,19 @@ describe LiteConfig do
|
|
5
5
|
LiteConfig.reset
|
6
6
|
end
|
7
7
|
|
8
|
-
describe "
|
8
|
+
describe "config_path=" do
|
9
9
|
it "should fail after the first load" do
|
10
10
|
LiteConfig(:basic)
|
11
11
|
|
12
|
-
proc { LiteConfig.
|
12
|
+
proc { LiteConfig.config_path = 'foo' }.must_raise LiteConfig::ImmutableError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "app_env=" do
|
17
|
+
it "should fail after the first load" do
|
18
|
+
LiteConfig(:basic)
|
19
|
+
|
20
|
+
proc { LiteConfig.app_env = 'foo' }.must_raise LiteConfig::ImmutableError
|
13
21
|
end
|
14
22
|
end
|
15
23
|
|
@@ -76,6 +84,36 @@ describe LiteConfig do
|
|
76
84
|
end
|
77
85
|
end
|
78
86
|
|
87
|
+
describe "empty_override config" do
|
88
|
+
before do
|
89
|
+
@config = LiteConfig(:empty_override)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should load successfully" do
|
93
|
+
@config[:option].must_equal 'empty'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe "empty_environment_override config" do
|
98
|
+
before do
|
99
|
+
@config = LiteConfig(:empty_environment_override)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should load successfully" do
|
103
|
+
@config[:option].must_equal 'again empty'
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "missing_environment_override config" do
|
108
|
+
before do
|
109
|
+
@config = LiteConfig(:missing_environment_override)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "should load successfully" do
|
113
|
+
@config[:option].must_equal 'yet again empty'
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
79
117
|
describe "environmentless config" do
|
80
118
|
before do
|
81
119
|
@config = LiteConfig(:environmentless)
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lite_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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: 2014-01-
|
12
|
+
date: 2014-01-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -91,8 +91,9 @@ dependencies:
|
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
|
-
description: Lightweight configuration for your app.
|
95
|
-
|
94
|
+
description: Lightweight configuration for your ruby app. Reads Rails-style YAML
|
95
|
+
files from your config directory. Features include optional environment namespacing,
|
96
|
+
local convention-based override files, and indifferent (symbol vs string) access.
|
96
97
|
email:
|
97
98
|
- gabe@websaviour.com
|
98
99
|
executables: []
|
@@ -100,6 +101,7 @@ extensions: []
|
|
100
101
|
extra_rdoc_files: []
|
101
102
|
files:
|
102
103
|
- .gitignore
|
104
|
+
- .travis.yml
|
103
105
|
- Gemfile
|
104
106
|
- LICENSE.txt
|
105
107
|
- README.md
|
@@ -108,7 +110,13 @@ files:
|
|
108
110
|
- lib/lite_config/version.rb
|
109
111
|
- lite_config.gemspec
|
110
112
|
- spec/fixtures/basic.yml
|
113
|
+
- spec/fixtures/empty_environment_override.yml
|
114
|
+
- spec/fixtures/empty_environment_override_local.yml
|
115
|
+
- spec/fixtures/empty_override.yml
|
116
|
+
- spec/fixtures/empty_override_local.yml
|
111
117
|
- spec/fixtures/environmentless.yml
|
118
|
+
- spec/fixtures/missing_environment_override.yml
|
119
|
+
- spec/fixtures/missing_environment_override_local.yml
|
112
120
|
- spec/fixtures/nested.yml
|
113
121
|
- spec/fixtures/override.yml
|
114
122
|
- spec/fixtures/override_local.yml
|
@@ -138,10 +146,16 @@ rubyforge_project:
|
|
138
146
|
rubygems_version: 1.8.23
|
139
147
|
signing_key:
|
140
148
|
specification_version: 3
|
141
|
-
summary: Lightweight configuration for your app
|
149
|
+
summary: Lightweight configuration for your ruby app
|
142
150
|
test_files:
|
143
151
|
- spec/fixtures/basic.yml
|
152
|
+
- spec/fixtures/empty_environment_override.yml
|
153
|
+
- spec/fixtures/empty_environment_override_local.yml
|
154
|
+
- spec/fixtures/empty_override.yml
|
155
|
+
- spec/fixtures/empty_override_local.yml
|
144
156
|
- spec/fixtures/environmentless.yml
|
157
|
+
- spec/fixtures/missing_environment_override.yml
|
158
|
+
- spec/fixtures/missing_environment_override_local.yml
|
145
159
|
- spec/fixtures/nested.yml
|
146
160
|
- spec/fixtures/override.yml
|
147
161
|
- spec/fixtures/override_local.yml
|