konfig-yaml 0.9.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fde29a61af33ae8a4814948bbc67fdd876e2b47e
4
- data.tar.gz: 228da72085e0a21d1ee18a961c35a2f3bded46d2
2
+ SHA256:
3
+ metadata.gz: 81472d9c7b5adac249a44bf50b5ca8d6746f112ecb12638e32021fd8c134a8a2
4
+ data.tar.gz: b5063c6f970702f29dae0d9637fc3bb8e9395fa0929a81e5c53bc2c79950c478
5
5
  SHA512:
6
- metadata.gz: e9b0603ac9f70e0ee2ab0aa35cbd926e4fe7fb90e7e0a130597445a1c3bb14f9b323869210cc611b8b59aa3d15ddf3dfe282119c92207ed855455fc90042f61a
7
- data.tar.gz: 8c7e625c074a5bc38a90b5e77e923ab0d38e5a22499f99a383fe8b0c5903b8d0505bd2fa57548e9f4f0e9feeaf2a9a0dd73955037eb5681e1625bc71b94b9712
6
+ metadata.gz: 7365f1e19f29aae6627491839a5c622792ff607721865e632aaf554b945ccc05c0e44bbc7ccaa75e8d4c1a3d5719a3b8184e8f3b0adf2f3f45a5238e74248730
7
+ data.tar.gz: 1f96bb8b49d895c9f296fbd86fe8d05caba42da3a7733715ff30baae90d639974f470904e3c762714e4a345e8c2d2afbb3e90b2dc770a06c833ca2f8c90fec61
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # konfig-yaml for Ruby
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/konfig-yaml.svg)](https://badge.fury.io/rb/konfig-yaml)
4
+ [![document](https://img.shields.io/badge/document-1.0.0-green.svg)](http://www.rubydoc.info/gems/konfig-yaml/)
4
5
  [![Build Status](https://travis-ci.org/tilfin/konfig-yaml-rb.svg?branch=master)](https://travis-ci.org/tilfin/konfig-yaml-rb)
5
6
  [![Code Climate](https://codeclimate.com/github/tilfin/konfig-yaml-rb/badges/gpa.svg)](https://codeclimate.com/github/tilfin/konfig-yaml-rb)
6
7
  [![Test Coverage](https://codeclimate.com/github/tilfin/konfig-yaml-rb/badges/coverage.svg)](https://codeclimate.com/github/tilfin/konfig-yaml-rb/coverage)
@@ -43,8 +44,8 @@ config = KonfigYaml.new([name], [opts]);
43
44
  * `name` specifys the name of `config/<name>.yml` ( default `app` )
44
45
  * `opts`
45
46
  * `:path` config directory path resolved from the process current one ( default `config` )
47
+ * `:erb` whether expand ERB or not ( default `false` )
46
48
  * `:env` Execution environment ( default **RUBY_ENV** value, **RAILS_ENV** value, **RACK_ENV** value, or `development` )
47
- * `:use_cache` whether using cache ( default `true` )
48
49
 
49
50
  ### Load a configuration as Static class
50
51
 
@@ -96,14 +97,6 @@ p config.backend.host # "cms.example.com"
96
97
  p config.backend.port # 7080
97
98
  ```
98
99
 
99
- ### Clear caches
100
-
101
- For testing purpose
102
-
103
- ```
104
- KonfigYaml.clear
105
- ```
106
-
107
100
  ## Example
108
101
 
109
102
  ### Setup
@@ -0,0 +1,19 @@
1
+ default:
2
+ log:
3
+ level: info
4
+ short_env: default
5
+
6
+ development:
7
+ log:
8
+ level: debug
9
+ short_env: dev
10
+
11
+ empty_env:
12
+
13
+ staging:
14
+ short_env: stg
15
+
16
+ production:
17
+ short_env: prd
18
+ log:
19
+ level: error
@@ -0,0 +1,64 @@
1
+ default:
2
+ port: 1080
3
+ db:
4
+ host: localhost
5
+ name: service-${RUBY_ENV:development}
6
+ user: ${DATABASE_USER:-user}
7
+ pass: ${DATABASE_PASSWORD:-password}
8
+ log:
9
+ level: debug
10
+ access_limits:
11
+ - 127.0.0.1
12
+ entries:
13
+ - 0
14
+ - name: A
15
+ value: 1
16
+ - name: B
17
+ value: 2
18
+
19
+ development: &dev
20
+ root_url: http://localhost
21
+ log:
22
+ file: log/app.log
23
+
24
+ test:
25
+ <<: *dev
26
+ log:
27
+ level: error
28
+ file: log/test.log
29
+
30
+ integration: &deployments
31
+ root_url: https://api-itg.example.com
32
+ db:
33
+ host: ${DATABASE_HOST}
34
+ log:
35
+ level: info
36
+ bucket: storage-service-stg
37
+ bucket_path: /itg
38
+ cloud_access_key: aaabbbccc
39
+ access_limits:
40
+ - 192.168.0.0/24
41
+ - 10.0.0.0/8
42
+
43
+ staging:
44
+ <<: *deployments
45
+ root_url: https://api-stg.example.com
46
+ bucket_path: /stg
47
+
48
+ preproduction:
49
+ <<: *deployments
50
+ root_url: https://api-pre.example.com
51
+ log:
52
+ level: warn
53
+ bucket_path: /pre
54
+
55
+ production:
56
+ root_url: https://api.example.com
57
+ db:
58
+ host: ${DATABASE_HOST}
59
+ log:
60
+ level: error
61
+ bucket: storage-service
62
+ bucket_path: /
63
+ cloud_access_key: xxxyyyzzz
64
+ access_limits:
@@ -11,8 +11,8 @@ class KonfigYaml
11
11
  # @param [String] name ('app') the basename of YAML file
12
12
  # @param [Hash] opts the options to intialize
13
13
  # @option opts [String] :env (ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development') execution environment
14
+ # @option opts [Boolean] :erb (false) whether evaluate ERB or not
14
15
  # @option opts [String] :path ('config') directory path that contains the YAML file
15
- # @option opts [Boolean] :use_cache (true) whether cache settings or not
16
16
  def initialize(name = 'app', opts = nil)
17
17
  if name.is_a?(Hash) && opts.nil?
18
18
  opts = name
@@ -20,6 +20,7 @@ class KonfigYaml
20
20
  elsif opts.nil?
21
21
  opts = {}
22
22
  end
23
+
23
24
  hash = self.class.create_hash(name, opts)
24
25
  set_inner_hash(hash)
25
26
  end
@@ -56,18 +57,13 @@ class KonfigYaml
56
57
  Object.const_set(const_name, cls)
57
58
  end
58
59
 
59
- # Clear caches
60
- def clear
61
- @cfg_cache = {}
62
- end
63
-
64
60
  def create_hash(name, opts)
65
61
  name ||= 'app'
66
62
  path = File.expand_path(opts[:path] || 'config', Dir.pwd)
67
63
  env = environment(opts)
68
- use_cache = opts.fetch(:use_cache, true)
64
+ expand_erb = opts.fetch(:erb, false)
69
65
 
70
- h = load_config(name, env, path, use_cache)
66
+ h = load_config(name, env, path, expand_erb)
71
67
  dup_hash_expand_envs(h)
72
68
  end
73
69
 
@@ -79,24 +75,17 @@ class KonfigYaml
79
75
  ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
80
76
  end
81
77
 
82
- def cfg_cache
83
- @cfg_cache ||= {}
84
- end
85
-
86
- def load_config(name, env, path, use_cache)
87
- cfg_key = "#{name}/#{env}"
88
- if use_cache && cfg_cache.key?(cfg_key)
89
- return cfg_cache[cfg_key]
90
- end
91
-
92
- data = load_yaml(name, path)
93
- cfg_cache[cfg_key] = convert_data_to_hash(data, env)
78
+ def load_config(name, env, path, expand_erb)
79
+ data = load_yaml(name, path, expand_erb)
80
+ convert_data_to_hash(data, env)
94
81
  end
95
82
 
96
- def load_yaml(name, dir)
83
+ def load_yaml(name, dir, expand_erb)
97
84
  file_path = Dir.glob("#{dir}/#{name}.{yml,yaml}").first
98
85
  raise ArgumentError.new("Not found configuration yaml file") unless file_path
99
- YAML.load(File.read(file_path))
86
+ str = File.read(file_path)
87
+ str = ERB.new(str).result if expand_erb
88
+ YAML.load(str)
100
89
  end
101
90
 
102
91
  def convert_data_to_hash(data, env)
@@ -1,3 +1,3 @@
1
1
  class KonfigYaml
2
- VERSION = '0.9.1'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -2,6 +2,10 @@ default:
2
2
  log:
3
3
  level: info
4
4
  short_env: default
5
+ array:
6
+ - foo
7
+ - field1: bar
8
+ field2: baz
5
9
 
6
10
  development:
7
11
  log:
@@ -0,0 +1,2 @@
1
+ default:
2
+ label: <%= 3 * 15 %>seconds
@@ -120,43 +120,31 @@ describe KonfigYaml do
120
120
  end
121
121
  end
122
122
 
123
- describe 'option use_cache' do
123
+ describe 'option erb' do
124
124
  let!(:path) { File.expand_path("../fixtures", __FILE__) }
125
125
  let!(:pre_instance) { described_class.new }
126
126
 
127
- before do
128
- allow(described_class).to receive(:load_yaml).and_raise('load_yaml called')
129
- end
130
-
131
127
  context 'not specified' do
132
- subject { described_class.new }
128
+ subject { described_class.new('erb_config', path: path) }
133
129
 
134
- it 'loads cached instance without calling load_yaml' do
135
- expect{ subject }.not_to raise_error 'load_yaml called'
130
+ it 'loads without evaluating ERB' do
131
+ expect(subject.label).to eq '<%= 3 * 15 %>seconds'
136
132
  end
137
133
  end
138
134
 
139
135
  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 }
136
+ subject { described_class.new('erb_config', path: path, erb: true) }
148
137
 
149
- it 'loads new instance with calling load_yaml' do
150
- expect{ subject }.to raise_error 'load_yaml called'
151
- end
138
+ it 'loads with evaluating ERB' do
139
+ expect(subject.label).to eq '45seconds'
152
140
  end
153
141
  end
154
142
 
155
143
  context 'false' do
156
- subject { described_class.new(use_cache: false) }
144
+ subject { described_class.new('erb_config', path: path, erb: false) }
157
145
 
158
- it 'loads new instance with calling load_yaml' do
159
- expect{ subject }.to raise_error 'load_yaml called'
146
+ it 'loads without evaluating ERB' do
147
+ expect(subject.label).to eq '<%= 3 * 15 %>seconds'
160
148
  end
161
149
  end
162
150
  end
@@ -207,6 +195,11 @@ describe KonfigYaml do
207
195
  expect{ subject.cloud_access_key }.to raise_error(NoMethodError)
208
196
  expect(subject[:cloud_access_key]).to be_nil
209
197
  expect(subject.access_limits).to eq ['127.0.0.1']
198
+ expect(subject.entries[0]).to eq 0
199
+ expect(subject.entries[1].name).to eq 'A'
200
+ expect(subject.entries[1].value).to eq 1
201
+ expect(subject.entries[2].name).to eq 'B'
202
+ expect(subject.entries[2].value).to eq 2
210
203
  end
211
204
  end
212
205
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konfig-yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toshimitsu Takahashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-02-13 00:00:00.000000000 Z
11
+ date: 2018-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: neohash
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.1'
19
+ version: '0.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.1'
26
+ version: '0.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -75,12 +75,15 @@ extra_rdoc_files: []
75
75
  files:
76
76
  - LICENSE
77
77
  - README.md
78
+ - config/another.yaml
79
+ - config/app.yml
78
80
  - lib/konfig-yaml.rb
79
81
  - lib/konfig_yaml.rb
80
82
  - lib/konfig_yaml/main.rb
81
83
  - lib/konfig_yaml/version.rb
82
84
  - spec/fixtures/another.yml
83
85
  - spec/fixtures/app.yaml
86
+ - spec/fixtures/erb_config.yml
84
87
  - spec/konfig_yaml_spec.rb
85
88
  - spec/spec_helper.rb
86
89
  homepage: https://github.com/tilfin/konfig-yaml-rb
@@ -103,12 +106,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
106
  version: '0'
104
107
  requirements: []
105
108
  rubyforge_project:
106
- rubygems_version: 2.6.13
109
+ rubygems_version: 2.7.3
107
110
  signing_key:
108
111
  specification_version: 4
109
112
  summary: Loader for YAML configuration with ENVs.
110
113
  test_files:
111
114
  - spec/konfig_yaml_spec.rb
112
115
  - spec/fixtures/another.yml
116
+ - spec/fixtures/erb_config.yml
113
117
  - spec/fixtures/app.yaml
114
118
  - spec/spec_helper.rb
119
+ - config/another.yaml
120
+ - config/app.yml