precursor 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dbe652293d6dbc1fe13cd17330f59a9e37e36d89fa2ff60c4af4bba81d80598f
4
+ data.tar.gz: b98e83f130cf813c408c2fc416245f5a3b8cc16d58c17b0b9229db1c4c034f55
5
+ SHA512:
6
+ metadata.gz: 9b30d56c17c3bf072687cce95343b49853c4ba57f90632694070c864adb7a5a1d2549556a2e086a843af554b1c1617c254cfa68c87b2df931a2689d8234d455c
7
+ data.tar.gz: 45f98b6fd91306ef418438968a98d4f2ad83a24160f31644f803c9cacad6d56ddb0aaf583cae4ec83436ebec921d810104a3bdc43af0a940c59077bc071a4b29
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Precursor
4
+ class ConfigKeyBuilder
5
+ def initialize
6
+ @key_options = {}
7
+ end
8
+
9
+ def default(value)
10
+ @key_options[:default] = value
11
+ end
12
+
13
+ def build
14
+ @key_options
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Precursor
4
+ class ConfigRoot
5
+ def initialize(vaults, key_options)
6
+ @vaults = vaults
7
+ @key_options = key_options
8
+ end
9
+
10
+ def [](key)
11
+ found = false
12
+ value = nil
13
+ @vaults.each do |v|
14
+ if v.key? key
15
+ found = true
16
+ value = v.value key
17
+ break
18
+ end
19
+ end
20
+
21
+ if !found && @key_options.key?(key) && @key_options[key].key?(:default)
22
+ found = true
23
+ value = @key_options[key][:default]
24
+ end
25
+
26
+ raise KeyError, "key #{key} not found" unless found
27
+ value
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'config_key_builder'
4
+ require_relative 'config_root'
5
+
6
+ module Precursor
7
+ class ConfigRootBuilder
8
+ def initialize
9
+ @vaults = []
10
+ @key_builders = {}
11
+ end
12
+
13
+ def vault(vault)
14
+ @vaults.push(vault)
15
+ end
16
+
17
+ def key(key_name)
18
+ builder = ConfigKeyBuilder.new
19
+ @key_builders[key_name] = builder
20
+ yield builder
21
+ end
22
+
23
+ def build
24
+ key_options = @key_builders.transform_values(&:build)
25
+ ConfigRoot.new(@vaults, key_options)
26
+ end
27
+ end
28
+ end
data/lib/env_vault.rb ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'vault'
4
+
5
+ module Precursor
6
+ # Vault that provides config values from environment variables
7
+ class EnvVault < Vault
8
+ # Initializes new instance of [EnvVault]
9
+ # @param separator [String] separator for hierarchical config entries
10
+ # @param allow_list [Array<String>] list of allowed env vars to prevent leaking of sensitive or unrelated vars
11
+ def initialize(separator: '__', allow_list: [])
12
+ @separator = '__'
13
+
14
+ allow_set = Set.new(allow_list)
15
+
16
+ @env_vars = ENV.select { |n, _| allow_set.empty? || allow_set.include?(n) }.to_h do |name, value|
17
+ [name.sub(@separator, '.').to_sym, value]
18
+ end
19
+ end
20
+
21
+ def key?(key)
22
+ @env_vars.key? key
23
+ end
24
+
25
+ def value(key)
26
+ @env_vars[key]
27
+ end
28
+ end
29
+ end
data/lib/precursor.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'config_root_builder'
4
+
5
+ require_relative 'env_vault'
6
+ require_relative 'yaml_vault'
7
+
8
+ module Precursor
9
+ class << self
10
+ # Creates a new instance of [Precursor::ConfigRoot]
11
+ def create
12
+ builder = ConfigRootBuilder.new
13
+ yield builder
14
+ builder.build
15
+ end
16
+ end
17
+ end
data/lib/vault.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal
2
+
3
+ module Precursor
4
+ # Base class for config vaults
5
+ class Vault
6
+ # Returns true if the vault has a value for the given key, otherwise false
7
+ # @return [Boolean] true if the vault has a value for the given key, otherwise false
8
+ def key?
9
+ false
10
+ end
11
+
12
+ # Returns a value fot the given key, nil if key is not available
13
+ # @return [Object|Nil] a value for the given key
14
+ def value(key)
15
+ nil
16
+ end
17
+ end
18
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Precursor
2
+ VERSION = '0.3.0'
3
+ end
data/lib/yaml_vault.rb ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ require_relative 'vault'
6
+
7
+ module Precursor
8
+ class YamlVault < Vault
9
+ def initialize(src)
10
+ @yaml = flat_hash(YAML.load(src))
11
+ end
12
+
13
+ def key?(key)
14
+ @yaml.key? key
15
+ end
16
+
17
+ def value(key)
18
+ @yaml[key]
19
+ end
20
+
21
+ private
22
+
23
+ def flat_hash(hash, prefix = nil)
24
+ flat_hash = hash.flat_map do |k, v|
25
+ k = "#{prefix}.#{k}" unless prefix.nil?
26
+ if v.is_a? Hash
27
+ flat_hash(v, k)
28
+ else
29
+ { k => v }
30
+ end
31
+ end
32
+ flat_hash.reduce(:merge).transform_keys { |k| k.to_sym }
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: precursor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Maraev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: debug
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ - the_vk@thevk.net
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/config_key_builder.rb
35
+ - lib/config_root.rb
36
+ - lib/config_root_builder.rb
37
+ - lib/env_vault.rb
38
+ - lib/precursor.rb
39
+ - lib/vault.rb
40
+ - lib/version.rb
41
+ - lib/yaml_vault.rb
42
+ homepage: https://github.com/the-vk/precursor
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.3.3
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Extensible config system
65
+ test_files: []