qonfig 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 364dfdc09c159ed886729491cc0d066f961b5be382b766d99d57705fca5f88d7
4
- data.tar.gz: 9525458cf3d84fa9588ab48805cdb8a5ec0150278a7658986fb6325efcd2d191
3
+ metadata.gz: 29fa558caa3bd1f96663617174ba854aaf286cf17a08433c8fb4a497fde1d702
4
+ data.tar.gz: 6f3ad5c3bd5c4e6ca9b81897c11cb8a8edf208d50e86238ade40ccab99832e36
5
5
  SHA512:
6
- metadata.gz: 6cf8f8df874ebcb636f7862247ada545579f413c030b4d4dde51decb72ed18f38f5af3faa9c595e6cf741f25ab6fc8a09afef4c72133ced1448f12bbe76dbb96
7
- data.tar.gz: 785f7e10238601b7f5be5c506d516004eee7d914b451e2a4c0a7e0aebf6257ca0003f19a52bad0c86f35c89713b4a837d5e175cbf71217cbd1f0de1a6950cc45
6
+ metadata.gz: 0aa25942d38b95a2af5f432af94bd642d8b7074e35f4a53f31b987dab409f3a274853b76eb42fe72f2ca171336e2907eb06309c00062ab3fb188ad1d268c775e
7
+ data.tar.gz: c7870403e266a21f837bd4789b5a7df74bc06e165a78f4155fb707a4083c165c144ba19e2764610efd3aef802ef0644dfc932abf89be63a7c7553596b495010d
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  Gemfile.lock
11
11
  /.idea
12
12
  .ruby-version
13
+ /.vscode/
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [0.3.0] - 2018-06-13
5
+ ### Added
6
+ - Improved configuration process: `#configure` can take a hash as a configuration `[option key => option]`
7
+ map of values;
8
+
9
+ ### Changed
10
+ - `#clear!` causes `Qonfig::FrozenSettingsError` if config object is frozen;
11
+
4
12
  ## [0.2.0] - 2018-06-07
5
13
  ### Added
6
14
  - Instant configuration via block `config = Config.new { |conf| <<your configuration code>> }`;
