config_default 0.1.2 → 0.2.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: e7e18b5eb5ba11e06e39b8ecc08aa74d98a32dc0bfe36caa787978224f1aca90
4
- data.tar.gz: fee80751ea9a44bda3acbbb29d9714f287c6055f6c52c241251d7d8b42440df0
3
+ metadata.gz: 0f4cf3d6077b01b84dd411087f261e2bbd05d60e94c44dbb56533c67a28cbf44
4
+ data.tar.gz: ca3206dbcc7f82a9f1d37049f7106a51cba979ef735ad2b28393c3452152488f
5
5
  SHA512:
6
- metadata.gz: d6da506d9904ebaf20ffd47fb3d9e0aeef689fa3bb0e39950670f7ecc2fc24f22baf064ab38b23ab7f1ed5a88a6286a6d2aba0abd22291e84229a2f905d92f28
7
- data.tar.gz: 444e8ddfdcee3d3ba48bbe8e3b19b83fde496cc9429b68d8df973de971f6994106d6b2dde311f795f02da2a8d0ef7a743a0ae07ccc25e651ccdf1cf5b4334df8
6
+ metadata.gz: 7891a43cdf5152d15524e240a0d89ef33333073d4e8514310d49f58b134b14f34f18306882d653f11a4032b968977f73081657ae806dcead0876e19f6c1d09e7
7
+ data.tar.gz: 6ca0ce6cd318011478cffda1ac799d93587b0fc341ed06443fac793f3f756e207f0d72679ec76725c0b845a28a3e6bc021d6696759ee2018cdc32f512627425f
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- config_default (0.1.2)
4
+ config_default (0.2.0)
5
5
  activesupport (~> 6)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -9,7 +9,7 @@ Maybe in future I'll remove Rails dependency (cause it's never cool) and leave o
9
9
  Add this line to your application's Gemfile:
10
10
 
11
11
  ```ruby
12
- gem 'config_default'
12
+ gem "config_default"
13
13
  ```
14
14
 
15
15
  And then execute:
@@ -104,7 +104,7 @@ config = ConfigDefault.load(:app, key: nil) # Will not use key at all and result
104
104
  config = ConfigDefault.load(:app, key: "preprod") # Will search preprod key in file
105
105
  ```
106
106
 
107
- ### Struct using
107
+ ### `#load_struct` method
108
108
 
109
109
  If you want to use configuration as a struct object you can use `#load_struct` method.
110
110
  Let's see an example with `database.yaml` config above:
@@ -156,6 +156,24 @@ config.first.second.to_hash
156
156
  # => { "third" => "option" }
157
157
  ```
158
158
 
159
+ ### Using `ConfigDefault::Struct` without configuration load
160
+
161
+ You can use `ConfigDefault::Struct` to achive ability to create config object from Hash objects.
162
+ Here an example of creation struct object on the fly:
163
+
164
+ ```ruby
165
+ config_on_the_fly = { first: { second: { third: "option" } } }
166
+ config = ConfigDefault::Struct.new(attributes: config_on_the_fly, recursive: true)
167
+ config.first.to_hash
168
+ # => { "second" => { "third" => "option" } }
169
+ config.first.second.third
170
+ # => "option"
171
+ config.first.lolkek
172
+ # => StandardError: There is no option :lolkek in configuration.
173
+ ```
174
+
175
+ `ConfigDefault::Struct` supports `recursive` and `allow_nil` options.
176
+
159
177
  ## Contributing
160
178
 
161
179
  Bug reports and pull requests are welcome on GitHub at https://github.com/skirushkin/config_default.
@@ -3,22 +3,13 @@
3
3
  class ConfigDefault::Struct
4
4
  RESERVED_METHODS = %i[method_missing respond_to_missing? to_hash].freeze
5
5
 
6
- def initialize(attributes = {}, recursive: false, allow_nil: false)
6
+ def initialize(attributes:, recursive: false, allow_nil: false)
7
7
  @attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes)
8
8
  @allow_nil = allow_nil
9
9
  @recursive = recursive
10
10
 
11
- if @recursive
12
- @attributes.each do |key, value|
13
- next unless value.is_a?(Hash)
14
- @attributes[key] = self.class.new(value, recursive: @recursive, allow_nil: @allow_nil)
15
- end
16
- end
17
-
18
- @attributes.each do |key, value|
19
- next if RESERVED_METHODS.include?(key.to_sym)
20
- define_singleton_method(key) { value }
21
- end
11
+ make_recursive!
12
+ define_methods!
22
13
 
23
14
  @attributes.freeze
24
15
  end
@@ -28,7 +19,8 @@ class ConfigDefault::Struct
28
19
  end
29
20
 
30
21
  def method_missing(method, *_args)
31
- raise StandardError.new("There is no option :#{method} in configuration.") unless @allow_nil
22
+ return if @allow_nil
23
+ raise StandardError.new("There is no option :#{method} in configuration.")
32
24
  end
33
25
 
34
26
  def respond_to_missing?(*_args)
@@ -47,4 +39,26 @@ class ConfigDefault::Struct
47
39
 
48
40
  dup
49
41
  end
42
+
43
+ private
44
+
45
+ def make_recursive!
46
+ return unless @recursive
47
+
48
+ @attributes.each do |key, value|
49
+ next unless value.is_a?(Hash)
50
+ @attributes[key] = self.class.new(
51
+ attributes: value,
52
+ recursive: @recursive,
53
+ allow_nil: @allow_nil,
54
+ )
55
+ end
56
+ end
57
+
58
+ def define_methods!
59
+ @attributes.each do |key, value|
60
+ next if RESERVED_METHODS.include?(key.to_sym)
61
+ define_singleton_method(key) { value }
62
+ end
63
+ end
50
64
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ConfigDefault
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -19,10 +19,10 @@ module ConfigDefault
19
19
  end
20
20
 
21
21
  def load(name, key: Rails.env, symbolize_keys: false, deep_symbolize_keys: false)
22
- base_config = load_file("#{name}.#{config.postfix}")
22
+ default_config = load_file("#{name}.#{config.postfix}")
23
23
  config = load_file(name)
24
24
 
25
- data = base_config.deep_merge(config)
25
+ data = default_config.deep_merge(config)
26
26
  data = key ? data[key] : data
27
27
 
28
28
  if deep_symbolize_keys
@@ -42,8 +42,11 @@ module ConfigDefault
42
42
  end
43
43
 
44
44
  def load_struct(name, key: Rails.env, recursive: false, allow_nil: false)
45
- attributes = load(name, key: key)
46
- ConfigDefault::Struct.new(attributes, recursive: recursive, allow_nil: allow_nil)
45
+ ConfigDefault::Struct.new(
46
+ attributes: load(name, key: key),
47
+ recursive: recursive,
48
+ allow_nil: allow_nil,
49
+ )
47
50
  end
48
51
  end
49
52
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: config_default
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stepan Kirushkin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-20 00:00:00.000000000 Z
11
+ date: 2021-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -44,13 +44,6 @@ files:
44
44
  - Rakefile
45
45
  - bin/console
46
46
  - bin/setup
47
- - config/database.default.yml
48
- - config/database.yml
49
- - config/example1.default.yml
50
- - config/example1.yml
51
- - config/example2.default.yml
52
- - config/example3.yml
53
- - config/nested.yml
54
47
  - lib/config_default.rb
55
48
  - lib/config_default/config.rb
56
49
  - lib/config_default/rails/application/configuration_extension.rb
@@ -1,2 +0,0 @@
1
- development:
2
- host: localhost
data/config/database.yml DELETED
@@ -1,2 +0,0 @@
1
- development:
2
- database: example
@@ -1,3 +0,0 @@
1
- development:
2
- first: one
3
- second: two
data/config/example1.yml DELETED
@@ -1,2 +0,0 @@
1
- development:
2
- second: example
@@ -1,2 +0,0 @@
1
- development:
2
- first: one
data/config/example3.yml DELETED
@@ -1,2 +0,0 @@
1
- development:
2
- second: two
data/config/nested.yml DELETED
@@ -1,4 +0,0 @@
1
- development:
2
- first:
3
- second:
4
- third: "three"