nxt_config 0.1.0 → 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 +2 -2
- data/README.md +14 -11
- data/lib/nxt_config/struct.rb +10 -2
- data/lib/nxt_config/version.rb +1 -1
- data/lib/nxt_config.rb +4 -18
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b54a945863c828c3a6f04f8c7e2c220b6fb877b42ffb3e7011ad1af98bdbb5c
|
4
|
+
data.tar.gz: 858083ef8180ca162cebf589f6eddfe58b41828702a8373677f9f707dd759720
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfeafb100eb0585ca72806a956f5edd7894faa78a9f924debb2b2aae891bab40ee620be470ce9e87393c7ebdbb033b88c82f39b51711d0509686ec65c1d8bc10
|
7
|
+
data.tar.gz: 749ac16ca1fe736f6208eb9a37670c08ba2b51b0c8b6370c2ddaa343b481e9d22c9a4c8699ea2c1853dffdeb33577b1fb8a11b6ca9ff3cb8d4f7d27a3d15970e
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nxt_config (0.
|
4
|
+
nxt_config (0.2.0)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
@@ -15,7 +15,7 @@ GEM
|
|
15
15
|
zeitwerk (~> 2.2)
|
16
16
|
concurrent-ruby (1.1.5)
|
17
17
|
diff-lcs (1.3)
|
18
|
-
i18n (1.7.
|
18
|
+
i18n (1.7.1)
|
19
19
|
concurrent-ruby (~> 1.0)
|
20
20
|
minitest (5.13.0)
|
21
21
|
rake (12.3.3)
|
data/README.md
CHANGED
@@ -27,17 +27,15 @@ Or install it yourself as:
|
|
27
27
|
|
28
28
|
## Usage
|
29
29
|
|
30
|
-
You can load YAML files and populate their content into a configuration object using `NxtConfig.
|
30
|
+
You can load YAML files and populate their content into a configuration object using `NxtConfig.load`. If you are in a rails application, you can do this in an initializer (e.g. `config/initializers/nxt_config.rb`).
|
31
31
|
|
32
32
|
```ruby
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
namespace: MyRailsApp
|
37
|
-
)
|
33
|
+
module MyRailsApp
|
34
|
+
ExternalApiConfig = NxtConfig.load Rails.root.join('config', 'external_api.yml.erb')
|
35
|
+
end
|
38
36
|
```
|
39
37
|
|
40
|
-
|
38
|
+
Of course you can also load configuration structs everywhere else in the application. Depending on where you assign it to a constant, you can have many configuration structs available via constants namespaced all over your application, scoped to the context where you need them.
|
41
39
|
|
42
40
|
```ruby
|
43
41
|
# Use struct like method chaining to access nested data
|
@@ -48,14 +46,19 @@ MyRailsApp::ExternalApiConfig.http.headers.user_agent
|
|
48
46
|
MyRailsApp::ExternalApiConfig.non_existent_key
|
49
47
|
# => raises NoMethodError
|
50
48
|
|
51
|
-
# You can also use hash like #
|
52
|
-
MyRailsApp::ExternalApiConfig
|
49
|
+
# You can also use hash like #fetch calls with symbols (multiple ones, like with Hash#dig)
|
50
|
+
MyRailsApp::ExternalApiConfig.fetch(:http, :headers, :user_agent)
|
53
51
|
# => "MyRailsApp 1.0.0"
|
54
52
|
|
55
|
-
# You can also use hash like #
|
56
|
-
MyRailsApp::ExternalApiConfig
|
53
|
+
# You can also use hash like #fetch calls with strings (multiple ones, like with Hash#dig)
|
54
|
+
MyRailsApp::ExternalApiConfig.fetch("http", "headers", "user_agent")
|
57
55
|
# => "MyRailsApp 1.0.0"
|
58
56
|
|
57
|
+
MyRailsApp::ExternalApiConfig.fetch(:http, :oh_no, :user_agent, &Proc.new { 'Hy!' })
|
58
|
+
# => "Hy!"
|
59
|
+
|
60
|
+
# You can also pass a block to #fetch in case a key does not exist
|
61
|
+
|
59
62
|
# If you don't walk through the struct until its leaves, you will get a sub struct
|
60
63
|
MyRailsApp::ExternalApiConfig.http
|
61
64
|
# => #<NxtConfig::Struct:0x00007fe657467680 @hash={"headers"=>#<NxtConfig::Struct:0x00007fe657467518 @hash={"user_agent"=>"my cool app", "api_key"=>"secret123"}>}>
|
data/lib/nxt_config/struct.rb
CHANGED
@@ -6,7 +6,15 @@ module NxtConfig
|
|
6
6
|
hash.freeze
|
7
7
|
end
|
8
8
|
|
9
|
-
|
9
|
+
def fetch(*keys, &block)
|
10
|
+
if keys.length == 0
|
11
|
+
raise ArgumentError, "Provide at least one key"
|
12
|
+
elsif keys.length == 1
|
13
|
+
hash.fetch(keys.first, &block)
|
14
|
+
else
|
15
|
+
hash.fetch(keys.first, &block).fetch(*keys[1..-1], &block)
|
16
|
+
end
|
17
|
+
end
|
10
18
|
|
11
19
|
private
|
12
20
|
|
@@ -19,7 +27,7 @@ module NxtConfig
|
|
19
27
|
end
|
20
28
|
|
21
29
|
def transform_hash_value(value)
|
22
|
-
if value.is_a?(ActiveSupport::HashWithIndifferentAccess)
|
30
|
+
if value.is_a?(ActiveSupport::HashWithIndifferentAccess) || value.is_a?(Hash)
|
23
31
|
Struct.new(value)
|
24
32
|
elsif value.is_a?(Array)
|
25
33
|
value.map { |item| transform_hash_value(item) }
|
data/lib/nxt_config/version.rb
CHANGED
data/lib/nxt_config.rb
CHANGED
@@ -6,24 +6,10 @@ require "nxt_config/struct"
|
|
6
6
|
require "nxt_config/version"
|
7
7
|
|
8
8
|
module NxtConfig
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
source_hash = YAML.safe_load(ERB.new(File.read(source)).result)
|
13
|
-
struct = Struct.new(source_hash)
|
14
|
-
namespace_constant = namespace || Object
|
15
|
-
|
16
|
-
begin
|
17
|
-
if namespace_constant.const_get(constant_name)
|
18
|
-
raise ConstantNameAlreadyTaken,
|
19
|
-
"Cannot define constant #{constant_name} on #{namespace_constant.name} because it already exists."
|
20
|
-
end
|
21
|
-
rescue NameError
|
22
|
-
# If the constant does not exist => fine, let's set it!
|
23
|
-
end
|
24
|
-
|
25
|
-
namespace_constant.const_set(constant_name, struct)
|
9
|
+
def load(filename)
|
10
|
+
source_hash = YAML.safe_load(ERB.new(File.read(filename)).result)
|
11
|
+
Struct.new(source_hash)
|
26
12
|
end
|
27
13
|
|
28
|
-
module_function :
|
14
|
+
module_function :load
|
29
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nxt_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nils Sommer
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|