nxt_config 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99bdd425a2f8fad0172fcb0abed1b1a0c4c4b6c73f689020bd980018f6c9b405
4
- data.tar.gz: 3a61b5deacd727ee4e932b0b82abdc6e31d1bbad3bc29623721f97d33f237996
3
+ metadata.gz: 1b54a945863c828c3a6f04f8c7e2c220b6fb877b42ffb3e7011ad1af98bdbb5c
4
+ data.tar.gz: 858083ef8180ca162cebf589f6eddfe58b41828702a8373677f9f707dd759720
5
5
  SHA512:
6
- metadata.gz: d863dac54820171a4211ef9540510486dbcd5f95a792d0315d50db65a943c9e59b1248c1aac7e5111a73d068440d03049cf60e8773eed47b00c6304f8aab1578
7
- data.tar.gz: '0832012a16cc951d9da8edb459c3d513414718121487df658150e384f87575d925e12e1dd3b83f3fa61a8243776d94282ce85922d7d55de4b52208667af3f656'
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.1.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.0)
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.load_and_constantize`. If you are in a rails application, you can do this in an initializer (e.g. `config/initializers/nxt_config.rb`).
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
- NxtConfig.load_and_constantize(
34
- source: Rails.root.join('config', 'external_api.yml.erb'),
35
- constant_name: :ExternalApiConfig,
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
- This assumes your rails app defines the `MyRailsApp` constant in `config/aplication.rb` and populates the configuration to `MyRailsApp::ExternalApiConfig`. You can also skip the namespace parameter alltogether or pass in any other module that is defined at the time of calling `::load_and_constantize`.
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 #[] calls with symbols
52
- MyRailsApp::ExternalApiConfig[:http][:headers][:user_agent]
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 #[] calls with strings
56
- MyRailsApp::ExternalApiConfig["http"]["headers"]["user_agent"]
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"}>}>
@@ -6,7 +6,15 @@ module NxtConfig
6
6
  hash.freeze
7
7
  end
8
8
 
9
- delegate :[], to: :hash
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) }
@@ -1,3 +1,3 @@
1
1
  module NxtConfig
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
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
- class ConstantNameAlreadyTaken < StandardError; end
10
-
11
- def load_and_constantize(source:, constant_name: nil, namespace: nil)
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 :load_and_constantize
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.1.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-04 00:00:00.000000000 Z
11
+ date: 2020-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport