konfig-yaml 0.8.1 → 0.9.1

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
2
  SHA1:
3
- metadata.gz: 655ff90650e291f68f89b1c9b85e50443722a594
4
- data.tar.gz: d20864bd49f7610a8bedd5afb54dafdbe5c9ebcc
3
+ metadata.gz: fde29a61af33ae8a4814948bbc67fdd876e2b47e
4
+ data.tar.gz: 228da72085e0a21d1ee18a961c35a2f3bded46d2
5
5
  SHA512:
6
- metadata.gz: 192f77af8b61f26a6932e612d312fc82caf75d95d5cd54b23cce79a5dfadf54c4542362829100dd197b090e3f607f5489894b882bb262702acdeae711cdccc41
7
- data.tar.gz: 329d9eb47988927b9abc280b08eda11619bf55c7b596dbad67b17571fda79fbb15c2a384ddb9d7ac78e5cdfa0aa113fe586a96a13a52339d8e2f818eec20f62e
6
+ metadata.gz: e9b0603ac9f70e0ee2ab0aa35cbd926e4fe7fb90e7e0a130597445a1c3bb14f9b323869210cc611b8b59aa3d15ddf3dfe282119c92207ed855455fc90042f61a
7
+ data.tar.gz: 8c7e625c074a5bc38a90b5e77e923ab0d38e5a22499f99a383fe8b0c5903b8d0505bd2fa57548e9f4f0e9feeaf2a9a0dd73955037eb5681e1625bc71b94b9712
data/README.md CHANGED
@@ -32,7 +32,7 @@ Or install it yourself as:
32
32
 
33
33
  ## Usage
34
34
 
35
- ### Load a configuration
35
+ ### Load a configuration instance
36
36
 
37
37
  ```ruby
38
38
  require 'konfig-yaml'
@@ -46,6 +46,18 @@ config = KonfigYaml.new([name], [opts]);
46
46
  * `:env` Execution environment ( default **RUBY_ENV** value, **RAILS_ENV** value, **RACK_ENV** value, or `development` )
47
47
  * `:use_cache` whether using cache ( default `true` )
48
48
 
49
+ ### Load a configuration as Static class
50
+
51
+ ```ruby
52
+ require 'konfig-yaml'
53
+
54
+ KonfigYaml.setup do |config|
55
+ config.const_name = 'Config' # default is 'Settings'
56
+ end
57
+
58
+ p Config.log.level
59
+ ```
60
+
49
61
  ### Access the values
50
62
 
51
63
  #### by accessor method
@@ -71,7 +83,7 @@ log_lv = config['log'].level
71
83
 
72
84
  ### Dynamically change settings
73
85
 
74
- Support only symbol or string
86
+ Support only symbol or string key if adding new field
75
87
 
76
88
  ```ruby
77
89
  config[:port] = 80
@@ -1,16 +1,17 @@
1
1
  require 'pathname'
2
2
  require 'neohash'
3
+ require 'ostruct'
3
4
 
4
5
  # KonfigYaml main class
5
6
  class KonfigYaml
6
7
  include NeoHash::Support
7
8
 
8
- # Create an configuration from yaml file
9
+ # Create an configuration from YAML file
9
10
  #
10
- # @param [String] name ('app') the basename of yaml file
11
+ # @param [String] name ('app') the basename of YAML file
11
12
  # @param [Hash] opts the options to intialize
12
13
  # @option opts [String] :env (ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development') execution environment
13
- # @option opts [String] :path ('config') directory path that contains the yaml file
14
+ # @option opts [String] :path ('config') directory path that contains the YAML file
14
15
  # @option opts [Boolean] :use_cache (true) whether cache settings or not
15
16
  def initialize(name = 'app', opts = nil)
16
17
  if name.is_a?(Hash) && opts.nil?
@@ -19,100 +20,138 @@ class KonfigYaml
19
20
  elsif opts.nil?
20
21
  opts = {}
21
22
  end
23
+ hash = self.class.create_hash(name, opts)
24
+ set_inner_hash(hash)
25
+ end
22
26
 
23
- path = File.expand_path(opts[:path] || 'config', Dir.pwd)
24
- env = environment(opts)
25
- use_cache = opts.fetch(:use_cache, true)
27
+ class << self
28
+ # Setup global configuration class from YAML file
29
+ # If this is called without block, this defines `Settings` class by loading `config/app.yml`
30
+ # @yield [config] configuration to the block
31
+ # @yieldparam config [OpenStruct] support `const_name`, `name`, `env`, `path` and `use_cahe` attribute
32
+ def setup(&block)
33
+ const_name = 'Settings'
34
+ name = nil
35
+ opts = {}
26
36
 
27
- h = load_config(name, env, path, use_cache)
28
- set_inner_hash(dup_hash_expand_envs(h))
29
- end
37
+ if block_given?
38
+ cfg = OpenStruct.new
39
+ yield(cfg)
40
+ cfg.each_pair do |key, val|
41
+ if key == :const_name
42
+ const_name = val
43
+ elsif key == :name
44
+ name = val
45
+ else
46
+ opts[key] = val
47
+ end
48
+ end
49
+ end
30
50
 
31
- # Clear caches
32
- def self.clear
33
- @@cfg_cache = {};
34
- end
51
+ hash = create_hash(name, opts)
52
+ cls = Class.new do
53
+ extend NeoHash::Support
54
+ set_inner_hash(hash)
55
+ end
56
+ Object.const_set(const_name, cls)
57
+ end
35
58
 
36
- private
59
+ # Clear caches
60
+ def clear
61
+ @cfg_cache = {}
62
+ end
37
63
 
38
- def environment(opts)
39
- env = opts[:env]
40
- return env.to_s if env
41
- ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
42
- end
64
+ def create_hash(name, opts)
65
+ name ||= 'app'
66
+ path = File.expand_path(opts[:path] || 'config', Dir.pwd)
67
+ env = environment(opts)
68
+ use_cache = opts.fetch(:use_cache, true)
43
69
 
44
- def cfg_cache
45
- @@cfg_cache ||= {}
46
- end
70
+ h = load_config(name, env, path, use_cache)
71
+ dup_hash_expand_envs(h)
72
+ end
47
73
 
48
- def load_config(name, env, path, use_cache)
49
- cfg_key = "#{name}/#{env}"
50
- if use_cache && cfg_cache.key?(cfg_key)
51
- return cfg_cache[cfg_key]
74
+ private
75
+
76
+ def environment(opts)
77
+ env = opts[:env]
78
+ return env.to_s if env
79
+ ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
52
80
  end
53
81
 
54
- data = load_yaml(name, path)
55
- cfg_cache[cfg_key] = convert_data_to_hash(data, env)
56
- end
82
+ def cfg_cache
83
+ @cfg_cache ||= {}
84
+ end
57
85
 
58
- def load_yaml(name, dir)
59
- file_path = Dir.glob("#{dir}/#{name}.{yml,yaml}").first
60
- raise ArgumentError.new("Not found configuration yaml file") unless file_path
61
- YAML.load(File.read(file_path))
62
- end
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
63
91
 
64
- def convert_data_to_hash(data, env)
65
- if data.include?(env)
66
- deep_merge(data[env] || {}, data['default'] || {})
67
- elsif data.include?('default')
68
- data['default']
69
- else
70
- raise ArgumentError.new("The configuration for #{env} is not defined in #{name}")
92
+ data = load_yaml(name, path)
93
+ cfg_cache[cfg_key] = convert_data_to_hash(data, env)
71
94
  end
72
- end
73
95
 
