precursor 0.5.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: bba5bce81c5c7e1d955c9f5c0acb4cf3898a5512f8fd66c1378469e6afe38081
4
- data.tar.gz: 41f71f5e7a582aa678c1a62c5e453c31416de9d10706270b69cbae031ba98299
3
+ metadata.gz: 0e3304cac9ee21759234bd745095460b9dc71573f4cb6a0f54ba8ca7862c0b0a
4
+ data.tar.gz: b1995f5395d1b767e08d61db569c40995b277f69f4b516646fed026a000acdb6
5
5
  SHA512:
6
- metadata.gz: 14d9ed609d3b9d5322fa42da86698b5e6e7eab865c8c190aafc69a10d76f2c47f6be3667281dd9847a1549ec7cb5c41dbaebd3ede2de5e095d3c47e69394c997
7
- data.tar.gz: 4b6ec72589f67c6f701e49ec026a7dfc14208ee0632c5d23d25adff565502e3decd51b98ff37db6cd12544bd039441e532b776f42795ef9f19e04330feafe23e
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,26 +9,12 @@ 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
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
12
+ key_vault = @vaults.find { |v| v.key? key }
27
13
 
28
- value = resolve_variables(value) if value.is_a? String
14
+ value = key_vault.value key unless key_vault.nil?
15
+ value = get_default(key) if key_vault.nil?
29
16
 
30
- value
17
+ value.is_a?(String) ? resolve_variables(value) : value
31
18
  end
32
19
 
33
20
  private
@@ -35,6 +22,13 @@ module Precursor
35
22
  VAR_PATTERN = /\$\{(?<var_name>[a-zA-Z]+[\w.]+)\}/
36
23
  MAX_DEPTH = 16
37
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
+
38
32
  def resolve_variables(str)
39
33
  res = str
40
34
  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
@@ -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 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
8
  def initialize(hash)
9
+ super()
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
8
  def initialize
9
+ super()
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,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.5.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.5.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-12 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
@@ -60,7 +74,8 @@ files:
60
74
  homepage: https://github.com/the-vk/precursor
61
75
  licenses:
62
76
  - MIT
63
- metadata: {}
77
+ metadata:
78
+ rubygems_mfa_required: 'true'
64
79
  post_install_message:
65
80
  rdoc_options: []
66
81
  require_paths: