precursor 0.5.0 → 0.6.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: bba5bce81c5c7e1d955c9f5c0acb4cf3898a5512f8fd66c1378469e6afe38081
4
- data.tar.gz: 41f71f5e7a582aa678c1a62c5e453c31416de9d10706270b69cbae031ba98299
3
+ metadata.gz: 86985495bc3e48757e33837b70e20bd1a431f5e19ce750dd8a38cc6512ed0ffd
4
+ data.tar.gz: fecd720458ce41d39b8f000754500b0ebb9fc586a4cd7bf869657cc3b22391ec
5
5
  SHA512:
6
- metadata.gz: 14d9ed609d3b9d5322fa42da86698b5e6e7eab865c8c190aafc69a10d76f2c47f6be3667281dd9847a1549ec7cb5c41dbaebd3ede2de5e095d3c47e69394c997
7
- data.tar.gz: 4b6ec72589f67c6f701e49ec026a7dfc14208ee0632c5d23d25adff565502e3decd51b98ff37db6cd12544bd039441e532b776f42795ef9f19e04330feafe23e
6
+ metadata.gz: c582c2744224ddfb04b89eddfe0193bbb4b17ff789db6b3a1c56905806f3f769d1bbb3efe9f3f1e9211d1a0693cc262ca915991ea6ec3f5911ffa6df74ae5b41
7
+ data.tar.gz: 01e5f3bade8c2b1c16d7e630b299984a1abd5a752bf60a7ab0b2f2b0726e7fdb14709a5eeb23a5027587b663995fa1891afc4e3654aba3cea0b6dec8158a39c3
data/lib/argv_vault.rb CHANGED
@@ -10,7 +10,9 @@ require_relative 'vault'
10
10
  module Precursor
11
11
  # Vault that parses command line arguments
12
12
  class ArgvVault < Vault
13
- def initialize(argv)
13
+ def initialize(argv, priority:)
14
+ super(priority: priority)
15
+
14
16
  @vault_data = {}
15
17
  @parser = OptionParser.new do |parser|
16
18
  parser.on('-h', '--help', 'Prints this help') do
@@ -34,6 +36,7 @@ module Precursor
34
36
  @vault_data
35
37
  end
36
38
 
39
+ # A builder to configure ArgvVault key options
37
40
  class ArgvKeyBuilder
38
41
  def initialize(key_name)
39
42
  @key_name = key_name
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Precursor
4
+ # A builder to provide Precursor key options
4
5
  class ConfigKeyBuilder
5
6
  def initialize
6
7
  @key_options = {}
data/lib/config_root.rb CHANGED
@@ -1,33 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Precursor
4
+ # Root class to access config vaults
4
5
  class ConfigRoot
5
6
  def initialize(vaults, key_options)
6
- @vaults = vaults
7
+ @vaults = vaults.sort_by(&:priority).reverse
7
8
  @key_options = key_options
8
9
  end
9
10
 
10
11
  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
12
+ key_vault = @vaults.find { |v| v.key? key }
25
13
 
26
- raise KeyError, "key #{key} not found" unless found
14
+ value = key_vault.value key unless key_vault.nil?
15
+ value = get_default(key) if key_vault.nil?
27
16
 
28
- value = resolve_variables(value) if value.is_a? String
17
+ value.is_a?(String) ? resolve_variables(value) : value
18
+ end
29
19
 
30
- value
20
+ def add_vault(vault)
21
+ @vaults = @vaults.push(vault).sort_by(&:priority).reverse
31
22
  end
32
23
 
33
24
  private
@@ -35,6 +26,13 @@ module Precursor
35
26
  VAR_PATTERN = /\$\{(?<var_name>[a-zA-Z]+[\w.]+)\}/
36
27
  MAX_DEPTH = 16
37
28
 
29
+ def get_default(key)
30
+ has_default = @key_options.key?(key) && @key_options[key].key?(:default)
31
+ raise KeyError, "key #{key} not found" unless has_default
32
+
33
+ @key_options[key][:default]
34
+ end
35
+
38
36
  def resolve_variables(str)
39
37
  res = str
40
38
  until (m = res.match(VAR_PATTERN)).nil?
@@ -4,6 +4,7 @@ require_relative 'config_key_builder'
4
4
  require_relative 'config_root'
5
5
 
6
6
  module Precursor
7
+ # A builder to set up Precurcor
7
8
  class ConfigRootBuilder
8
9
  def initialize
9
10
  @vaults = []
data/lib/env_vault.rb CHANGED
@@ -8,13 +8,14 @@ module Precursor
8
8
  # Initializes new instance of [EnvVault]