74
- def deep_merge(target, default)
75
- target.merge(default) do |key, target_val, default_val|
76
- if target_val.is_a?(Hash) && default_val.is_a?(Hash)
77
- deep_merge(target_val, default_val)
96
+ def load_yaml(name, dir)
97
+ file_path = Dir.glob("#{dir}/#{name}.{yml,yaml}").first
98
+ raise ArgumentError.new("Not found configuration yaml file") unless file_path
99
+ YAML.load(File.read(file_path))
100
+ end
101
+
102
+ def convert_data_to_hash(data, env)
103
+ if data.include?(env)
104
+ deep_merge(data[env] || {}, data['default'] || {})
105
+ elsif data.include?('default')
106
+ data['default']
78
107
  else
79
- target_val
108
+ raise ArgumentError.new("The configuration for #{env} is not defined in #{name}")
80
109
  end
81
110
  end
82
- end
83
111
 
84
- def dup_hash_expand_envs(root)
85
- root.map do |name, val|
86
- if val.is_a?(Hash)
87
- [name, dup_hash_expand_envs(val)]
88
- elsif val.is_a?(Array)
89
- [name, dup_array_expand_envs(val)]
90
- elsif val.is_a?(String)
91
- [name, expand_envs(val)]
92
- else
93
- [name, val]
112
+ def deep_merge(target, default)
113
+ target.merge(default) do |key, target_val, default_val|
114
+ if target_val.is_a?(Hash) && default_val.is_a?(Hash)
115
+ deep_merge(target_val, default_val)
116
+ else
117
+ target_val
118
+ end
94
119
  end
95
- end.to_h
96
- end
120
+ end
97
121
 
98
- def dup_array_expand_envs(root)
99
- root.map do |val|
100
- if val.is_a?(Hash)
101
- dup_hash_expand_envs(val)
102
- elsif val.is_a?(Array)
103
- dup_array_expand_envs(val)
104
- elsif val.is_a?(String)
105
- expand_envs(val)
106
- else
107
- val
122
+ def dup_hash_expand_envs(root)
123
+ root.map do |name, val|
124
+ if val.is_a?(Hash)
125
+ [name, dup_hash_expand_envs(val)]
126
+ elsif val.is_a?(Array)
127
+ [name, dup_array_expand_envs(val)]
128
+ elsif val.is_a?(String)
129
+ [name, expand_envs(val)]
130
+ else
131
+ [name, val]
132
+ end
133
+ end.to_h
134
+ end
135
+
136
+ def dup_array_expand_envs(root)
137
+ root.map do |val|
138
+ if val.is_a?(Hash)
139
+ dup_hash_expand_envs(val)
140
+ elsif val.is_a?(Array)
141
+ dup_array_expand_envs(val)
142
+ elsif val.is_a?(String)
143
+ expand_envs(val)
144
+ else
145
+ val
146
+ end
108
147
  end
109
148
  end
110
- end
111
149
 
112
- def expand_envs(str)
113
- str.gsub(/\$\{(.+?)\}/) do |m|
114
- env_key, default = $1.split(/:\-?/)
115
- ENV[env_key] || default || ''
150
+ def expand_envs(str)
151
+ str.gsub(/\$\{(.+?)\}/) do |m|
152
+ env_key, default = $1.split(/:\-?/)
153
+ ENV[env_key] || default || ''
154
+ end
116
155
  end
117
156
  end
118
157
  end
@@ -1,3 +1,3 @@
1
1
  class KonfigYaml
2
- VERSION = '0.8.1'
2
+ VERSION = '0.9.1'
3
3
  end
@@ -1,10 +1,10 @@
1
1
  default:
2
- logger:
2
+ log:
3
3
  level: info
4
4
  short_env: default
5
5
 
6
6
  development:
7
- logger:
7
+ log:
8
8
  level: debug
9
9
  short_env: dev
10
10
 
@@ -15,5 +15,5 @@ staging:
15
15
 
16
16
  production:
17
17
  short_env: prd
18
- logger:
18
+ log:
19
19
  level: error
@@ -1,10 +1,10 @@
1
1
  default:
2
- logger:
2
+ log:
3
3
  level: info
4
4
  short_env: default
5
5
 
6
6
  development:
7
- logger:
7
+ log:
8
8
  level: debug
9
9
  short_env: dev
10
10
 
