configs 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +47 -0
- data/Rakefile +2 -0
- data/configs.gemspec +20 -0
- data/lib/configs.rb +57 -0
- data/lib/configs/railtie.rb +8 -0
- data/lib/configs/version.rb +3 -0
- data/test/configs_test.rb +51 -0
- data/test/test_helper.rb +17 -0
- metadata +81 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kickstarter
|
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,47 @@
|
|
1
|
+
# Configs
|
2
|
+
|
3
|
+
Loads and manages config/*.yml files.
|
4
|
+
|
5
|
+
Searches through a few locations to find the right environment config:
|
6
|
+
|
7
|
+
1. config/$name/$env.yml
|
8
|
+
2. config/$name.yml (with $env key)
|
9
|
+
3. config/$name/default.yml
|
10
|
+
3. config/$name.yml (with 'default' key)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'configs'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install configs
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
If you have a `config/foo.yml`, then anywhere you need to read the file
|
29
|
+
you can use `Configs[:foo]` as a hash.
|
30
|
+
|
31
|
+
Example:
|
32
|
+
|
33
|
+
# config/foo.yml
|
34
|
+
development:
|
35
|
+
hello: world
|
36
|
+
|
37
|
+
# Elsewhere (even in a config/initializer)
|
38
|
+
Configs[:foo][:hello]
|
39
|
+
=> 'world'
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/configs.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/configs/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Lance Ivy"]
|
6
|
+
gem.email = ["lance@cainlevy.net"]
|
7
|
+
gem.description = "Easy (easier?) management of config/*.yml files. Defines a lookup priority for the current environment's settings."
|
8
|
+
gem.summary = "Easy (easier?) management of config/*.yml files."
|
9
|
+
gem.homepage = "http://github.com/kickstarter/configs"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "configs"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Configs::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'activesupport', '>3.0'
|
19
|
+
gem.add_development_dependency "test-unit"
|
20
|
+
end
|
data/lib/configs.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "configs/version"
|
2
|
+
require "configs/railtie" if defined? Rails
|
3
|
+
|
4
|
+
module Configs
|
5
|
+
class NotFound < StandardError; end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
# Where the wild .yml live.
|
10
|
+
# In a Rails app, this is Rails.root.join('config')
|
11
|
+
attr_accessor :config_dir
|
12
|
+
|
13
|
+
# The name of our environment.
|
14
|
+
# In a Rails app, this is Rails.env
|
15
|
+
attr_accessor :environment
|
16
|
+
|
17
|
+
# will find (and memoize) the yml config file with this name
|
18
|
+
#
|
19
|
+
# cascades through a loading order to find the most specific yml file available (see Configs.load)
|
20
|
+
#
|
21
|
+
# if none can be found, it will raise an error
|
22
|
+
def [](name)
|
23
|
+
@_configs ||= {}
|
24
|
+
@_configs[name.to_sym] ||= load(name).symbolize_keys
|
25
|
+
end
|
26
|
+
|
27
|
+
def inspect
|
28
|
+
@_configs.inspect
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
# checks loading order for named yml file
|
34
|
+
#
|
35
|
+
# 1) `config/$NAME/$ENV.yml'
|
36
|
+
# 2) `config/$NAME.yml' with $ENV key
|
37
|
+
# 3) `config/$NAME.yml' with 'default' key
|
38
|
+
def load(name)
|
39
|
+
yml_file("#{name}/#{environment}") ||
|
40
|
+
yml_file_with_key("#{name}", environment) ||
|
41
|
+
yml_file("#{name}/default") ||
|
42
|
+
yml_file_with_key("#{name}", 'default') ||
|
43
|
+
raise(NotFound)
|
44
|
+
end
|
45
|
+
|
46
|
+
def yml_file(name)
|
47
|
+
path = config_dir.join(name + '.yml')
|
48
|
+
YAML.load_file path if File.exists? path
|
49
|
+
end
|
50
|
+
|
51
|
+
def yml_file_with_key(path, key)
|
52
|
+
hash = yml_file(path)
|
53
|
+
hash && hash[key]
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class ConfigsTest < Configs::TestCase
|
4
|
+
|
5
|
+
should "find config/foo/test.yml" do
|
6
|
+
with_config('foo/test.yml', :hello => 'world') do
|
7
|
+
assert_equal 'world', Configs[:foo][:hello]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
should "find config/foo.yml with test key" do
|
12
|
+
with_config('foo.yml', :test => {:hello => 'world'}) do
|
13
|
+
assert_equal 'world', Configs[:foo][:hello]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
should "find config/foo/default.yml" do
|
18
|
+
with_config('foo/default.yml', :hello => 'world') do
|
19
|
+
assert_equal 'world', Configs[:foo][:hello]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
should "find config/foo.yml with 'default' key" do
|
24
|
+
with_config('foo.yml', :default => {:hello => 'world'}) do
|
25
|
+
assert_equal 'world', Configs[:foo][:hello]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
should "not find missing config" do
|
30
|
+
assert_raises(Configs::NotFound) { Configs[:unknown] }
|
31
|
+
end
|
32
|
+
|
33
|
+
should "symbolize keys" do
|
34
|
+
with_config('foo.yml', :test => {'hello' => 'world'}) do
|
35
|
+
assert_equal 'world', Configs[:foo][:hello]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
protected
|
40
|
+
|
41
|
+
def with_config(path, contents, &block)
|
42
|
+
path = Configs.config_dir.join(path).to_s
|
43
|
+
begin
|
44
|
+
FileUtils.mkdir_p(File.dirname(path))
|
45
|
+
File.open(path, 'w') { |f| f << contents.to_yaml }
|
46
|
+
yield
|
47
|
+
ensure
|
48
|
+
File.delete(path)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.require(:default, :test)
|
4
|
+
|
5
|
+
require 'test/unit'
|
6
|
+
require 'active_support/core_ext/hash/keys'
|
7
|
+
|
8
|
+
class Configs::TestCase < Test::Unit::TestCase
|
9
|
+
def default_test; end # quiet Test::Unit
|
10
|
+
|
11
|
+
def self.should(name, &block) # very simple syntax
|
12
|
+
define_method("test_should_#{name.gsub(/[ -\/]/, '_').gsub(/[^a-z0-9_]/i, '')}", &block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
Configs.config_dir = Pathname.new(File.dirname(__FILE__) + '/config')
|
17
|
+
Configs.environment = 'test'
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: configs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lance Ivy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70229314323460 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>'
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70229314323460
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: test-unit
|
27
|
+
requirement: &70229314803880 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70229314803880
|
36
|
+
description: Easy (easier?) management of config/*.yml files. Defines a lookup priority
|
37
|
+
for the current environment's settings.
|
38
|
+
email:
|
39
|
+
- lance@cainlevy.net
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- LICENSE
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- configs.gemspec
|
50
|
+
- lib/configs.rb
|
51
|
+
- lib/configs/railtie.rb
|
52
|
+
- lib/configs/version.rb
|
53
|
+
- test/configs_test.rb
|
54
|
+
- test/test_helper.rb
|
55
|
+
homepage: http://github.com/kickstarter/configs
|
56
|
+
licenses: []
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 1.8.11
|
76
|
+
signing_key:
|
77
|
+
specification_version: 3
|
78
|
+
summary: Easy (easier?) management of config/*.yml files.
|
79
|
+
test_files:
|
80
|
+
- test/configs_test.rb
|
81
|
+
- test/test_helper.rb
|