smart_config 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2916bd8a506f8c74af2f8b00729d7ba04241159b7ea993090b0010b82657b62e
4
+ data.tar.gz: 55d1eadaf37c20f76de2ff01e21b23aba1448eedae035404c739d0a7b2d74cff
5
+ SHA512:
6
+ metadata.gz: 04f2a8e1094f315d3afe3e47d0dc42fbd305d079abd11024b0f8b2762c4f0154f23a5deda724c6223b4b08cd784f0e4836777354595f401102cc59d573a0276e
7
+ data.tar.gz: 59ee036cb4a4253af357017db22662e01c6765d589282939e2638e422e303f5e97213ee91395fe009e9b1a11f8e795c15f86adb11130a59d862f6c08a65e3917
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Damien MATHIEU
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Smart Config
2
+
3
+ We have "smart" toothbrushes these days, so why not smart configuration?
4
+
5
+ **Note**: As you can see in the version number, this gem is still in a very early version, and you should expect that there may be breaking changes until we reach 1.0.
6
+
7
+ ## Usage
8
+
9
+ Smart Config allows you to define a static configuration and access it from anywhere within your application.
10
+ It will try to read the configuration from a YAML file, and fallback on environment variables.
11
+
12
+ ### Installation
13
+
14
+ Add the `smart_gem` dependency to you Gemfile:
15
+
16
+ ```
17
+ gem 'smart_gem'
18
+ ```
19
+
20
+ Then, create a new config class that, and define the configuration you need:
21
+
22
+ ```
23
+ class Config
24
+ extend SmartConfig::Config
25
+
26
+ # Optional. Will default to `config/config.yml`
27
+ config_path 'config/app_config.yml'
28
+
29
+ value :app_name
30
+
31
+ group :smtp do
32
+ value :hostname
33
+ value :port
34
+ value :username
35
+ value :password
36
+ end
37
+
38
+ group :redis do
39
+ group :connection do
40
+ value :hostname
41
+ value :port
42
+ value :username
43
+ value :password
44
+ end
45
+
46
+ value :timeout
47
+ end
48
+ end
49
+ ```
50
+
51
+ Then, within your application, you can call:
52
+
53
+ ```
54
+ Config.redis.connection.hostname
55
+ ```
56
+
57
+ To access the configuration value.
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module SmartConfig
6
+ #
7
+ # Allows creating static configurations that rely on YAML files, or fall back
8
+ # to environment variables
9
+ #
10
+ module Config
11
+ include Values
12
+
13
+ def config_path(path)
14
+ @config_path = path
15
+ end
16
+
17
+ private
18
+
19
+ def config_file_path
20
+ return 'config/config.yaml' if @config_path.nil?
21
+
22
+ @config_path
23
+ end
24
+
25
+ def data
26
+ @data ||= [
27
+ YAML.load_file(config_file_path),
28
+ ENV.to_h.transform_keys(&:downcase)
29
+ ]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module SmartConfig
6
+ #
7
+ # Handles recursive subgroups of configurations
8
+ #
9
+ class Group
10
+ include SmartConfig::Values
11
+
12
+ def initialize(name, parent, &)
13
+ @name = name
14
+ @parent = parent
15
+ instance_exec(&)
16
+ end
17
+
18
+ private
19
+
20
+ def data
21
+ @data ||= @parent.get_path(@name)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module SmartConfig
6
+ #
7
+ # Handles the retrieval of values, but with already provided data.
8
+ #
9
+ module Values
10
+ def value(name)
11
+ @config ||= {}
12
+ @config[name.to_sym] = {}
13
+ end
14
+
15
+ def group(name, &)
16
+ @config ||= {}
17
+ @config[name.to_sym] = SmartConfig::Group.new(name, self, &)
18
+ end
19
+
20
+ def keys
21
+ @config.keys
22
+ end
23
+
24
+ def method_missing(name, *args, &)
25
+ return get_value(name) if keys.include?(name)
26
+
27
+ super
28
+ end
29
+
30
+ def respond_to_missing?(name, include_private = false)
31
+ return true if keys.include?(name)
32
+
33
+ super
34
+ end
35
+
36
+ def get_value(name)
37
+ path = get_path(name)
38
+
39
+ case @config[name]
40
+ when SmartConfig::Group
41
+ @config[name]
42
+ else
43
+ path.first
44
+ end
45
+ end
46
+
47
+ def get_path(name)
48
+ name = name.to_s
49
+ data.map { |a| a[name] }.compact
50
+ end
51
+
52
+ private
53
+
54
+ # Getting the data needs to be implemented by a lateral module
55
+ def data
56
+ raise NotImplementedError
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SmartConfig
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # The main SmartConfig module. See documentation for the sub modules.
5
+ #
6
+ module SmartConfig; end
7
+
8
+ require 'smart_config/values'
9
+ require 'smart_config/group'
10
+ require 'smart_config/config'
11
+ require 'smart_config/version'
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_config
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Damien MATHIEU
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Define and access static configuration, from a YAML config file, or environment
14
+ variables
15
+ email:
16
+ - 42@dmathieu.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - lib/smart_config.rb
24
+ - lib/smart_config/config.rb
25
+ - lib/smart_config/group.rb
26
+ - lib/smart_config/values.rb
27
+ - lib/smart_config/version.rb
28
+ homepage: https://github.com/dmathieu/smart_config
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ rubygems_mfa_required: 'true'
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '3.0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.5.6
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: A DLS for reading and accessing static configuration
52
+ test_files: []