config_default 0.1.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -1
- data/Gemfile.lock +2 -2
- data/README.md +29 -3
- data/lib/config_default/init.rb +14 -0
- data/lib/config_default/{rails/application/configuration_extension.rb → rails_application_configuration_extension.rb} +1 -3
- data/lib/config_default/{rails/application_extension.rb → rails_application_extension.rb} +1 -3
- data/lib/config_default/struct.rb +29 -9
- data/lib/config_default/version.rb +1 -1
- data/lib/config_default.rb +14 -7
- metadata +6 -12
- 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: b0e496b125200f0a672fbbc01b51ce0f2634f83d742384980a5e77300401de55
|
4
|
+
data.tar.gz: 367ebd46e430509331e964e31a463f237f29f9b73a81967a978a9875d41760bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4463a3895be54cda1a391edf76b78c55a1389fb981b00e6529ce7643ec307fe98ea7acbc778ddb51b4d330046f07c5e345e1ab14a0e409c5c158c5ed0693f919
|
7
|
+
data.tar.gz: ff999b172f7971c4c0f891c8195155b1ac421ae74997e37bb71cb2b06e1e2e5c8d76794d1701b46cc9c8de37999e4a5dab8d85f2c82e13f388ccafde12a0983c
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
## 0.1.2
|
2
|
+
|
3
|
+
- Move config struct option definition from `#method_missing` to `#define_singleton_method`
|
4
|
+
|
5
|
+
## 0.1.1
|
6
|
+
|
7
|
+
- Remove Rails dependency.
|
8
|
+
|
1
9
|
## 0.1.0
|
2
10
|
|
3
|
-
-
|
11
|
+
- Default behaviour.
|
12
|
+
- Option `recursive` to `#load_struct`.
|
13
|
+
- Option `allow_nil` to `#load_struct`.
|
14
|
+
- Specs.
|
15
|
+
- Documentation and examples.
|
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:
|
@@ -33,6 +33,14 @@ ConfigDefault.configure do |config|
|
|
33
33
|
end
|
34
34
|
```
|
35
35
|
|
36
|
+
If you want to implement Rails monkey patches for `Rails.application.config_for` and ability to
|
37
|
+
separate `database.yml` file you need to apply `#init_rails_monkey_patch!` method in your
|
38
|
+
`application.yml` file before application initialization.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
ConfigDefault.init_rails_monkey_patch!
|
42
|
+
```
|
43
|
+
|
36
44
|
## Usage
|
37
45
|
|
38
46
|
### Default behaviour
|
@@ -104,9 +112,9 @@ config = ConfigDefault.load(:app, key: nil) # Will not use key at all and result
|
|
104
112
|
config = ConfigDefault.load(:app, key: "preprod") # Will search preprod key in file
|
105
113
|
```
|
106
114
|
|
107
|
-
###
|
115
|
+
### `#load_struct` method
|
108
116
|
|
109
|
-
If you want to use configuration as
|
117
|
+
If you want to use configuration as a struct object you can use `#load_struct` method.
|
110
118
|
Let's see an example with `database.yaml` config above:
|
111
119
|
|
112
120
|
```ruby
|
@@ -156,6 +164,24 @@ config.first.second.to_hash
|
|
156
164
|
# => { "third" => "option" }
|
157
165
|
```
|
158
166
|
|
167
|
+
### Using `ConfigDefault::Struct` without configuration load
|
168
|
+
|
169
|
+
You can use `ConfigDefault::Struct` to achive ability to create config object from Hash objects.
|
170
|
+
Here an example of creation struct object on the fly:
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
config_on_the_fly = { first: { second: { third: "option" } } }
|
174
|
+
config = ConfigDefault::Struct.new(attributes: config_on_the_fly, recursive: true)
|
175
|
+
config.first.to_hash
|
176
|
+
# => { "second" => { "third" => "option" } }
|
177
|
+
config.first.second.third
|
178
|
+
# => "option"
|
179
|
+
config.first.lolkek
|
180
|
+
# => StandardError: There is no option :lolkek in configuration.
|
181
|
+
```
|
182
|
+
|
183
|
+
`ConfigDefault::Struct` supports `recursive` and `allow_nil` options.
|
184
|
+
|
159
185
|
## Contributing
|
160
186
|
|
161
187
|
Bug reports and pull requests are welcome on GitHub at https://github.com/skirushkin/config_default.
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConfigDefault::Init
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def init_rails_monkey_patch!
|
7
|
+
return unless Object.const_defined?("Rails")
|
8
|
+
return unless Object.const_defined?("Rails::Application")
|
9
|
+
return unless Object.const_defined?("Rails::Application::Configuration")
|
10
|
+
|
11
|
+
Rails::Application.prepend(ConfigDefault::RailsApplicationExtension)
|
12
|
+
Rails::Application::Configuration.prepend(ConfigDefault::RailsApplicationConfigurationExtension)
|
13
|
+
end
|
14
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module ConfigDefault::RailsApplicationConfigurationExtension
|
4
4
|
def load_database_yaml
|
5
5
|
ConfigDefault.load(:database, key: nil)
|
6
6
|
end
|
@@ -9,5 +9,3 @@ module Rails::Application::ConfigurationExtension
|
|
9
9
|
load_database_yaml
|
10
10
|
end
|
11
11
|
end
|
12
|
-
|
13
|
-
Rails::Application::Configuration.prepend(Rails::Application::ConfigurationExtension)
|
@@ -1,10 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
module
|
3
|
+
module ConfigDefault::RailsApplicationExtension
|
4
4
|
def config_for(name, env: Rails.env)
|
5
5
|
data = ConfigDefault.load(name, key: env, deep_symbolize_keys: true)
|
6
6
|
ActiveSupport::OrderedOptions.new.merge(data)
|
7
7
|
end
|
8
8
|
end
|
9
|
-
|
10
|
-
Rails::Application.prepend(Rails::ApplicationExtension)
|
@@ -1,17 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class ConfigDefault::Struct
|
4
|
-
|
4
|
+
RESERVED_METHODS = %i[method_missing respond_to_missing? to_hash].freeze
|
5
|
+
|
6
|
+
def initialize(attributes:, recursive: false, allow_nil: false)
|
5
7
|
@attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes)
|
6
8
|
@allow_nil = allow_nil
|
7
9
|
@recursive = recursive
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
next unless value.is_a?(Hash)
|
12
|
-
@attributes[key] = self.class.new(value, recursive: @recursive, allow_nil: @allow_nil)
|
13
|
-
end
|
14
|
-
end
|
11
|
+
make_recursive!
|
12
|
+
define_methods!
|
15
13
|
|
16
14
|
@attributes.freeze
|
17
15
|
end
|
@@ -21,8 +19,8 @@ class ConfigDefault::Struct
|
|
21
19
|
end
|
22
20
|
|
23
21
|
def method_missing(method, *_args)
|
24
|
-
return
|
25
|
-
raise StandardError.new("There is no option :#{method} in configuration.")
|
22
|
+
return if @allow_nil
|
23
|
+
raise StandardError.new("There is no option :#{method} in configuration.")
|
26
24
|
end
|
27
25
|
|
28
26
|
def respond_to_missing?(*_args)
|
@@ -41,4 +39,26 @@ class ConfigDefault::Struct
|
|
41
39
|
|
42
40
|
dup
|
43
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
|
44
64
|
end
|
data/lib/config_default.rb
CHANGED
@@ -4,10 +4,10 @@ require "active_support/core_ext/hash"
|
|
4
4
|
|
5
5
|
require "config_default/version"
|
6
6
|
require "config_default/config"
|
7
|
+
require "config_default/init"
|
7
8
|
require "config_default/struct"
|
8
|
-
|
9
|
-
require "config_default/
|
10
|
-
require "config_default/rails/application_extension"
|
9
|
+
require "config_default/rails_application_extension"
|
10
|
+
require "config_default/rails_application_configuration_extension"
|
11
11
|
|
12
12
|
module ConfigDefault
|
13
13
|
extend self
|
@@ -20,11 +20,15 @@ module ConfigDefault
|
|
20
20
|
yield(config) if block_given?
|
21
21
|
end
|
22
22
|
|
23
|
+
def init_rails_monkey_patch!
|
24
|
+
ConfigDefault::Init.init_rails_monkey_patch!
|
25
|
+
end
|
26
|
+
|
23
27
|
def load(name, key: Rails.env, symbolize_keys: false, deep_symbolize_keys: false)
|
24
|
-
|
28
|
+
default_config = load_file("#{name}.#{config.postfix}")
|
25
29
|
config = load_file(name)
|
26
30
|
|
27
|
-
data =
|
31
|
+
data = default_config.deep_merge(config)
|
28
32
|
data = key ? data[key] : data
|
29
33
|
|
30
34
|
if deep_symbolize_keys
|
@@ -44,7 +48,10 @@ module ConfigDefault
|
|
44
48
|
end
|
45
49
|
|
46
50
|
def load_struct(name, key: Rails.env, recursive: false, allow_nil: false)
|
47
|
-
|
48
|
-
|
51
|
+
ConfigDefault::Struct.new(
|
52
|
+
attributes: load(name, key: key),
|
53
|
+
recursive: recursive,
|
54
|
+
allow_nil: allow_nil,
|
55
|
+
)
|
49
56
|
end
|
50
57
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_default
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
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-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
@@ -44,17 +44,11 @@ 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
|
-
- lib/config_default/
|
57
|
-
- lib/config_default/
|
49
|
+
- lib/config_default/init.rb
|
50
|
+
- lib/config_default/rails_application_configuration_extension.rb
|
51
|
+
- lib/config_default/rails_application_extension.rb
|
58
52
|
- lib/config_default/struct.rb
|
59
53
|
- lib/config_default/version.rb
|
60
54
|
homepage: https://github.com/skirushkin/config_default
|
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