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 ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
data/README.md CHANGED
@@ -1,50 +1,113 @@
1
1
  # LiteConfig
2
2
 
3
+ [![Build Status](https://travis-ci.org/gtd/lite_config.png?branch=master)](https://travis-ci.org/gtd/lite_config)
4
+ [![Code Climate](https://codeclimate.com/github/gtd/lite_config.png)](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 a naming convention for overriding
5
- the official config file without source code changes.
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
- Add this line to your application's Gemfile:
13
+ It's just a regular gem, add with the usual Gemfile declaration:
10
14
 
11
15
  gem 'lite_config'
12
16
 
13
- And then execute:
17
+ Alternatively if you aren't using Bundler then you probably don't need me telling you what to do.
18
+
14
19
 
15
- $ bundle
20
+ ## Usage
16
21
 
17
- Or install it yourself as:
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
- $ gem install lite_config
25
+ development:
26
+ stuff: Things
27
+ test:
28
+ stuff: Test Things
20
29
 
21
- ## Usage
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
- Simple loading of environment-namespaced config files with local overrides.
83
+ LiteConfig automatically picks this up based on the naming convention and merges the values into main config file hash:
24
84
 
25
- LiteConfig(:foobar)
85
+ LiteConfig(:geoip)[:data_file]
86
+ => "/Users/apple-fanboy/Documents/geoip.dat"
26
87
 
27
- loads and caches the current environment section from:
88
+ LiteConfig(:geoip)[:foo]
89
+ => "bar"
28
90
 
29
- config/foobar.yml
91
+ The naming convention also allows you to add the following to your `.gitignore`:
30
92
 
31
- The file must contain a yaml hash with environments for top-level keys.
93
+ config/*_local.yml
32
94
 
33
- Config files can be overridden by adding '_local' to the filename:
95
+ #### LiteConfig Settings
34
96
 
35
- config/foobar_local.yml
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
- Local overrides are intended for individual developer boxes and thus
38
- should be left out of version control.
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
- ## TODO
104
+ ## Future Ideas
42
105
 
43
- * Convert hash to OpenStruct for method-based access
44
- * Raise errors for undefined config keys
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 configuration of source directory
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
@@ -1,3 +1,3 @@
1
1
  module LiteConfig
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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 set_config_path(path)
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
- config.deep_merge!(load_single(local_config_filename(name)))
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. Supports open_struct style access, detects environments by default, for Rails, Rack or any other 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
 
@@ -0,0 +1,4 @@
1
+ development:
2
+ option: again empty
3
+ test:
4
+ option: again test_empty
@@ -0,0 +1,2 @@
1
+ development:
2
+ test:
@@ -0,0 +1,4 @@
1
+ development:
2
+ option: empty
3
+ test:
4
+ option: test_empty
File without changes
@@ -0,0 +1,4 @@
1
+ development:
2
+ option: yet again empty
3
+ test:
4
+ option: yet again test_empty
@@ -0,0 +1,2 @@
1
+ test:
2
+ option: only test option here
@@ -5,11 +5,19 @@ describe LiteConfig do
5
5
  LiteConfig.reset
6
6
  end
7
7
 
8
- describe "set_config_path" do
8
+ describe "config_path=" do
9
9
  it "should fail after the first load" do
10
10
  LiteConfig(:basic)
11
11
 
12
- proc { LiteConfig.set_config_path('foo') }.must_raise LiteConfig::ImmutableError
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
@@ -4,4 +4,5 @@ require 'debugger'
4
4
 
5
5
  require 'lite_config'
6
6
 
7
- LiteConfig.set_config_path(File.expand_path(__FILE__ + '/../fixtures'))
7
+ LiteConfig.config_path = File.expand_path(__FILE__ + '/../fixtures')
8
+ LiteConfig.app_env = 'development'
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.1
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-17 00:00:00.000000000 Z
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. Supports open_struct style access,
95
- detects environments by default, for Rails, Rack or any other app.
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