config_for 0.1.2 → 0.2.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +6 -2
- data/README.md +10 -2
- data/gemfiles/rails_3.gemfile +6 -2
- data/gemfiles/rails_4.gemfile +6 -2
- data/gemfiles/rails_4_1.gemfile +6 -2
- data/lib/config_for.rb +31 -38
- data/lib/config_for/config.rb +69 -0
- data/lib/config_for/integrations.rb +11 -0
- data/lib/config_for/integrations/rails.rb +3 -0
- data/lib/config_for/integrations/sinatra.rb +1 -0
- data/lib/config_for/rails.rb +4 -5
- data/lib/config_for/sinatra.rb +9 -5
- data/lib/config_for/version.rb +1 -1
- data/spec/config_for_spec.rb +33 -19
- data/spec/rails_spec.rb +1 -2
- data/spec/sinatra_spec.rb +1 -1
- data/spec/spec_helper.rb +6 -1
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f3f3b81d68611878491f67a0014bb0d1f8bc13d
|
4
|
+
data.tar.gz: 6c954ff1b4082e9b8b6c7bfa59fae50357427090
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f7bb67663da4cbd96470890c8e2e88f66ba2d5c4e3ecf413d2d59967a116ed7dd5a56030f95ddac0159e40912c7b50fabe957383e91f68ad30f58d4c6a0fdf0
|
7
|
+
data.tar.gz: 2b8738ab9ef1edd4beceff12116d13235202f837afd7d895439a6402a20bbc05e30b198c16b74fcce9fc4c56ef7b37f2f69fc0e020404bd0dc412d39992d00ca
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
|
4
4
|
## master - unreleased
|
5
|
+
### Changed
|
6
|
+
- Introduced `ConfigFor.load_config!` that raises exception when environment is missing
|
7
|
+
- Both integrations will raise exception when environment is missing
|
5
8
|
|
6
9
|
## 0.1.2 - 2014-10-29
|
7
10
|
|
data/Gemfile
CHANGED
@@ -1,9 +1,13 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in config_for.gemspec
|
4
|
-
gemspec
|
4
|
+
gemspec development_group: :test
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem 'codeclimate-test-reporter', require: false
|
8
|
+
gem 'appraisal'
|
9
|
+
end
|
5
10
|
|
6
|
-
gem 'appraisal'
|
7
11
|
|
8
12
|
platforms :mri_20, :mri_21 do
|
9
13
|
gem 'pry-byebug'
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# ConfigFor [![Build Status](https://travis-ci.org/3scale/config_for.svg?branch=master)](https://travis-ci.org/3scale/config_for)
|
1
|
+
# ConfigFor [![Build Status](https://travis-ci.org/3scale/config_for.svg?branch=master)](https://travis-ci.org/3scale/config_for) [![Code Climate](https://codeclimate.com/github/3scale/config_for/badges/gpa.svg)](https://codeclimate.com/github/3scale/config_for) [![Test Coverage](https://codeclimate.com/github/3scale/config_for/badges/coverage.svg)](https://codeclimate.com/github/3scale/config_for)
|
2
2
|
|
3
3
|
Framework for generating, uploading and loading config files in Ruby apps.
|
4
4
|
|
@@ -6,6 +6,14 @@ It offers integrations with Rails and Sinatra.
|
|
6
6
|
|
7
7
|
For generating and uploading configs it uses Capistrano task.
|
8
8
|
|
9
|
+
# How to use it?
|
10
|
+
|
11
|
+
* Check [RDoc documentation](http://www.rubydoc.info/github/3scale/config_for/master/frames)
|
12
|
+
* If you have Rails/Sinatra, just add the gem to Gemfile and use it (
|
13
|
+
[Sinatra example](http://www.rubydoc.info/github/3scale/config_for/master/ConfigFor/Sinatra:config_for),
|
14
|
+
[Rails example](http://www.rubydoc.info/github/3scale/config_for/master/ConfigFor/Rails:config_for))
|
15
|
+
* Otherwise check [ConfigFor.load_config!](http://www.rubydoc.info/github/3scale/config_for/master/ConfigFor.load_config!)
|
16
|
+
|
9
17
|
## Installation
|
10
18
|
|
11
19
|
Add this line to your application's Gemfile:
|
@@ -115,7 +123,7 @@ If you have your own framework. You need to create your own `config_for` method.
|
|
115
123
|
require 'config_for'
|
116
124
|
module MyApp
|
117
125
|
def self.config_for(name)
|
118
|
-
ConfigFor.load_config(MyApp.root.join('config'), name, MyApp.env)
|
126
|
+
ConfigFor.load_config!(MyApp.root.join('config'), name, MyApp.env)
|
119
127
|
end
|
120
128
|
end
|
121
129
|
```
|
data/gemfiles/rails_3.gemfile
CHANGED
@@ -2,11 +2,15 @@
|
|
2
2
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem "appraisal"
|
6
5
|
gem "rails", "~> 3.2.14"
|
7
6
|
|
7
|
+
group :test do
|
8
|
+
gem "codeclimate-test-reporter", :require => false
|
9
|
+
gem "appraisal"
|
10
|
+
end
|
11
|
+
|
8
12
|
platforms :mri_20, :mri_21 do
|
9
13
|
gem "pry-byebug"
|
10
14
|
end
|
11
15
|
|
12
|
-
gemspec :path => "../"
|
16
|
+
gemspec :development_group => :test, :path => "../"
|
data/gemfiles/rails_4.gemfile
CHANGED
@@ -2,11 +2,15 @@
|
|
2
2
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem "appraisal"
|
6
5
|
gem "rails", "~> 4.0.0"
|
7
6
|
|
7
|
+
group :test do
|
8
|
+
gem "codeclimate-test-reporter", :require => false
|
9
|
+
gem "appraisal"
|
10
|
+
end
|
11
|
+
|
8
12
|
platforms :mri_20, :mri_21 do
|
9
13
|
gem "pry-byebug"
|
10
14
|
end
|
11
15
|
|
12
|
-
gemspec :path => "../"
|
16
|
+
gemspec :development_group => :test, :path => "../"
|
data/gemfiles/rails_4_1.gemfile
CHANGED
@@ -2,11 +2,15 @@
|
|
2
2
|
|
3
3
|
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem "appraisal"
|
6
5
|
gem "rails", "~> 4.1.0"
|
7
6
|
|
7
|
+
group :test do
|
8
|
+
gem "codeclimate-test-reporter", :require => false
|
9
|
+
gem "appraisal"
|
10
|
+
end
|
11
|
+
|
8
12
|
platforms :mri_20, :mri_21 do
|
9
13
|
gem "pry-byebug"
|
10
14
|
end
|
11
15
|
|
12
|
-
gemspec :path => "../"
|
16
|
+
gemspec :development_group => :test, :path => "../"
|
data/lib/config_for.rb
CHANGED
@@ -1,58 +1,51 @@
|
|
1
1
|
require 'config_for/version'
|
2
|
-
require 'pathname'
|
3
|
-
|
4
|
-
require 'yaml'
|
5
|
-
require 'erb'
|
6
|
-
|
7
|
-
require 'active_support/hash_with_indifferent_access'
|
8
|
-
# HashWithIndifferentAccess needs this core extension to be loaded
|
9
|
-
require 'active_support/core_ext/hash/indifferent_access'
|
10
2
|
|
11
3
|
module ConfigFor
|
12
|
-
|
13
|
-
|
14
|
-
|
4
|
+
ReadError = Class.new(StandardError)
|
5
|
+
InvalidConfigError = Class.new(StandardError)
|
6
|
+
MissingEnvironmentError = Class.new(StandardError)
|
15
7
|
|
16
|
-
|
17
|
-
|
18
|
-
|
8
|
+
autoload :Rails, 'config_for/rails'
|
9
|
+
autoload :Sinatra, 'config_for/sinatra'
|
10
|
+
autoload :Capistrano, 'config_for/capistrano'
|
19
11
|
|
20
|
-
|
21
|
-
require 'config_for/capistrano'
|
22
|
-
end
|
12
|
+
autoload :Config, 'config_for/config'
|
23
13
|
|
24
14
|
# inspired by https://github.com/rails/rails/commit/9629dea4fb15a948ab4394590fdd946bd9dd4f91
|
25
15
|
|
26
|
-
# Loads yaml file "#{{name}}.yml" from path and gets
|
16
|
+
# Loads yaml file "#{{name}}.yml" from path and gets given environment key.
|
17
|
+
# In case the environment is not found, it returns empty hash.
|
27
18
|
#
|
28
19
|
# @param [Pathname, String] path partial of full path to folder with configs
|
29
20
|
# @param [String] name without extension
|
30
21
|
# @param [Symbol,String] env key to get from the config
|
22
|
+
# @raise [ConfigFor::InvalidConfigError] when the config is invalid YAML
|
23
|
+
# @raise [ConfigFor::ReadError] when the config can't be read
|
31
24
|
# @return [ActiveSupport::HashWithIndifferentAccess] config file for given env
|
32
|
-
# @raise [RuntimeError] when file does not exist or can't be parsed
|
33
25
|
# @example Load config in rails
|
34
26
|
# ConfigFor.load_config(Rails.root.join('config'), 'redis', Rails.env)
|
35
27
|
def self.load_config(path, name, env)
|
36
|
-
|
37
|
-
|
38
|
-
if File.readable?(yaml)
|
39
|
-
config = parse_yaml(yaml)
|
40
|
-
ActiveSupport::HashWithIndifferentAccess.new(config).fetch(env) { Hash.new }
|
41
|
-
else
|
42
|
-
raise "Could not load configuration. Can't read #{yaml}"
|
43
|
-
end
|
28
|
+
config(path, name).fetch(env) { ConfigFor::Config.empty }
|
44
29
|
end
|
45
30
|
|
46
|
-
#
|
47
|
-
|
48
|
-
#
|
49
|
-
#
|
50
|
-
# @
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
31
|
+
# Same as ConfigFor.load_config but raises exception when environment is not found.
|
32
|
+
# Note that this is the preferred way of loading configuration. Also it is used by
|
33
|
+
# Rails and Sinatra integrations.
|
34
|
+
#
|
35
|
+
# @param (see .load_config)
|
36
|
+
# @raise (see .load_config)
|
37
|
+
# @raise [ConfigFor::MissingEnvironmentError] when the config does not have the environment key
|
38
|
+
# @return (see .load_config)
|
39
|
+
def self.load_config!(path, name, env)
|
40
|
+
config(path, name).fetch(env)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
# @api private
|
46
|
+
def self.config(path, name)
|
47
|
+
ConfigFor::Config.new(path, name)
|
57
48
|
end
|
58
49
|
end
|
50
|
+
|
51
|
+
require 'config_for/integrations'
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'yaml'
|
3
|
+
require 'forwardable'
|
4
|
+
require 'monitor'
|
5
|
+
require 'pathname'
|
6
|
+
|
7
|
+
require 'active_support/hash_with_indifferent_access'
|
8
|
+
# HashWithIndifferentAccess needs this core extension to be loaded
|
9
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
10
|
+
|
11
|
+
module ConfigFor
|
12
|
+
# @api private
|
13
|
+
class Config
|
14
|
+
extend Forwardable
|
15
|
+
|
16
|
+
CONFIG_CLASS = ActiveSupport::HashWithIndifferentAccess
|
17
|
+
|
18
|
+
def initialize(path, name)
|
19
|
+
@path = path
|
20
|
+
@name = name
|
21
|
+
@monitor = Monitor.new
|
22
|
+
@pathname = Pathname("#{name}.yml").expand_path(path)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.empty
|
26
|
+
CONFIG_CLASS.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def environments
|
30
|
+
config.keys
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch(key, &block)
|
34
|
+
config.fetch(key, &block)
|
35
|
+
rescue KeyError
|
36
|
+
raise ConfigFor::MissingEnvironmentError, "#{@pathname} contains just #{environments}, not #{key}"
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def config
|
42
|
+
@monitor.synchronize do
|
43
|
+
if instance_variable_defined?(:@config)
|
44
|
+
@config
|
45
|
+
else
|
46
|
+
@config = convert(parse)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def convert(hash)
|
52
|
+
CONFIG_CLASS.new(hash)
|
53
|
+
end
|
54
|
+
|
55
|
+
def read
|
56
|
+
@pathname.read
|
57
|
+
rescue => error
|
58
|
+
raise ConfigFor::ReadError, error.message
|
59
|
+
end
|
60
|
+
|
61
|
+
def parse
|
62
|
+
content = read
|
63
|
+
erb = ::ERB.new(content).result
|
64
|
+
::YAML.load(erb, @pathname)
|
65
|
+
rescue ::Psych::SyntaxError => e
|
66
|
+
fail ConfigFor::InvalidConfigError, "YAML syntax error occurred while parsing #{content}. Error: #{e.message}"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
::Sinatra.register(::ConfigFor::Sinatra)
|
data/lib/config_for/rails.rb
CHANGED
@@ -1,19 +1,18 @@
|
|
1
|
+
require 'config_for'
|
2
|
+
|
1
3
|
module ConfigFor
|
2
4
|
module Rails
|
3
5
|
# Convenience loading of config files
|
4
6
|
#
|
5
7
|
# @param [String, Symbol] name the config file to load
|
8
|
+
# @raise (see ConfigFor.load_config!)
|
6
9
|
# @return [ActiveSupport::HashWithIndifferentAccess] loaded config file
|
7
10
|
# @example in config/production.rb
|
8
11
|
# Rails.application.configure do
|
9
12
|
# config.redis = config_for(:redis)
|
10
13
|
# end
|
11
14
|
def config_for(name)
|
12
|
-
ConfigFor.load_config(paths['config'].existent.first, name, ::Rails.env)
|
15
|
+
ConfigFor.load_config!(paths['config'].existent.first, name, ::Rails.env)
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
16
|
-
|
17
|
-
::Rails::Application.class_eval do
|
18
|
-
include ConfigFor::Rails
|
19
|
-
end
|
data/lib/config_for/sinatra.rb
CHANGED
@@ -1,10 +1,17 @@
|
|
1
|
+
require 'config_for'
|
2
|
+
|
1
3
|
module ConfigFor
|
2
4
|
module Sinatra
|
3
5
|
|
4
|
-
|
6
|
+
def self.registered(base)
|
7
|
+
base.set :config_path, lambda { File.join(base.settings.root, 'config') }
|
8
|
+
end
|
9
|
+
|
10
|
+
# Convenience loading of config files.
|
5
11
|
#
|
6
12
|
# @param [String, Symbol] name the config file to load
|
7
13
|
# @return [ActiveSupport::HashWithIndifferentAccess] loaded config file for current environment
|
14
|
+
# @raise (see ConfigFor.load_config!)
|
8
15
|
# @example
|
9
16
|
# class MyApp < Sinatra::Base
|
10
17
|
# register ConfigFor::Sinatra
|
@@ -12,10 +19,7 @@ module ConfigFor
|
|
12
19
|
# set :redis, Redis.new(config_for(:redis))
|
13
20
|
# end
|
14
21
|
def config_for(name)
|
15
|
-
|
16
|
-
ConfigFor.load_config(config, name, settings.environment)
|
22
|
+
ConfigFor.load_config!(settings.config_path, name, settings.environment)
|
17
23
|
end
|
18
24
|
end
|
19
25
|
end
|
20
|
-
|
21
|
-
Sinatra.register(ConfigFor::Sinatra)
|
data/lib/config_for/version.rb
CHANGED
data/spec/config_for_spec.rb
CHANGED
@@ -2,38 +2,52 @@ require 'config_for'
|
|
2
2
|
|
3
3
|
RSpec.describe ConfigFor do
|
4
4
|
let(:env) { 'production' }
|
5
|
-
subject(:config) { config_for(self.class.description) }
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
end
|
6
|
+
shared_examples 'load_config' do
|
7
|
+
subject(:config) { config_for(self.class.metadata.fetch(:name)) }
|
10
8
|
|
11
|
-
context 'database' do
|
12
9
|
it { is_expected.to eq('host' => 'localhost', 'port' => 3306) }
|
13
10
|
it { is_expected.to include(port: 3306, host: 'localhost') }
|
14
|
-
end
|
15
11
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
context 'with symbol as env' do
|
13
|
+
let(:env) { :production }
|
14
|
+
|
15
|
+
it { is_expected.to_not be_empty }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'unknown config', name: 'unknown' do
|
19
|
+
it do
|
20
|
+
expect{ subject }
|
21
|
+
.to raise_error(ConfigFor::ReadError, /^No such file or directory/)
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
context '
|
25
|
-
let(:env) { :production }
|
26
|
+
context '.load_config', name: 'database' do
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
def config_for(name)
|
29
|
+
described_class.load_config(fixtures_path, name, env)
|
29
30
|
end
|
30
|
-
end
|
31
31
|
|
32
|
-
|
33
|
-
let(:env) { 'unknown' }
|
32
|
+
include_examples 'load_config'
|
34
33
|
|
35
|
-
context '
|
34
|
+
context 'with unknown env' do
|
35
|
+
let(:env) { 'unknown' }
|
36
36
|
it { is_expected.to be_empty }
|
37
37
|
end
|
38
38
|
end
|
39
|
+
|
40
|
+
context '.load_config!', name: 'database' do
|
41
|
+
def config_for(name)
|
42
|
+
described_class.load_config!(fixtures_path, name, env)
|
43
|
+
end
|
44
|
+
|
45
|
+
include_examples 'load_config'
|
46
|
+
|
47
|
+
context 'unknown env' do
|
48
|
+
let(:env) { 'unknown' }
|
49
|
+
it { expect{subject}
|
50
|
+
.to raise_error(ConfigFor::MissingEnvironmentError, /database\.yml contains just \["production", "development"\], not unknown/) }
|
51
|
+
end
|
52
|
+
end
|
39
53
|
end
|
data/spec/rails_spec.rb
CHANGED
data/spec/sinatra_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
require 'codeclimate-test-reporter'
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
4
|
+
require 'config_for'
|
5
|
+
|
1
6
|
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
7
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
8
|
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
@@ -112,6 +117,6 @@ RSpec.shared_examples 'framework integration' do
|
|
112
117
|
before { MemFs.touch(config.join('database.yml')) }
|
113
118
|
|
114
119
|
after { subject }
|
115
|
-
it { expect(ConfigFor).to receive(:load_config).with(config.to_s,:database,'production') }
|
120
|
+
it { expect(ConfigFor).to receive(:load_config!).with(config.to_s,:database,'production') }
|
116
121
|
end
|
117
122
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michal Cichra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -165,6 +165,10 @@ files:
|
|
165
165
|
- gemfiles/rails_4_1.gemfile
|
166
166
|
- lib/config_for.rb
|
167
167
|
- lib/config_for/capistrano.rb
|
168
|
+
- lib/config_for/config.rb
|
169
|
+
- lib/config_for/integrations.rb
|
170
|
+
- lib/config_for/integrations/rails.rb
|
171
|
+
- lib/config_for/integrations/sinatra.rb
|
168
172
|
- lib/config_for/rails.rb
|
169
173
|
- lib/config_for/sinatra.rb
|
170
174
|
- lib/config_for/version.rb
|
@@ -189,9 +193,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
189
193
|
version: '0'
|
190
194
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
191
195
|
requirements:
|
192
|
-
- - "
|
196
|
+
- - ">"
|
193
197
|
- !ruby/object:Gem::Version
|
194
|
-
version:
|
198
|
+
version: 1.3.1
|
195
199
|
requirements: []
|
196
200
|
rubyforge_project:
|
197
201
|
rubygems_version: 2.2.2
|