konfig-yaml 0.8.0
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.
- checksums.yaml +7 -0
- data/.gitignore +34 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +43 -0
- data/LICENSE +21 -0
- data/README.md +118 -0
- data/Rakefile +6 -0
- data/lib/konfig-yaml.rb +3 -0
- data/lib/konfig_yaml.rb +7 -0
- data/lib/konfig_yaml/hash_wrapper.rb +87 -0
- data/lib/konfig_yaml/inner_hash.rb +10 -0
- data/lib/konfig_yaml/main.rb +84 -0
- data/lib/konfig_yaml/version.rb +3 -0
- data/spec/fixtures/another.yml +19 -0
- data/spec/fixtures/app.yaml +19 -0
- data/spec/inner_hash_spec.rb +60 -0
- data/spec/konfig_yaml_spec.rb +222 -0
- data/spec/spec_helper.rb +7 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 13da2d445b1464b1ab4b58325fd99cc4704c1864
|
4
|
+
data.tar.gz: aa81f79ce0cf51c67743a5984c9ba613fe2d426c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c0d0a890eea02df118042736de5d72313891cdda3047d8e911a918096c89ef5d41c250b16e68763939139ed15c08ea6fc26d0d7175a3629c56973d636a6d9bc5
|
7
|
+
data.tar.gz: e436a125f2178c66d2797c6cb600f31368695e20ea28fded94cf8447014ef1adf5771e7980a8e9a46419eba817022c9ca4321e80d4b180eb3033123a96c2cf53
|
data/.gitignore
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Documentation cache and generated files:
|
17
|
+
/.yardoc/
|
18
|
+
/_yardoc/
|
19
|
+
/doc/
|
20
|
+
/rdoc/
|
21
|
+
|
22
|
+
## Environment normalization:
|
23
|
+
/.bundle/
|
24
|
+
/vendor/bundle
|
25
|
+
/lib/bundler/man/
|
26
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
# Gemfile.lock
|
30
|
+
# .ruby-version
|
31
|
+
# .ruby-gemset
|
32
|
+
|
33
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
34
|
+
.rvmrc
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
konfig-yaml (0.8.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
docile (1.1.5)
|
11
|
+
json (2.1.0)
|
12
|
+
rake (10.5.0)
|
13
|
+
rspec (3.7.0)
|
14
|
+
rspec-core (~> 3.7.0)
|
15
|
+
rspec-expectations (~> 3.7.0)
|
16
|
+
rspec-mocks (~> 3.7.0)
|
17
|
+
rspec-core (3.7.1)
|
18
|
+
rspec-support (~> 3.7.0)
|
19
|
+
rspec-expectations (3.7.0)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.7.0)
|
22
|
+
rspec-mocks (3.7.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.7.0)
|
25
|
+
rspec-support (3.7.1)
|
26
|
+
simplecov (0.15.1)
|
27
|
+
docile (~> 1.1.0)
|
28
|
+
json (>= 1.8, < 3)
|
29
|
+
simplecov-html (~> 0.10.0)
|
30
|
+
simplecov-html (0.10.2)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
bundler (~> 1.16)
|
37
|
+
konfig-yaml!
|
38
|
+
rake (~> 10.0)
|
39
|
+
rspec (~> 3.0)
|
40
|
+
simplecov
|
41
|
+
|
42
|
+
BUNDLED WITH
|
43
|
+
1.16.0
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Toshimitsu Takahashi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
# konfig-yaml for Ruby
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/konfig-yaml)
|
4
|
+
[](https://travis-ci.org/tilfin/konfig-yaml-rb)
|
5
|
+
[](https://codeclimate.com/github/tilfin/konfig-yaml-rb)
|
6
|
+
[](https://codeclimate.com/github/tilfin/konfig-yaml-rb/coverage)
|
7
|
+
|
8
|
+
The loader of yaml base configuration for each run enviroments like [settingslogic](https://github.com/settingslogic/settingslogic).
|
9
|
+
|
10
|
+
- Expand environment variables (ex. `users-${RUBY_ENV}`)
|
11
|
+
- Deep merge the environment settings and default settings (except array items)
|
12
|
+
- Ruby version of [konfig-yaml](https://github.com/tilfin/konfig-yaml)
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'konfig-yaml'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install konfig-yaml
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
```
|
33
|
+
require 'konfig-yaml'
|
34
|
+
|
35
|
+
config = KonfigYaml.new([name], [opts]);
|
36
|
+
```
|
37
|
+
|
38
|
+
* `name` specifys the name of `config/<name>.yml` ( default `app` )
|
39
|
+
* `opts`
|
40
|
+
* `:path` config directory path resolved from the process current one ( default `config` )
|
41
|
+
* `:env` Run environment ( default **RUBY_ENV** value, **RAILS_ENV** value, **RACK_ENV** value, or `development` )
|
42
|
+
* `:use_cache` whether using cache ( default `true` )
|
43
|
+
|
44
|
+
#### Clear cache
|
45
|
+
|
46
|
+
```
|
47
|
+
KonfigYaml.clear
|
48
|
+
```
|
49
|
+
|
50
|
+
## Example
|
51
|
+
|
52
|
+
### Setup
|
53
|
+
|
54
|
+
#### config/app.yml
|
55
|
+
|
56
|
+
```
|
57
|
+
default:
|
58
|
+
port: 8080
|
59
|
+
logger:
|
60
|
+
level: info
|
61
|
+
db:
|
62
|
+
name: ${BRAND:-normal}-app-${RUBY_ENV}
|
63
|
+
user: user
|
64
|
+
pass: pass
|
65
|
+
|
66
|
+
production:
|
67
|
+
port: 1080
|
68
|
+
logger:
|
69
|
+
level: error
|
70
|
+
db:
|
71
|
+
pass: Password
|
72
|
+
```
|
73
|
+
|
74
|
+
#### main.rb
|
75
|
+
|
76
|
+
```
|
77
|
+
require 'konfig-yaml'
|
78
|
+
|
79
|
+
config = KonfigYaml.new
|
80
|
+
|
81
|
+
puts config.port
|
82
|
+
puts config.logger.level
|
83
|
+
puts config.db.user
|
84
|
+
puts config[:db]['name']
|
85
|
+
puts config['db'].password
|
86
|
+
```
|
87
|
+
|
88
|
+
### Run
|
89
|
+
|
90
|
+
#### In development
|
91
|
+
|
92
|
+
```
|
93
|
+
$ RUBY_ENV=development ruby main.rb
|
94
|
+
8080
|
95
|
+
info
|
96
|
+
normal-app-development
|
97
|
+
user
|
98
|
+
pass
|
99
|
+
```
|
100
|
+
|
101
|
+
#### In production
|
102
|
+
|
103
|
+
```
|
104
|
+
$ RUBY_ENV=production BRAND=awesome ruby main.rb
|
105
|
+
1080
|
106
|
+
error
|
107
|
+
awesome-app-production
|
108
|
+
user
|
109
|
+
Password
|
110
|
+
```
|
111
|
+
|
112
|
+
## License
|
113
|
+
|
114
|
+
[MIT](LICENSE)
|
115
|
+
|
116
|
+
## Contributing
|
117
|
+
|
118
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tilfin/konfig-yaml-rb.
|
data/Rakefile
ADDED
data/lib/konfig-yaml.rb
ADDED
data/lib/konfig_yaml.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
class KonfigYaml
|
2
|
+
module HashWrapper
|
3
|
+
def activate
|
4
|
+
replace_hash_and_expand_env
|
5
|
+
define_attr_readers
|
6
|
+
end
|
7
|
+
|
8
|
+
def [](key)
|
9
|
+
@h[key.to_sym]
|
10
|
+
end
|
11
|
+
|
12
|
+
def []=(key, value)
|
13
|
+
if value.is_a?(Hash)
|
14
|
+
@h[key.to_sym] = InnerHash.new(value)
|
15
|
+
else
|
16
|
+
@h[key.to_sym] = value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def each
|
21
|
+
@h.each
|
22
|
+
end
|
23
|
+
|
24
|
+
def each_key
|
25
|
+
@h.each_key
|
26
|
+
end
|
27
|
+
|
28
|
+
def each_value
|
29
|
+
@h.each_value
|
30
|
+
end
|
31
|
+
|
32
|
+
def include?(key)
|
33
|
+
@h.include?(key)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Convert to original Hash
|
37
|
+
#
|
38
|
+
# @return [Hash] hash
|
39
|
+
# @param [Hash] opts the options to intialize
|
40
|
+
# @option opts [String] :symbolize_names (true) whether symbolize names or not
|
41
|
+
def to_h(opts = {})
|
42
|
+
symbolize = opts.fetch(:symbolize_names, true)
|
43
|
+
|
44
|
+
@h.map {|name, val|
|
45
|
+
[symbolize ? name : name.to_s,
|
46
|
+
val.is_a?(InnerHash) ? val.to_h(opts) : val]
|
47
|
+
}.to_h
|
48
|
+
end
|
49
|
+
|
50
|
+
alias_method :each_pair, :each
|
51
|
+
alias_method :has_key?, :include?
|
52
|
+
alias_method :key?, :include?
|
53
|
+
alias_method :member?, :include?
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def define_attr_readers
|
58
|
+
self.instance_eval do |obj|
|
59
|
+
s = class << self; self end
|
60
|
+
@h.each_key do |name|
|
61
|
+
s.send :define_method , name, lambda { @h[name] }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def replace_hash_and_expand_env
|
67
|
+
new_h = {}
|
68
|
+
@h.each do |name, val|
|
69
|
+
new_h[name.to_sym] = convert_value(val)
|
70
|
+
end
|
71
|
+
@h = new_h
|
72
|
+
end
|
73
|
+
|
74
|
+
def convert_value(val)
|
75
|
+
if val.is_a?(Hash)
|
76
|
+
InnerHash.new(val)
|
77
|
+
elsif val.is_a?(String)
|
78
|
+
val.gsub(/\$\{(.+?)\}/) {|m|
|
79
|
+
env_key, default = $1.split(/:\-?/)
|
80
|
+
ENV[env_key] || default || ''
|
81
|
+
}
|
82
|
+
else
|
83
|
+
val
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
# KonfigYaml main class
|
4
|
+
class KonfigYaml
|
5
|
+
include HashWrapper
|
6
|
+
# Create an configuration from yaml file
|
7
|
+
#
|
8
|
+
# @param [String] name ('app') the basename of yaml file
|
9
|
+
# @param [Hash] opts the options to intialize
|
10
|
+
# @option opts [String] :env (ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development') run environment
|
11
|
+
# @option opts [String] :path ('config') directory path that contains the yaml file
|
12
|
+
# @option opts [Boolean] :use_cache (true) whether cache settings or not
|
13
|
+
def initialize(name = 'app', opts = nil)
|
14
|
+
if name.is_a?(Hash) && opts.nil?
|
15
|
+
opts = name
|
16
|
+
name = 'app'
|
17
|
+
elsif opts.nil?
|
18
|
+
opts = {}
|
19
|
+
end
|
20
|
+
|
21
|
+
path = File.expand_path(opts[:path] || 'config', Dir.pwd)
|
22
|
+
env = environment(opts)
|
23
|
+
use_cache = opts.fetch(:use_cache, true)
|
24
|
+
|
25
|
+
load_config(name, env, path, use_cache)
|
26
|
+
activate
|
27
|
+
end
|
28
|
+
|
29
|
+
# Clear caches
|
30
|
+
def self.clear
|
31
|
+
@@cfg_cache = {};
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def environment(opts)
|
37
|
+
env = opts[:env]
|
38
|
+
return env.to_s if env
|
39
|
+
ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
|
40
|
+
end
|
41
|
+
|
42
|
+
def cfg_cache
|
43
|
+
@@cfg_cache ||= {}
|
44
|
+
end
|
45
|
+
|
46
|
+
def load_config(name, env, path, use_cache)
|
47
|
+
cfg_key = "#{name}/#{env}"
|
48
|
+
if use_cache && cfg_cache.key?(cfg_key)
|
49
|
+
@h = cfg_cache[cfg_key]
|
50
|
+
return
|
51
|
+
end
|
52
|
+
|
53
|
+
data = load_yaml(name, path)
|
54
|
+
convert_data_to_h(data, env)
|
55
|
+
|
56
|
+
cfg_cache[cfg_key] = @h
|
57
|
+
end
|
58
|
+
|
59
|
+
def load_yaml(name, dir)
|
60
|
+
file_path = Dir.glob("#{dir}/#{name}.{yml,yaml}").first
|
61
|
+
raise ArgumentError.new("Not found configuration yaml file") unless file_path
|
62
|
+
YAML.load(File.read(file_path))
|
63
|
+
end
|
64
|
+
|
65
|
+
def convert_data_to_h(data, env)
|
66
|
+
if data.include?(env)
|
67
|
+
@h = deep_merge(data[env] || {}, data['default'] || {})
|
68
|
+
elsif data.include?('default')
|
69
|
+
@h = data['default']
|
70
|
+
else
|
71
|
+
raise ArgumentError.new("The configuration for #{env} is not defined in #{name}")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def deep_merge(target, default)
|
76
|
+
target.merge!(default) do |key, target_val, default_val|
|
77
|
+
if target_val.is_a?(Hash) && default_val.is_a?(Hash)
|
78
|
+
deep_merge(target_val, default_val)
|
79
|
+
else
|
80
|
+
target_val
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
describe KonfigYaml::InnerHash do
|
5
|
+
subject { described_class.new(h) }
|
6
|
+
|
7
|
+
let!(:h) do
|
8
|
+
{
|
9
|
+
'port' => 80,
|
10
|
+
'logger' => { 'level' => 'warn' }
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#initialize' do
|
15
|
+
it 'an instance that has accessors by method' do
|
16
|
+
expect(subject.port).to eq(80)
|
17
|
+
expect(subject.logger.level).to eq('warn')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'an instance that has accessors by symbol' do
|
21
|
+
expect(subject[:port]).to eq(80)
|
22
|
+
expect(subject[:logger][:level]).to eq('warn')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'an instance that has accessors by string' do
|
26
|
+
expect(subject['port']).to eq(80)
|
27
|
+
expect(subject['logger']['level']).to eq('warn')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'an instance that has accessors by various ways' do
|
31
|
+
expect(subject.logger[:level]).to eq('warn')
|
32
|
+
expect(subject.logger['level']).to eq('warn')
|
33
|
+
expect(subject[:logger].level).to eq('warn')
|
34
|
+
expect(subject[:logger]['level']).to eq('warn')
|
35
|
+
expect(subject['logger'].level).to eq('warn')
|
36
|
+
expect(subject['logger'][:level]).to eq('warn')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#to_h' do
|
41
|
+
let!(:h_symbol) do
|
42
|
+
{
|
43
|
+
port: 80,
|
44
|
+
logger: { level: 'warn' }
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'creates an hash with name as symbol' do
|
49
|
+
expect(subject.to_h).to eq(h_symbol)
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'creates an hash with name as symbol' do
|
53
|
+
expect(subject.to_h(symbolize_names: true)).to eq(h_symbol)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'creates an hash with name as string' do
|
57
|
+
expect(subject.to_h(symbolize_names: false)).to eq(h)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,222 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
describe KonfigYaml do
|
5
|
+
describe '#initialize' do
|
6
|
+
describe 'option name, env' do
|
7
|
+
before do
|
8
|
+
ENV['RUBY_ENV'] = nil
|
9
|
+
ENV['RAILS_ENV'] = nil
|
10
|
+
ENV['RACK_ENV'] = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
subject { described_class.new('another') }
|
14
|
+
|
15
|
+
it 'not specified' do
|
16
|
+
expect(subject.short_env).to eq('dev')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'specified symbol' do
|
20
|
+
sbj = described_class.new('another', env: 'staging')
|
21
|
+
expect(sbj.short_env).to eq('stg')
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'specified string' do
|
25
|
+
sbj = described_class.new('another', env: :production)
|
26
|
+
expect(sbj.short_env).to eq('prd')
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'setenv RUBY_ENV' do
|
30
|
+
before { ENV['RUBY_ENV'] = 'production' }
|
31
|
+
after { ENV['RUBY_ENV'] = nil }
|
32
|
+
|
33
|
+
it { expect(subject.short_env).to eq('prd') }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'setenv RAILS_ENV' do
|
37
|
+
before { ENV['RAILS_ENV'] = 'staging' }
|
38
|
+
after { ENV['RAILS_ENV'] = nil }
|
39
|
+
|
40
|
+
it { expect(subject.short_env).to eq('stg') }
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'setenv RACK_ENV' do
|
44
|
+
before { ENV['RACK_ENV'] = 'integration' }
|
45
|
+
after { ENV['RACK_ENV'] = nil }
|
46
|
+
|
47
|
+
it { expect(subject.short_env).to eq('default') }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'setenv RUBY_ENV, RAILS_ENV' do
|
51
|
+
before do
|
52
|
+
ENV['RUBY_ENV'] = 'production'
|
53
|
+
ENV['RAILS_ENV'] = 'staging'
|
54
|
+
end
|
55
|
+
|
56
|
+
after do
|
57
|
+
ENV['RUBY_ENV'] = nil
|
58
|
+
ENV['RAILS_ENV'] = nil
|
59
|
+
end
|
60
|
+
|
61
|
+
it { expect(subject.short_env).to eq('prd') }
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'setenv RAILS_ENV, RACK_ENV' do
|
65
|
+
before do
|
66
|
+
ENV['RAILS_ENV'] = 'staging'
|
67
|
+
ENV['RACK_ENV'] = 'integration'
|
68
|
+
end
|
69
|
+
|
70
|
+
after do
|
71
|
+
ENV['RAILS_ENV'] = nil
|
72
|
+
ENV['RACK_ENV'] = nil
|
73
|
+
end
|
74
|
+
|
75
|
+
it { expect(subject.short_env).to eq('stg') }
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'setenv RUBY_ENV, RAILS_ENV, RACK_ENV' do
|
79
|
+
before do
|
80
|
+
ENV['RUBY_ENV'] = 'production'
|
81
|
+
ENV['RAILS_ENV'] = 'staging'
|
82
|
+
ENV['RACK_ENV'] = 'integration'
|
83
|
+
end
|
84
|
+
|
85
|
+
after do
|
86
|
+
ENV['RUBY_ENV'] = nil
|
87
|
+
ENV['RAILS_ENV'] = nil
|
88
|
+
ENV['RACK_ENV'] = nil
|
89
|
+
end
|
90
|
+
|
91
|
+
it { expect(subject.short_env).to eq('prd') }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe 'option path' do
|
96
|
+
let!(:path) { File.expand_path("../fixtures", __FILE__) }
|
97
|
+
|
98
|
+
context 'name is not specified' do
|
99
|
+
subject { described_class.new(path: path) }
|
100
|
+
|
101
|
+
it 'loads app.yaml' do
|
102
|
+
expect { subject }.to_not raise_error
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'name is specified' do
|
107
|
+
subject { described_class.new('another', path: path) }
|
108
|
+
|
109
|
+
it 'loads another.yml' do
|
110
|
+
expect { subject }.to_not raise_error
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context "name's file does not exist" do
|
115
|
+
subject { described_class.new('none', path: path) }
|
116
|
+
|
117
|
+
it 'raises an error' do
|
118
|
+
expect { subject }.to raise_error ArgumentError
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
describe 'option use_cache' do
|
124
|
+
let!(:path) { File.expand_path("../fixtures", __FILE__) }
|
125
|
+
let!(:pre_instance) { described_class.new }
|
126
|
+
|
127
|
+
before do
|
128
|
+
allow_any_instance_of(described_class).to receive(:load_yaml).and_raise('load_yaml called')
|
129
|
+
end
|
130
|
+
|
131
|
+
context 'not specified' do
|
132
|
+
subject { described_class.new }
|
133
|
+
|
134
|
+
it 'loads cached instance without calling load_yaml' do
|
135
|
+
expect{ subject }.not_to raise_error 'load_yaml called'
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'true' do
|
140
|
+
subject { described_class.new(use_cache: true) }
|
141
|
+
|
142
|
+
it 'loads cached instance without calling load_yaml' do
|
143
|
+
expect{ subject }.not_to raise_error 'load_yaml called'
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'after cache cleared' do
|
147
|
+
before { described_class.clear }
|
148
|
+
|
149
|
+
it 'loads new instance with calling load_yaml' do
|
150
|
+
expect{ subject }.to raise_error 'load_yaml called'
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
context 'false' do
|
156
|
+
subject { described_class.new(use_cache: false) }
|
157
|
+
|
158
|
+
it 'loads new instance with calling load_yaml' do
|
159
|
+
expect{ subject }.to raise_error 'load_yaml called'
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe 'accessors' do
|
166
|
+
subject { described_class.new('app', path: 'config') }
|
167
|
+
|
168
|
+
it 'an instance that has accessors by method' do
|
169
|
+
expect(subject.port).to eq(8080)
|
170
|
+
expect(subject.logger.level).to eq('info')
|
171
|
+
expect(subject.db.name).to eq('-app-development')
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'an instance that has accessors by symbol' do
|
175
|
+
expect(subject[:port]).to eq(8080)
|
176
|
+
expect(subject[:logger][:level]).to eq('info')
|
177
|
+
end
|
178
|
+
|
179
|
+
it 'an instance that has accessors by string' do
|
180
|
+
expect(subject['port']).to eq(8080)
|
181
|
+
expect(subject['logger']['level']).to eq('info')
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'an instance that has accessors by various ways' do
|
185
|
+
expect(subject.logger[:level]).to eq('info')
|
186
|
+
expect(subject.logger['level']).to eq('info')
|
187
|
+
expect(subject[:logger].level).to eq('info')
|
188
|
+
expect(subject[:logger]['level']).to eq('info')
|
189
|
+
expect(subject['logger'].level).to eq('info')
|
190
|
+
expect(subject['logger'][:level]).to eq('info')
|
191
|
+
end
|
192
|
+
|
193
|
+
context 'defined BRAND' do
|
194
|
+
before { ENV['BRAND'] = 'awesome' }
|
195
|
+
after { ENV['BRAND'] = nil }
|
196
|
+
|
197
|
+
it 'expands undefined ENV value as default' do
|
198
|
+
expect(subject.db.name).to eq('awesome-app-development')
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'environment is production' do
|
203
|
+
before { ENV['RUBY_ENV'] = 'production' }
|
204
|
+
after { ENV['RUBY_ENV'] = nil }
|
205
|
+
|
206
|
+
it 'an instance that provides the values overwritten by values of production:' do
|
207
|
+
expect(subject.port).to eq(1080)
|
208
|
+
expect(subject[:logger]['level']).to eq('error')
|
209
|
+
expect(subject.db.name).to eq('-app-production')
|
210
|
+
end
|
211
|
+
|
212
|
+
context 'defined BRAND' do
|
213
|
+
before { ENV['BRAND'] = 'brand' }
|
214
|
+
after { ENV['BRAND'] = nil }
|
215
|
+
|
216
|
+
it 'expands defined ENV value' do
|
217
|
+
expect(subject.db.name).to eq('brand-app-production')
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: konfig-yaml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Toshimitsu Takahashi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: The loader of yaml base configuration for each run enviroments.
|
56
|
+
email:
|
57
|
+
- toshi@tilfin.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- Gemfile.lock
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/konfig-yaml.rb
|
69
|
+
- lib/konfig_yaml.rb
|
70
|
+
- lib/konfig_yaml/hash_wrapper.rb
|
71
|
+
- lib/konfig_yaml/inner_hash.rb
|
72
|
+
- lib/konfig_yaml/main.rb
|
73
|
+
- lib/konfig_yaml/version.rb
|
74
|
+
- spec/fixtures/another.yml
|
75
|
+
- spec/fixtures/app.yaml
|
76
|
+
- spec/inner_hash_spec.rb
|
77
|
+
- spec/konfig_yaml_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
homepage: https://github.com/tilfin/konfig-yaml-rb
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubyforge_project:
|
99
|
+
rubygems_version: 2.6.13
|
100
|
+
signing_key:
|
101
|
+
specification_version: 4
|
102
|
+
summary: Loader for yaml base configuration with ENVs.
|
103
|
+
test_files:
|
104
|
+
- spec/inner_hash_spec.rb
|
105
|
+
- spec/konfig_yaml_spec.rb
|
106
|
+
- spec/fixtures/another.yml
|
107
|
+
- spec/fixtures/app.yaml
|
108
|
+
- spec/spec_helper.rb
|