@@ -15,5 +15,5 @@ staging:
15
15
 
16
16
  production:
17
17
  short_env: prd
18
- logger:
18
+ log:
19
19
  level: error
@@ -125,7 +125,7 @@ describe KonfigYaml do
125
125
  let!(:pre_instance) { described_class.new }
126
126
 
127
127
  before do
128
- allow_any_instance_of(described_class).to receive(:load_yaml).and_raise('load_yaml called')
128
+ allow(described_class).to receive(:load_yaml).and_raise('load_yaml called')
129
129
  end
130
130
 
131
131
  context 'not specified' do
@@ -162,6 +162,31 @@ describe KonfigYaml do
162
162
  end
163
163
  end
164
164
 
165
+ describe '#setup' do
166
+ let!(:path) { File.expand_path("../fixtures", __FILE__) }
167
+
168
+ context 'without block' do
169
+ it 'defines Settings class' do
170
+ described_class.setup
171
+
172
+ expect(Settings.log.file).to eq('log/app.log')
173
+ end
174
+ end
175
+
176
+ context 'with block' do
177
+ it 'defines SettingsB class' do
178
+ described_class.setup do |config|
179
+ config.name = 'another'
180
+ config.path = path
181
+ config.const_name = 'SettingsB'
182
+ end
183
+
184
+ expect(SettingsB.log.level).to eq('debug')
185
+ expect(SettingsB.short_env).to eq('dev')
186
+ end
187
+ end
188
+ end
189
+
165
190
  describe 'merged configuraton' do
166
191
  subject { described_class.new('app', path: 'config', env: env) }
167
192
 
@@ -175,8 +200,8 @@ describe KonfigYaml do
175
200
  expect(subject.db.user).to eq('user')
176
201
  expect(subject.db.pass).to eq('password')
177
202
  expect(subject.root_url).to eq('http://localhost')
178
- expect(subject.logger.level).to eq('debug')
179
- expect(subject.logger.file).to eq('log/app.log')
203
+ expect(subject.log.level).to eq('debug')
204
+ expect(subject.log.file).to eq('log/app.log')
180
205
  expect{ subject.bucket }.to raise_error(NoMethodError)
181
206
  expect{ subject.bucket_path }.to raise_error(NoMethodError)
182
207
  expect{ subject.cloud_access_key }.to raise_error(NoMethodError)
@@ -195,8 +220,8 @@ describe KonfigYaml do
195
220
  expect(subject.db.user).to eq('user')
196
221
  expect(subject.db.pass).to eq('password')
197
222
  expect(subject.root_url).to eq('http://localhost')
198
- expect(subject.logger.level).to eq('error')
199
- expect(subject.logger.file).to eq('log/test.log')
223
+ expect(subject.log.level).to eq('error')
224
+ expect(subject.log.file).to eq('log/test.log')
200
225
  expect{ subject.bucket }.to raise_error(NoMethodError)
201
226
  expect{ subject.bucket_path }.to raise_error(NoMethodError)
202
227
  expect{ subject.cloud_access_key }.to raise_error(NoMethodError)
@@ -232,8 +257,8 @@ describe KonfigYaml do
232
257
  expect(subject.db.user).to eq('user')
233
258
  expect(subject.db.pass).to eq('password')
234
259
  expect(subject.root_url).to eq('https://api-itg.example.com')
235
- expect(subject.logger.level).to eq('info')
236
- expect(subject.logger[:file]).to be_nil
260
+ expect(subject.log.level).to eq('info')
261
+ expect(subject.log[:file]).to be_nil
237
262
  expect(subject.bucket).to eq('storage-service-stg')
238
263
  expect(subject.bucket_path).to eq('/itg')
239
264
  expect(subject.cloud_access_key).to eq('aaabbbccc')
@@ -274,8 +299,8 @@ describe KonfigYaml do
274
299
  expect(subject.db.user).to eq('user')
275
300
  expect(subject.db.pass).to eq('password')
276
301
  expect(subject.root_url).to eq('https://api-stg.example.com')