9
9
  # @param separator [String] separator for hierarchical config entries
10
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 = '__'
11
+ def initialize(priority:, separator: '__', allow_list: [])
12
+ super(priority: priority)
13
+ @separator = separator
13
14
 
14
15
  allow_set = Set.new(allow_list)
15
16
 
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]
17
+ @env_vars = ENV.select { |n, _| allow_set.empty? || allow_set.include?(n) }.transform_keys do |name|
18
+ name.sub(@separator, '.').to_sym
18
19
  end
19
20
  end
20
21
 
data/lib/hash_vault.rb CHANGED
@@ -3,8 +3,10 @@
3
3
  require_relative 'vault'
4
4
 
5
5
  module Precursor
6
+ # Vault that reads config data from passed hash
6
7
  class HashVault < Vault
7
- def initialize(hash)
8
+ def initialize(hash, priority:)
9
+ super(priority: priority)
8
10
  @store = hash
9
11
  end
10
12
 
@@ -3,8 +3,10 @@
3
3
  require_relative 'vault'
4
4
 
5
5
  module Precursor
6
+ # Vault that stores data in hash and allows to override config on the fly
6
7
  class OverrideVault < Vault
7
- def initialize
8
+ def initialize(priority:)
9
+ super(priority: priority)
8
10
  @vault_data = {}
9
11
  end
10
12
 
data/lib/precursor.rb CHANGED
@@ -9,6 +9,7 @@ require_relative 'override_vault'
9
9
  require_relative 'thread_local_override_vault'
10
10
  require_relative 'yaml_vault'
11
11
 
12
+ # Entry point to Precursor
12
13
  module Precursor
13
14
  class << self
14
15
  # Creates a new instance of [Precursor::ConfigRoot]
@@ -3,6 +3,7 @@
3
3
  require_relative 'override_vault'
4
4
 
5
5
  module Precursor
6
+ # Extension of [Precursor::OverrideVault] that stored data in thread-local variable
6
7
  class ThreadLocalOverrideVault < OverrideVault
7
8
  protected
8
9
 
data/lib/vault.rb CHANGED
@@ -1,8 +1,17 @@
1
- # frozen_string_literal
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Precursor
4
4
  # Base class for config vaults
5
5
  class Vault
6
+ # Priority of the vault. Vaults with higher priority are checked first when looking for config value
7
+ attr_reader :priority
8
+
9
+ # Initializes new instance of [Precursor::Vault]
10
+ # @param priority [Integer] the vault priority
11
+ def initialize(priority:)
12
+ @priority = priority
13
+ end
14
+
6
15
  # Returns true if the vault has a value for the given key, otherwise false
7
16
  # @return [Boolean] true if the vault has a value for the given key, otherwise false
8
17
  def key?(key)
@@ -16,6 +25,7 @@ module Precursor
16
25
  end
17
26
 
18
27
  protected
28
+
19
29
  # Replace this method in descendant classes to return Hash-like store
20
30
  def store
21
31
  {}
data/lib/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Precursor
2
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
3
5
  end
data/lib/yaml_vault.rb CHANGED
@@ -5,9 +5,11 @@ require 'yaml'
5
5
  require_relative 'vault'
6
6
 
7
7
  module Precursor
8
+ # Vault that reads config data from a string with Yaml
8
9
  class YamlVault < Vault
9
- def initialize(src)
10
- @yaml = flat_hash(YAML.load(src))
10
+ def initialize(src, priority:)
11
+ super(priority: priority)
12
+ @yaml = flat_hash(YAML.safe_load(src))
11
13
  end
12
14
 
13
15
  protected
@@ -27,7 +29,7 @@ module Precursor
27
29
  { k => v }
28
30
  end
29
31
  end
30
- flat_hash.reduce(:merge).transform_keys { |k| k.to_sym }
32
+ flat_hash.reduce(:merge).transform_keys(&:to_sym)
31
33
  end
32
34
  end
33
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: precursor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Maraev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-12 00:00:00.000000000 Z
11
+ date: 2022-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: optparse
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: code-scanning-rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: debug
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,20 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  description:
42
70
  email:
43
71
  - the_vk@thevk.net
@@ -60,7 +88,8 @@ files:
60
88
  homepage: https://github.com/the-vk/precursor
61
89
  licenses:
62
90
  - MIT
63
- metadata: {}
91
+ metadata:
92
+ rubygems_mfa_required: 'true'
64
93
  post_install_message:
65
94
  rdoc_options: []
66
95
  require_paths: