precursor 0.4.0 → 0.5.1

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: e106154712ad13288b622f2c6fcdded80b583a68387c259f136d1b0c448fdfe0
4
- data.tar.gz: c99dc664e48950a6d6de38a0bd7f56c257f505d6065337f57c7d6021adc086e1
3
+ metadata.gz: 0e3304cac9ee21759234bd745095460b9dc71573f4cb6a0f54ba8ca7862c0b0a
4
+ data.tar.gz: b1995f5395d1b767e08d61db569c40995b277f69f4b516646fed026a000acdb6
5
5
  SHA512:
6
- metadata.gz: 405695b440d4202e2c1a0678a92d5f17bb63fe9404774cf0f9d33dfff20c6c2c64322cfccd90f61bb06894325ea4edd3633dc08d71698459950b5aa37827cd08
7
- data.tar.gz: 7b843d26c18872d5174ae6fef796ffaf63c2deaeec4726d0948a5a2a18181748942757066949e9633edaa78657b11fc69bf14baf0b6db9d4c624615ddee802b9
6
+ metadata.gz: 4b3bb038529d96de309c2cf16e7acf1918310944612bd002dfa272e612c2a584c42fdea929cfe1a06f19999dc7b01b4f805c607891cbfb2621eebdd11a3724b6
7
+ data.tar.gz: d73549d136b50202dd4b222a4e0145851ba78927f3677e9ff55259057a8599018e1494cee40941c7ceafdff745d79f41447aad01f7d7b13c2d1867bad7fc0a6d
data/lib/argv_vault.rb CHANGED
@@ -11,6 +11,8 @@ module Precursor
11
11
  # Vault that parses command line arguments
12
12
  class ArgvVault < Vault
13
13
  def initialize(argv)
14
+ super()
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,6 +1,7 @@
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
7
  @vaults = vaults
@@ -8,23 +9,34 @@ module Precursor
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
12
+ key_vault = @vaults.find { |v| v.key? key }
20
13
 
21
- if !found && @key_options.key?(key) && @key_options[key].key?(:default)
22
- found = true
23
- value = @key_options[key][:default]
24
- end
14
+ value = key_vault.value key unless key_vault.nil?
15
+ value = get_default(key) if key_vault.nil?
16
+
17
+ value.is_a?(String) ? resolve_variables(value) : value
18
+ end
19
+
20
+ private
25
21
 
26
- raise KeyError, "key #{key} not found" unless found
27
- value
22
+ VAR_PATTERN = /\$\{(?<var_name>[a-zA-Z]+[\w.]+)\}/
23
+ MAX_DEPTH = 16
24
+
25
+ def get_default(key)
26
+ has_default = @key_options.key?(key) && @key_options[key].key?(:default)
27
+ raise KeyError, "key #{key} not found" unless has_default
28
+
29
+ @key_options[key][:default]
30
+ end
31
+
32
+ def resolve_variables(str)
33
+ res = str
34
+ until (m = res.match(VAR_PATTERN)).nil?
35
+ k = m[1]
36
+ v = self[k.to_sym]
37
+ res = res.sub("${#{k}}", v.to_s)
38
+ end
39
+ res
28
40
  end
29
41
  end
30
42
  end
@@ -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
@@ -9,12 +9,13 @@ module Precursor
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
11
  def initialize(separator: '__', allow_list: [])
12
- @separator = '__'
12
+ super()
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 ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'vault'
4
+
5
+ module Precursor
6
+ # Vault that reads config data from passed hash
7
+ class HashVault < Vault
8
+ def initialize(hash)
9
+ super()
10
+ @store = hash
11
+ end
12
+
13
+ protected
14
+
15
+ attr_reader :store
16
+ end
17
+ end
@@ -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
8
  def initialize
9
+ super()
8
10
  @vault_data = {}
9
11
  end
10
12
 
data/lib/precursor.rb CHANGED
@@ -4,10 +4,12 @@ require_relative 'config_root_builder'
4
4
 
5
5
  require_relative 'argv_vault'
6
6
  require_relative 'env_vault'
7
+ require_relative 'hash_vault'
7
8
  require_relative 'override_vault'
8
9
  require_relative 'thread_local_override_vault'
9
10
  require_relative 'yaml_vault'
10
11
 
12
+ # Entry point to Precursor
11
13
  module Precursor
12
14
  class << self
13
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,4 +1,4 @@
1
- # frozen_string_literal
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Precursor
4
4
  # Base class for config vaults
@@ -16,6 +16,7 @@ module Precursor
16
16
  end
17
17
 
18
18
  protected
19
+
19
20
  # Replace this method in descendant classes to return Hash-like store
20
21
  def store
21
22
  {}
data/lib/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Precursor
2
- VERSION = '0.4.0'
4
+ VERSION = '0.5.1'
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
10
  def initialize(src)
10
- @yaml = flat_hash(YAML.load(src))
11
+ super()
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.4.0
4
+ version: 0.5.1
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-08 00:00:00.000000000 Z
11
+ date: 2022-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: optparse
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description:
42
56
  email:
43
57
  - the_vk@thevk.net
@@ -50,6 +64,7 @@ files:
50
64
  - lib/config_root.rb
51
65
  - lib/config_root_builder.rb
52
66
  - lib/env_vault.rb
67
+ - lib/hash_vault.rb
53
68
  - lib/override_vault.rb
54
69
  - lib/precursor.rb
55
70
  - lib/thread_local_override_vault.rb
@@ -59,7 +74,8 @@ files:
59
74
  homepage: https://github.com/the-vk/precursor
60
75
  licenses:
61
76
  - MIT
62
- metadata: {}
77
+ metadata:
78
+ rubygems_mfa_required: 'true'
63
79
  post_install_message:
64
80
  rdoc_options: []
65
81
  require_paths: