lite_config 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Gabe da Silveira
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # LiteConfig
2
+
3
+ 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.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'lite_config'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lite_config
20
+
21
+ ## Usage
22
+
23
+ Simple loading of environment-namespaced config files with local overrides.
24
+
25
+ LiteConfig(:foobar)
26
+
27
+ loads and caches the current environment section from:
28
+
29
+ config/foobar.yml
30
+
31
+ The file must contain a yaml hash with environments for top-level keys.
32
+
33
+ Config files can be overridden by adding '_local' to the filename:
34
+
35
+ config/foobar_local.yml
36
+
37
+ Local overrides are intended for individual developer boxes and thus
38
+ should be left out of version control.
39
+
40
+
41
+ ## TODO
42
+
43
+ * Convert hash to OpenStruct for method-based access
44
+ * Raise errors for undefined config keys
45
+ * Support TOML or other config formats
46
+ * Allow configuration of source directory
47
+ * Allow multiple source directories
48
+
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new :test do |t|
6
+ t.test_files = FileList['spec/*_spec.rb']
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ module LiteConfig
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,88 @@
1
+ require "lite_config/version"
2
+
3
+ require 'active_support/core_ext/hash/deep_merge'
4
+ require 'active_support/core_ext/hash/indifferent_access'
5
+
6
+ require 'yaml'
7
+
8
+ module LiteConfig
9
+ class ImmutableError < StandardError; end
10
+
11
+ extend self
12
+
13
+ def fetch(name)
14
+ name = name.to_sym
15
+ @configs ||= {}
16
+ @configs.key?(name) ? @configs[name] : (@configs[name] = HashWithIndifferentAccess.new(load(name)))
17
+ end
18
+
19
+ def set_config_path(path)
20
+ raise ImmutableError, "config_path is frozen after the first file load" unless @configs.nil?
21
+
22
+ @config_path = path
23
+ end
24
+
25
+ def reset
26
+ @configs = nil
27
+ end
28
+
29
+ private
30
+
31
+ def load(name)
32
+ config = load_single(config_filename(name))
33
+
34
+ if File.exist?(local_config_filename(name))
35
+ config.deep_merge!(load_single(local_config_filename(name)))
36
+ end
37
+
38
+ raise "Oops, no #{app_env} config found for #{name} in #{filename}" unless config
39
+
40
+ config
41
+ end
42
+
43
+ def load_single(filename)
44
+ hash = YAML.load_file(filename)
45
+
46
+ has_environmenty_key?(hash) ? hash[app_env] : hash
47
+ end
48
+
49
+ def config_path
50
+ @config_path ||= File.join(app_root, 'config')
51
+ end
52
+
53
+ def config_filename(name)
54
+ File.join(config_path, name.to_s + '.yml')
55
+ end
56
+
57
+ def local_config_filename(name)
58
+ config_filename(name).gsub(/.yml$/, '_local.yml')
59
+ end
60
+
61
+ def active_config_filename(name)
62
+ end
63
+
64
+ def app_root
65
+ defined?(Rails) ? Rails.root : `pwd`.strip
66
+ end
67
+
68
+ def app_env
69
+ @app_env ||=
70
+ if defined?(Rails)
71
+ Rails.env
72
+ elsif ENV['RAILS_ENV']
73
+ ENV['RAILS_ENV']
74
+ elsif ENV['RACK_ENV']
75
+ ENV['RACK_ENV']
76
+ else
77
+ 'development'
78
+ end
79
+ end
80
+
81
+ def has_environmenty_key?(hash)
82
+ %w(development test production).any?{ |envy| hash.key?(envy) }
83
+ end
84
+ end
85
+
86
+ def LiteConfig(name)
87
+ LiteConfig.fetch(name)
88
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lite_config/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "lite_config"
8
+ spec.version = LiteConfig::VERSION
9
+ spec.authors = ["Gabe da Silveira"]
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}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest", ">= 5.0.0"
26
+ spec.add_development_dependency "debugger"
27
+ end
@@ -0,0 +1,4 @@
1
+ development:
2
+ option: dev
3
+ test:
4
+ option: test
@@ -0,0 +1,4 @@
1
+ one: fish
2
+ two: fish
3
+ red: fish
4
+ blue: fish
@@ -0,0 +1,19 @@
1
+ development:
2
+ top_level:
3
+ deeper:
4
+ and_deeper: qux
5
+ stuff: foobar
6
+ an_array:
7
+ - one
8
+ - two
9
+ - three
10
+ array_of_hashish:
11
+ - fu: schnickens
12
+ wu: tang
13
+ - br: and
14
+ nu: bians
15
+ test:
16
+ top_level:
17
+ deeper:
18
+ and_deeper: testqux
19
+ stuff: testfoobar
@@ -0,0 +1,6 @@
1
+ development:
2
+ this: is a string
3
+ that: is another string
4
+ inner:
5
+ one: bee
6
+ two: baw
@@ -0,0 +1,6 @@
1
+ development:
2
+ this: overrides the first string
3
+ other: is yet one more
4
+ inner:
5
+ one: baz
6
+ three: boo
@@ -0,0 +1,88 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe LiteConfig do
4
+ before do
5
+ LiteConfig.reset
6
+ end
7
+
8
+ describe "set_config_path" do
9
+ it "should fail after the first load" do
10
+ LiteConfig(:basic)
11
+
12
+ proc { LiteConfig.set_config_path('foo') }.must_raise LiteConfig::ImmutableError
13
+ end
14
+ end
15
+
16
+ describe "basic config" do
17
+ before do
18
+ @config = LiteConfig(:basic)
19
+ end
20
+
21
+ it "should default to development enviroment" do
22
+ @config['option'].must_equal 'dev'
23
+ end
24
+
25
+ it "should allow indifferent access" do
26
+ @config[:option].must_equal 'dev'
27
+ end
28
+ end
29
+
30
+ describe "nested config" do
31
+ before do
32
+ @config = LiteConfig(:nested)
33
+ end
34
+
35
+ it "should find nested keys" do
36
+ @config['top_level']['deeper']['and_deeper'].must_equal 'qux'
37
+ end
38
+
39
+ it "should allow indifferent access" do
40
+ @config[:top_level][:deeper][:and_deeper].must_equal 'qux'
41
+ end
42
+
43
+ it "should allow arrays" do
44
+ @config[:top_level][:an_array][0].must_equal 'one'
45
+ @config[:top_level][:an_array][1].must_equal 'two'
46
+ @config[:top_level][:an_array][2].must_equal 'three'
47
+ end
48
+
49
+ it "should allow hashes in arrays" do
50
+ @config['top_level']['array_of_hashish'][0]['fu'].must_equal 'schnickens'
51
+ @config['top_level']['array_of_hashish'][0]['wu'].must_equal 'tang'
52
+ end
53
+
54
+ it "should allow hashes in arrays indifferently" do
55
+ @config[:top_level][:array_of_hashish][0][:fu].must_equal 'schnickens'
56
+ @config[:top_level][:array_of_hashish][0][:wu].must_equal 'tang'
57
+ end
58
+ end
59
+
60
+ describe "local_override config" do
61
+ before do
62
+ @config = LiteConfig(:override)
63
+ end
64
+
65
+ it "should respect override" do
66
+ @config[:this].must_equal "overrides the first string"
67
+ end
68
+
69
+ it "should merge override params" do
70
+ @config[:that].must_equal "is another string"
71
+ end
72
+
73
+ it "should merge deep override params" do
74
+ @config[:inner][:one].must_equal "baz"
75
+ @config[:inner][:two].must_equal "baw"
76
+ end
77
+ end
78
+
79
+ describe "environmentless config" do
80
+ before do
81
+ @config = LiteConfig(:environmentless)
82
+ end
83
+
84
+ it "should use root level as config if keys do not look environmenty" do
85
+ @config[:red].must_equal 'fish'
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,7 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'debugger'
4
+
5
+ require 'lite_config'
6
+
7
+ LiteConfig.set_config_path(File.expand_path(__FILE__ + '/../fixtures'))
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lite_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gabe da Silveira
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: minitest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 5.0.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 5.0.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: debugger
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
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.
96
+ email:
97
+ - gabe@websaviour.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - lib/lite_config.rb
108
+ - lib/lite_config/version.rb
109
+ - lite_config.gemspec
110
+ - spec/fixtures/basic.yml
111
+ - spec/fixtures/environmentless.yml
112
+ - spec/fixtures/nested.yml
113
+ - spec/fixtures/override.yml
114
+ - spec/fixtures/override_local.yml
115
+ - spec/lite_config_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: ''
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.23
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: Lightweight configuration for your app
142
+ test_files:
143
+ - spec/fixtures/basic.yml
144
+ - spec/fixtures/environmentless.yml
145
+ - spec/fixtures/nested.yml
146
+ - spec/fixtures/override.yml
147
+ - spec/fixtures/override_local.yml
148
+ - spec/lite_config_spec.rb
149
+ - spec/spec_helper.rb
150
+ has_rdoc: