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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +20 -2
- data/lib/config_default/struct.rb +27 -13
- data/lib/config_default/version.rb +1 -1
- data/lib/config_default.rb +7 -4
- metadata +2 -9
- data/config/database.default.yml +0 -2
- data/config/database.yml +0 -2
- data/config/example1.default.yml +0 -3
- data/config/example1.yml +0 -2
- data/config/example2.default.yml +0 -2
- data/config/example3.yml +0 -2
- data/config/nested.yml +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f4cf3d6077b01b84dd411087f261e2bbd05d60e94c44dbb56533c67a28cbf44
|
4
|
+
data.tar.gz: ca3206dbcc7f82a9f1d37049f7106a51cba979ef735ad2b28393c3452152488f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7891a43cdf5152d15524e240a0d89ef33333073d4e8514310d49f58b134b14f34f18306882d653f11a4032b968977f73081657ae806dcead0876e19f6c1d09e7
|
7
|
+
data.tar.gz: 6ca0ce6cd318011478cffda1ac799d93587b0fc341ed06443fac793f3f756e207f0d72679ec76725c0b845a28a3e6bc021d6696759ee2018cdc32f512627425f
|
data/Gemfile.lock
CHANGED
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
|
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
|
-
###
|
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
|
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
|
-
|
12
|
-
|
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
|
-
|
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
|
data/lib/config_default.rb
CHANGED
@@ -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
|
-
|
22
|
+
default_config = load_file("#{name}.#{config.postfix}")
|
23
23
|
config = load_file(name)
|
24
24
|
|
25
|
-
data =
|
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
|
-
|
46
|
-
|
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.
|
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-
|
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
|
data/config/database.default.yml
DELETED
data/config/database.yml
DELETED
data/config/example1.default.yml
DELETED
data/config/example1.yml
DELETED
data/config/example2.default.yml
DELETED
data/config/example3.yml
DELETED
data/config/nested.yml
DELETED