rock_config 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +54 -0
- data/Rakefile +2 -0
- data/lib/rock_config/configuration.rb +10 -0
- data/lib/rock_config/environment_detector.rb +11 -0
- data/lib/rock_config/manager.rb +26 -0
- data/lib/rock_config/scanner.rb +26 -0
- data/lib/rock_config/version.rb +3 -0
- data/lib/rock_config.rb +38 -0
- data/rock_config.gemspec +21 -0
- data/spec/configuration_spec.rb +17 -0
- data/spec/environment_detector_spec.rb +20 -0
- data/spec/fixtures/database.yml +8 -0
- data/spec/manager_spec.rb +36 -0
- data/spec/scanner_spec.rb +23 -0
- data/spec/spec_helper.rb +9 -0
- metadata +119 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jiri Pospisil
|
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,54 @@
|
|
1
|
+
# RockConfig
|
2
|
+
|
3
|
+
RockConfig is a simple convention based library for reading and accessing config files.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem "rock_config"
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rock_config
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Given the file elastic_search.yml:
|
22
|
+
|
23
|
+
development:
|
24
|
+
host: 127.0.0.1
|
25
|
+
port: 9200
|
26
|
+
|
27
|
+
production:
|
28
|
+
host: 192.168.1.10
|
29
|
+
port: 9200
|
30
|
+
|
31
|
+
RockConfig allows you to read these settings with an API like this:
|
32
|
+
|
33
|
+
elastic_config = RockConfig.for :elastic_search
|
34
|
+
elastic_config.host # > 127.0.0.1
|
35
|
+
|
36
|
+
RockConfig automatically chooses the current application environment. You select the environment yourself
|
37
|
+
with:
|
38
|
+
|
39
|
+
RockConfig.for :elastic_search, :production
|
40
|
+
|
41
|
+
RockConfig scans predefined directories for config files. By default, it tries to scan directory
|
42
|
+
`config` in the project root. You can add more directories:
|
43
|
+
|
44
|
+
RockConfig.configure do |config|
|
45
|
+
config.scanned_directories << "custom_path"
|
46
|
+
end
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "rock_config/scanner"
|
2
|
+
|
3
|
+
module RockConfig
|
4
|
+
class Manager
|
5
|
+
def initialize(configuration, scanner = Scanner)
|
6
|
+
@scanner = scanner.new(configuration)
|
7
|
+
@configs = initialize_configs
|
8
|
+
end
|
9
|
+
|
10
|
+
def fetch(config_name, environment)
|
11
|
+
if config = @configs[config_name]
|
12
|
+
config.for_environment environment
|
13
|
+
else
|
14
|
+
yield if block_given?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize_configs
|
19
|
+
Hash.new do |hash, key|
|
20
|
+
if config = @scanner.find(key)
|
21
|
+
hash[key] = config
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RockConfig
|
2
|
+
class Scanner
|
3
|
+
def initialize(configuration)
|
4
|
+
@configuration = configuration
|
5
|
+
end
|
6
|
+
|
7
|
+
def find(file_name)
|
8
|
+
full_file_name = resolve_full_file_name(file_name)
|
9
|
+
|
10
|
+
@configuration.scanned_directories.each do |directory|
|
11
|
+
file = File.join(directory, full_file_name)
|
12
|
+
if File.exists? file
|
13
|
+
return File.read file
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def resolve_full_file_name(file_name)
|
23
|
+
"#{file_name}.yml"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/rock_config.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "rock_config/version"
|
2
|
+
require "rock_config/environment_detector"
|
3
|
+
require "rock_config/configuration"
|
4
|
+
require "rock_config/manager"
|
5
|
+
|
6
|
+
module RockConfig
|
7
|
+
class ConfigNotFound < Exception; end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def for(config_name, environment = detect_environment)
|
11
|
+
manager.fetch(config_name, environment) do
|
12
|
+
raise ConfigNotFound, "Config not found"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure
|
17
|
+
yield configuration if block_given?
|
18
|
+
@manager = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def detect_environment
|
24
|
+
@detected_environment ||= lambda do
|
25
|
+
detector = EnvironmentDetector.new(ENV)
|
26
|
+
detector.detect
|
27
|
+
end.call
|
28
|
+
end
|
29
|
+
|
30
|
+
def configuration
|
31
|
+
@configuration ||= Configuration.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def manager
|
35
|
+
@manager ||= Manager.new(configuration)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/rock_config.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rock_config/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Jiri Pospisil"]
|
6
|
+
gem.email = ["mekishizufu@gmail.com"]
|
7
|
+
gem.summary = %q{RockConfig allows you to use custom config files easily.}
|
8
|
+
gem.description = %q{RockConfig allows you to use custom config files easily.}
|
9
|
+
gem.homepage = ""
|
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 = "rock_config"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RockConfig::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec", "~> 2.11.0"
|
19
|
+
gem.add_development_dependency "pry", "~> 0.9.10"
|
20
|
+
gem.add_development_dependency "pry-nav", "~> 0.2.2"
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module RockConfig
|
4
|
+
describe Configuration do
|
5
|
+
it "has default scanned directory" do
|
6
|
+
configuration = Configuration.new
|
7
|
+
configuration.scanned_directories.count.should eq 1
|
8
|
+
end
|
9
|
+
|
10
|
+
it "accepts scanned directories" do
|
11
|
+
configuration = Configuration.new
|
12
|
+
|
13
|
+
configuration.scanned_directories << "/custom/path"
|
14
|
+
configuration.scanned_directories.should include "/custom/path"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module RockConfig
|
4
|
+
describe EnvironmentDetector do
|
5
|
+
it "detects rails' environment" do
|
6
|
+
detector = EnvironmentDetector.new("RAILS_ENV" => "production")
|
7
|
+
detector.detect.should eq("production")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "detects rack's environment" do
|
11
|
+
detector = EnvironmentDetector.new("RACK_ENV" => "production")
|
12
|
+
detector.detect.should eq("production")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "fallbacks to development environment if no other specified" do
|
16
|
+
detector = EnvironmentDetector.new({})
|
17
|
+
detector.detect.should eq("development")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module RockConfig
|
4
|
+
describe Manager do
|
5
|
+
before do
|
6
|
+
@configuration = Configuration.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns config if found" do
|
10
|
+
result = double("Config")
|
11
|
+
result.should_receive(:for_environment).with("development").and_return("yay")
|
12
|
+
|
13
|
+
scanner = double("Scanner")
|
14
|
+
scanner.should_receive(:new).with(@configuration).and_return(scanner)
|
15
|
+
scanner.should_receive(:find).with("sample").and_return(result)
|
16
|
+
|
17
|
+
manager = Manager.new(@configuration, scanner)
|
18
|
+
manager_result = manager.fetch "sample", "development"
|
19
|
+
|
20
|
+
manager_result.should eq("yay")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "yields if config not found" do
|
24
|
+
scanner = double("Scanner")
|
25
|
+
scanner.should_receive(:new).with(@configuration).and_return(scanner)
|
26
|
+
scanner.should_receive(:find).with("sample").and_return(nil)
|
27
|
+
|
28
|
+
manager = Manager.new(@configuration, scanner)
|
29
|
+
lambda do
|
30
|
+
manager_result = manager.fetch "sample", "development" do
|
31
|
+
raise Exception
|
32
|
+
end
|
33
|
+
end.should raise_error(Exception)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module RockConfig
|
4
|
+
describe Scanner do
|
5
|
+
it "finds nothing when the target doesnt exist" do
|
6
|
+
configuration = Configuration.new
|
7
|
+
scanner = Scanner.new(configuration)
|
8
|
+
|
9
|
+
scanner.find("database").should be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "scans additional directories" do
|
13
|
+
fixtures_path = File.join(Dir.pwd, "spec", "fixtures")
|
14
|
+
|
15
|
+
configuration = Configuration.new
|
16
|
+
configuration.scanned_directories << fixtures_path
|
17
|
+
|
18
|
+
scanner = Scanner.new(configuration)
|
19
|
+
|
20
|
+
scanner.find("database").should_not be_nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rock_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jiri Pospisil
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.11.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.11.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: pry
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.9.10
|
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: 0.9.10
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry-nav
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.2.2
|
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.2.2
|
62
|
+
description: RockConfig allows you to use custom config files easily.
|
63
|
+
email:
|
64
|
+
- mekishizufu@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/rock_config.rb
|
76
|
+
- lib/rock_config/configuration.rb
|
77
|
+
- lib/rock_config/environment_detector.rb
|
78
|
+
- lib/rock_config/manager.rb
|
79
|
+
- lib/rock_config/scanner.rb
|
80
|
+
- lib/rock_config/version.rb
|
81
|
+
- rock_config.gemspec
|
82
|
+
- spec/configuration_spec.rb
|
83
|
+
- spec/environment_detector_spec.rb
|
84
|
+
- spec/fixtures/database.yml
|
85
|
+
- spec/manager_spec.rb
|
86
|
+
- spec/scanner_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses: []
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 1.8.23
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: RockConfig allows you to use custom config files easily.
|
112
|
+
test_files:
|
113
|
+
- spec/configuration_spec.rb
|
114
|
+
- spec/environment_detector_spec.rb
|
115
|
+
- spec/fixtures/database.yml
|
116
|
+
- spec/manager_spec.rb
|
117
|
+
- spec/scanner_spec.rb
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
has_rdoc:
|