277
- expect(subject.logger.level).to eq('info')
278
- expect(subject.logger['file']).to be_nil
302
+ expect(subject.log.level).to eq('info')
303
+ expect(subject.log['file']).to be_nil
279
304
  expect(subject.bucket).to eq('storage-service-stg')
280
305
  expect(subject.bucket_path).to eq('/stg')
281
306
  expect(subject.cloud_access_key).to eq('aaabbbccc')
@@ -316,8 +341,8 @@ describe KonfigYaml do
316
341
  expect(subject.db.user).to eq('user')
317
342
  expect(subject.db.pass).to eq('password')
318
343
  expect(subject.root_url).to eq('https://api-pre.example.com')
319
- expect(subject.logger.level).to eq('warn')
320
- expect(subject.logger[:file]).to be_nil
344
+ expect(subject.log.level).to eq('warn')
345
+ expect(subject.log[:file]).to be_nil
321
346
  expect(subject.bucket).to eq('storage-service-stg')
322
347
  expect(subject.bucket_path).to eq('/pre')
323
348
  expect(subject.cloud_access_key).to eq('aaabbbccc')
@@ -358,8 +383,8 @@ describe KonfigYaml do
358
383
  expect(subject.db.user).to eq('user')
359
384
  expect(subject.db.pass).to eq('password')
360
385
  expect(subject.root_url).to eq('https://api.example.com')
361
- expect(subject.logger.level).to eq('error')
362
- expect(subject.logger['file']).to be_nil
386
+ expect(subject.log.level).to eq('error')
387
+ expect(subject.log['file']).to be_nil
363
388
  expect(subject.bucket).to eq('storage-service')
364
389
  expect(subject.bucket_path).to eq('/')
365
390
  expect(subject.cloud_access_key).to eq('xxxyyyzzz')
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.8.1
4
+ version: 0.9.1
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-12 00:00:00.000000000 Z
11
+ date: 2018-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: neohash
@@ -73,12 +73,8 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - ".gitignore"
77
- - Gemfile
78
- - Gemfile.lock
79
76
  - LICENSE
80
77
  - README.md
81
- - Rakefile
82
78
  - lib/konfig-yaml.rb
83
79
  - lib/konfig_yaml.rb
84
80
  - lib/konfig_yaml/main.rb
data/.gitignore DELETED
@@ -1,34 +0,0 @@
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 DELETED
@@ -1,8 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in konfig-yaml.gemspec
4
- gemspec
5
-
6
- group :test do
7
- gem 'simplecov', require: false
8
- end
@@ -1,45 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- konfig-yaml (0.8.1)
5
- neohash (~> 0.1)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- diff-lcs (1.3)
11
- docile (1.1.5)
12
- json (2.1.0)
13
- neohash (0.1.0)
14
- rake (10.5.0)
15
- rspec (3.7.0)
16
- rspec-core (~> 3.7.0)
17
- rspec-expectations (~> 3.7.0)
18
- rspec-mocks (~> 3.7.0)
19
- rspec-core (3.7.1)
20
- rspec-support (~> 3.7.0)
21
- rspec-expectations (3.7.0)
22
- diff-lcs (>= 1.2.0, < 2.0)
23
- rspec-support (~> 3.7.0)
24
- rspec-mocks (3.7.0)
25
- diff-lcs (>= 1.2.0, < 2.0)
26
- rspec-support (~> 3.7.0)
27
- rspec-support (3.7.1)
28
- simplecov (0.15.1)
29
- docile (~> 1.1.0)
30
- json (>= 1.8, < 3)
31
- simplecov-html (~> 0.10.0)
32
- simplecov-html (0.10.2)
33
-
34
- PLATFORMS
35
- ruby
36
-
37
- DEPENDENCIES
38
- bundler (~> 1.16)
39
- konfig-yaml!
40
- rake (~> 10.0)
41
- rspec (~> 3.0)
42
- simplecov
43
-
44
- BUNDLED WITH
45
- 1.16.0
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task :default => :spec