data/README.md CHANGED
@@ -26,9 +26,9 @@ require 'qonfig'
26
26
  - [Inheritance](#inheritance)
27
27
  - [Composition](#composition)
28
28
  - [Hash representation](#hash-representation)
29
- - [State freeze](#state-freeze)
30
29
  - [Config reloading](#config-reloading) (reload config definitions and option values)
31
30
  - [Clear options](#clear-options) (set to nil)
31
+ - [State freeze](#state-freeze)
32
32
  - [Settings as Predicates](#settings-as-predicates)
33
33
  - [Load from YAML file](#load-from-yaml-file)
34
34
  - [Load from ENV](#load-from-env)
@@ -131,6 +131,23 @@ config = Config.new do |conf|
131
131
  conf.geo_api.provider = :amazon_maps
132
132
  conf.testing.engine = :crypto_test
133
133
  end
134
+
135
+ # using a hash
136
+ config = Config.new(
137
+ testing: { engine: :mini_test, parallel: false },
138
+ geo_api: { provider: :rambler_maps },
139
+ enable_middlewares: true
140
+ )
141
+ config.configure(enable_middlewares: false)
142
+
143
+ # using both hash and proc (proc has higher priority)
144
+ config = Config.new(enable_middlewares: true) do |conf|
145
+ conf.testing.parallel = true
146
+ end
147
+
148
+ config.configure(geo_api: { provider: nil }) do |conf|
149
+ conf.testing.engine = :rspec
150
+ end
134
151
  ```
135
152
 
136
153
  ---
@@ -272,11 +289,11 @@ config.settings.logger # => #<Logger:0x00007ff9> (reloaded from defaults)
272
289
  config.settings.enable_api # => false (new setting)
273
290
 
274
291
  # reload with instant configuration
275
- config.reload! do |conf|
292
+ config.reload!(db: { adapter: 'oracle' }) do |conf|
276
293
  conf.enable_api = true # changed instantly
277
294
  end
278
295
 
279
- config.settings.db.adapter # => 'mongoid'
296
+ config.settings.db.adapter # => 'oracle'
280
297
  config.settings.logger = # => #<Logger:0x00007ff9>
281
298
  config.settings.enable_api # => true # value from instant change
282
299
  ```
@@ -337,6 +354,7 @@ config.settings.worker = :que # => Qonfig::FrozenSettingsError
337
354
  config.settings.db.adapter = 'mongoid' # => Qonfig::FrozenSettingsError
338
355
 
339
356
  config.reload! # => Qonfig::FrozenSettingsError
357
+ config.clear! # => Qonfig::FrozenSettingsError
340
358
  ```
341
359
 
342
360
  ---
@@ -441,7 +459,7 @@ config.settings.ruby.platform # => 'x86_64-darwin17'
441
459
  # --- strict mode ---
442
460
  class Config < Qonfig::DataSet
443
461
  setting :nonexistent_yaml do
444
- load_from_yaml 'unexistent_file.yml', strict: true # true by default
462
+ load_from_yaml 'nonexistent_yaml.yml', strict: true # true by default
445
463
  end
446
464
 
447
465
  setting :another_key
@@ -452,7 +470,7 @@ Config.new # => Qonfig::FileNotFoundError
452
470
  # --- non-strict mode ---
453
471
  class Config < Qonfig::DataSet
454
472
  settings :nonexistent_yaml do
455
- load_from_yaml 'unexistent_file.yml', strict: false
473
+ load_from_yaml 'nonexistent_yaml.yml', strict: false
456
474
  end
457
475
 
458
476
  setting :another_key
@@ -539,7 +557,7 @@ config.settings['RUN_CI'] # => '1'
539
557
  class Config < Qonfig::DataSet
540
558
  load_from_self # on the root
541
559
 
542
- setting :clowd do
560
+ setting :nested do
543
561
  load_from_self # nested
544
562
  end
545
563
  end
@@ -58,14 +58,15 @@ module Qonfig
58
58
  end
59
59
  end
60
60
 
61
+ # @param options_map [Hash]
61
62
  # @param block [Proc]
62
63
  # @return [void]
63
64
  #
64
65
  # @api public
65
66
  # @since 0.2.0
66
- def configure(&block)
67
+ def configure(options_map = {}, &block)
67
68
  @__qonfig_access_lock__.synchronize do
68
- config.configure(&block) if block_given?
69
+ config.configure(options_map, &block)
69
70
  end
70
71
  end
71
72
 
@@ -93,14 +94,15 @@ module Qonfig
93
94
  end
94
95
  end
95
96
 
97
+ # @param options_map [Hash]
96
98
  # @param block [Proc]
97
99
  # @return [void]
98
100
  #
99
101
  # @api public
100
102
  # @since 0.2.0
101
- def configure(&block)
103
+ def configure(options_map = {}, &block)
102
104
  self.class.instance_variable_get(:@__qonfig_access_lock__).synchronize do
103
- config.configure(&block) if block_given?
105
+ config.configure(options_map, &block)
104
106
  end
105
107
  end
106
108
  end
@@ -13,15 +13,16 @@ module Qonfig
13
13
  # @since 0.1.0
14
14
  attr_reader :settings
15
15
 
16
+ # @param options_map [Hash]
16
17
  # @param configurations [Proc]
17
18
  #
18
19
  # @api public
19
20
  # @since 0.1.0
20
- def initialize(&configurations)
21
+ def initialize(options_map = {}, &configurations)
21
22
  @__access_lock__ = Mutex.new
22
23
  @__definition_lock__ = Mutex.new
23
24
 
24
- thread_safe_definition { load!(&configurations) }
25
+ thread_safe_definition { load!(options_map, &configurations) }
25
26
  end
26
27
 
27
28
  # @return [void]
@@ -40,6 +41,7 @@ module Qonfig
40
41
  thread_safe_access { settings.__is_frozen__ }
41
42
  end
42
43
 
44
+ # @param options_map [Hash]
43
45
  # @param configurations [Proc]
44
46
  # @return [void]
45
47
  #
@@ -47,19 +49,23 @@ module Qonfig
47
49
  #
48
50
  # @api public
49
51
  # @since 0.2.0
50
- def reload!(&configurations)
52
+ def reload!(options_map = {}, &configurations)
51
53
  thread_safe_definition do
52
54
  raise Qonfig::FrozenSettingsError, 'Frozen config can not be reloaded' if frozen?
53
- load!(&configurations)
55
+ load!(options_map, &configurations)
54
56
  end
55
57
  end
56
58
 
59
+ # @param options_map [Hash]
57
60
  # @return [void]
58
61
  #
59
62
  # @api public
60
63
  # @since 0.1.0
61
- def configure
62
- thread_safe_access { yield(settings) if block_given? }
64
+ def configure(options_map = {})
65
+ thread_safe_access do
66
+ settings.__apply_values__(options_map)
67
+ yield(settings) if block_given?
68
+ end
63
69
  end
64
70
 
65
71
  # @return [Hash]
@@ -107,14 +113,15 @@ module Qonfig
107
113
  Qonfig::Settings::Builder.build(self.class.commands.dup)
108
114
  end
109
115
 
116
+ # @param options_map [Hash]
110
117
  # @param configurations [Proc]
111
118
  # @return [void]
112
119
  #
113
120
  # @api private
114
121
  # @since 0.2.0
115
- def load!(&configurations)
122
+ def load!(options_map = {}, &configurations)
116
123
  @settings = build_settings
117
- configure(&configurations) if block_given?
124
+ configure(options_map, &configurations)
118
125
  end
119
126
 
120
127
  # @param instructions [Proc]
@@ -76,6 +76,15 @@ module Qonfig
76
76
  __lock__.thread_safe_access { __set_value__(key, value) }
77
77
  end
78
78
 
79
+ # @param options_map [Hash]
80
+ # @return [void]
81
+ #
82
+ # @api private
83
+ # @since 0.3.0
84
+ def __apply_values__(options_map)
85
+ __lock__.thread_safe_access { __set_values_from_map__(options_map) }
86
+ end
87
+
79
88
  # @param keys [Array<String, Symbol>]
80
89
  # @return [Object]
81
90
  #
@@ -114,7 +123,7 @@ module Qonfig
114
123
  def method_missing(method_name, *arguments, &block)
115
124
  super
116
125
  rescue NoMethodError
117
- raise Qonfig::UnknownSettingError, "Setting with <#{method_name}> key doesnt exist!"
126
+ ::Kernel.raise(Qonfig::UnknownSettingError, "Setting with <#{method_name}> key doesnt exist!")
118
127
  end
119
128
 
120
129
  # @return [Boolean]
@@ -151,11 +160,54 @@ module Qonfig
151
160
 
152
161
  private
153
162
 
163
+ # @return [Qonfig::Settings::Lock]
164
+ #
165
+ # @api private
166
+ # @since 0.2.0
167
+ attr_reader :__lock__
168
+
169
+ # @param options_map [Hash]
154
170
  # @return [void]
155
171
  #
172
+ # @raise [Qonfig::ArgumentError]
173
+ # @raise [Qonfig::AmbiguousSettingValueError]
174
+ #
175
+ # @api private
176
+ # @since 0.3.0
177
+ def __set_values_from_map__(options_map)
178
+ ::Kernel.raise(
179
+ Qonfig::ArgumentError, 'Options map should be represented as a hash'
180
+ ) unless options_map.is_a?(Hash)
181
+
182
+ options_map.each_pair do |key, value|
183
+ current_value = __get_value__(key)
184
+
185
+ # NOTE: some duplications here was made only for the better code readability
186
+ case
187
+ when !current_value.is_a?(Qonfig::Settings)
188
+ __set_value__(key, value)
189
+ when current_value.is_a?(Qonfig::Settings) && value.is_a?(Hash)
190
+ current_value.__apply_values__(value)
191
+ when current_value.is_a?(Qonfig::Settings) && !value.is_a?(Hash)
192
+ ::Kernel.raise(
193
+ Qonfig::AmbiguousSettingValueError,
194
+ "Can not redefine option <#{key}> that contains nested options"
195
+ )
196
+ end
197
+ end
198
+ end
199
+
200
+ # @return [void]
201
+ #
202
+ # @raise [Qonfig::FrozenSettingsError]
203
+ #
156
204
  # @api private
157
205
  # @since 0.2.0
158
206
  def __clear_option_values__
207
+ ::Kernel.raise(
208
+ Qonfig::FrozenSettingsError, 'Can not modify frozen settings'
209
+ ) if __options__.frozen?
210
+
159
211
  __options__.each_pair do |key, value|
160
212
  if value.is_a?(Qonfig::Settings)
161
213
  value.__clear__
@@ -176,7 +228,7 @@ module Qonfig
176
228
  key = __indifferently_accessable_option_key__(key)
177
229
 
178
230
  unless __options__.key?(key)
179
- raise Qonfig::UnknownSettingError, "Setting with <#{key}> key does not exist!"
231
+ ::Kernel.raise(Qonfig::UnknownSettingError, "Setting with <#{key}> key does not exist!")
180
232
  end
181
233
 
182
234
  __options__[key]
@@ -196,15 +248,18 @@ module Qonfig
196
248
  key = __indifferently_accessable_option_key__(key)
197
249
 
198
250
  unless __options__.key?(key)
199
- raise Qonfig::UnknownSettingError, "Setting with <#{key}> key does not exist!"
251
+ ::Kernel.raise(Qonfig::UnknownSettingError, "Setting with <#{key}> key does not exist!")
200
252
  end
201
253
 
202
254
  if __options__.frozen?
203
- raise Qonfig::FrozenSettingsError, 'Can not modify frozen settings'
255
+ ::Kernel.raise(Qonfig::FrozenSettingsError, 'Can not modify frozen settings')
204
256
  end
205
257
 
206
258
  if __options__[key].is_a?(Qonfig::Settings)
207
- raise Qonfig::AmbiguousSettingValueError, 'Can not redefine option with nested options'
259
+ ::Kernel.raise(
260
+ Qonfig::AmbiguousSettingValueError,
261
+ "Can not redefine option <#{key}> that contains nested options"
262
+ )
208
263
  end
209
264
 
210
265
  __options__[key] = value
@@ -220,7 +275,7 @@ module Qonfig
220
275
  # @api private
221
276
  # @since 0.2.0
222
277
  def __deep_access__(*keys)
223
- raise Qonfig::ArgumentError, 'Key list can not be empty' if keys.empty?
278
+ ::Kernel.raise(Qonfig::ArgumentError, 'Key list can not be empty') if keys.empty?
224
279
 
225
280
  result = __get_value__(keys.first)
226
281
  rest_keys = Array(keys[1..-1])
@@ -229,18 +284,15 @@ module Qonfig
229
284
  when rest_keys.empty?
230
285
  result
231
286
  when !result.is_a?(Qonfig::Settings)
232
- raise(Qonfig::UnknownSettingError, 'Setting with required digging sequence does not exist!')
287
+ ::Kernel.raise(
288
+ Qonfig::UnknownSettingError,
289
+ 'Setting with required digging sequence does not exist!'
290
+ )
233
291
  when result.is_a?(Qonfig::Settings)
234
292
  result.__dig__(*rest_keys)
235
293
  end
236
294
  end
237
295
 
238
- # @return [Qonfig::Settings::Lock]
239
- #
240
- # @api private
241
- # @since 0.2.0
242
- attr_reader :__lock__
243
-
244
296
  # @param options_part [Hash]
245
297
  # @return [Hash]
246
298
  #
@@ -310,7 +362,7 @@ module Qonfig
310
362
  CORE_METHODS = Array(
311
363
  instance_methods(false) |
312
364
  private_instance_methods(false) |
313
- %i[super raise define_singleton_method]
365
+ %i[super define_singleton_method self]
314
366
  ).map(&:to_s).freeze
315
367
  end
316
368
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Qonfig
4
4
  # @api public
5
- # @since 0.2.0
6
- VERSION = '0.2.0'
5
+ # @since 0.3.0
6
+ VERSION = '0.3.0'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qonfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rustam Ibragimov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-07 00:00:00.000000000 Z
11
+ date: 2018-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coveralls