precursor 0.3.0 → 0.4.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: dbe652293d6dbc1fe13cd17330f59a9e37e36d89fa2ff60c4af4bba81d80598f
4
- data.tar.gz: b98e83f130cf813c408c2fc416245f5a3b8cc16d58c17b0b9229db1c4c034f55
3
+ metadata.gz: e106154712ad13288b622f2c6fcdded80b583a68387c259f136d1b0c448fdfe0
4
+ data.tar.gz: c99dc664e48950a6d6de38a0bd7f56c257f505d6065337f57c7d6021adc086e1
5
5
  SHA512:
6
- metadata.gz: 9b30d56c17c3bf072687cce95343b49853c4ba57f90632694070c864adb7a5a1d2549556a2e086a843af554b1c1617c254cfa68c87b2df931a2689d8234d455c
7
- data.tar.gz: 45f98b6fd91306ef418438968a98d4f2ad83a24160f31644f803c9cacad6d56ddb0aaf583cae4ec83436ebec921d810104a3bdc43af0a940c59077bc071a4b29
6
+ metadata.gz: 405695b440d4202e2c1a0678a92d5f17bb63fe9404774cf0f9d33dfff20c6c2c64322cfccd90f61bb06894325ea4edd3633dc08d71698459950b5aa37827cd08
7
+ data.tar.gz: 7b843d26c18872d5174ae6fef796ffaf63c2deaeec4726d0948a5a2a18181748942757066949e9633edaa78657b11fc69bf14baf0b6db9d4c624615ddee802b9
data/lib/argv_vault.rb ADDED
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+ require 'optparse/date'
5
+ require 'optparse/time'
6
+ require 'optparse/uri'
7
+
8
+ require_relative 'vault'
9
+
10
+ module Precursor
11
+ # Vault that parses command line arguments
12
+ class ArgvVault < Vault
13
+ def initialize(argv)
14
+ @vault_data = {}
15
+ @parser = OptionParser.new do |parser|
16
+ parser.on('-h', '--help', 'Prints this help') do
17
+ puts parser
18
+ exit
19
+ end
20
+ end
21
+ yield self
22
+ @parser.parse(argv)
23
+ end
24
+
25
+ def key(key_name)
26
+ builder = ArgvKeyBuilder.new(key_name)
27
+ yield builder
28
+ builder.build(@parser, @vault_data)
29
+ end
30
+
31
+ protected
32
+
33
+ def store
34
+ @vault_data
35
+ end
36
+
37
+ class ArgvKeyBuilder
38
+ def initialize(key_name)
39
+ @key_name = key_name
40
+ @short = []
41
+ @long = []
42
+ @type = nil
43
+ @description = nil
44
+ end
45
+
46
+ def short(short_key)
47
+ @short.push(short_key)
48
+ end
49
+
50
+ def long(long_key)
51
+ @long.push(long_key)
52
+ end
53
+
54
+ def type(type)
55
+ @type = type
56
+ end
57
+
58
+ def description(desc)
59
+ @description = desc
60
+ end
61
+
62
+ def build(parser, vault_data)
63
+ args = @short + @long
64
+ args.push(@type) unless @type.nil?
65
+ args.push(@description) unless @description.nil?
66
+
67
+ parser.on(*args) do |v|
68
+ vault_data[@key_name] = v
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
data/lib/env_vault.rb CHANGED
@@ -18,12 +18,10 @@ module Precursor
18
18
  end
19
19
  end
20
20
 
21
- def key?(key)
22
- @env_vars.key? key
23
- end
21
+ protected
24
22
 
25
- def value(key)
26
- @env_vars[key]
23
+ def store
24
+ @env_vars
27
25
  end
28
26
  end
29
27
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'vault'
4
+
5
+ module Precursor
6
+ class OverrideVault < Vault
7
+ def initialize
8
+ @vault_data = {}
9
+ end
10
+
11
+ def override(key, value)
12
+ store[key] = value
13
+ end
14
+
15
+ def clear(key)
16
+ store.delete(key)
17
+ end
18
+
19
+ protected
20
+
21
+ def store
22
+ @vault_data
23
+ end
24
+ end
25
+ end
data/lib/precursor.rb CHANGED
@@ -2,7 +2,10 @@
2
2
 
3
3
  require_relative 'config_root_builder'
4
4
 
5
+ require_relative 'argv_vault'
5
6
  require_relative 'env_vault'
7
+ require_relative 'override_vault'
8
+ require_relative 'thread_local_override_vault'
6
9
  require_relative 'yaml_vault'
7
10
 
8
11
  module Precursor
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'override_vault'
4
+
5
+ module Precursor
6
+ class ThreadLocalOverrideVault < OverrideVault
7
+ protected
8
+
9
+ def store
10
+ Thread.current[:precursor_override_vault] ||= {}
11
+ end
12
+ end
13
+ end
data/lib/vault.rb CHANGED
@@ -5,14 +5,20 @@ module Precursor
5
5
  class Vault
6
6
  # Returns true if the vault has a value for the given key, otherwise false
7
7
  # @return [Boolean] true if the vault has a value for the given key, otherwise false
8
- def key?
9
- false
8
+ def key?(key)
9
+ store.key?(key)
10
10
  end
11
11
 
12
12
  # Returns a value fot the given key, nil if key is not available
13
13
  # @return [Object|Nil] a value for the given key
14
14
  def value(key)
15
- nil
15
+ store[key]
16
+ end
17
+
18
+ protected
19
+ # Replace this method in descendant classes to return Hash-like store
20
+ def store
21
+ {}
16
22
  end
17
23
  end
18
24
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Precursor
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
data/lib/yaml_vault.rb CHANGED
@@ -10,12 +10,10 @@ module Precursor
10
10
  @yaml = flat_hash(YAML.load(src))
11
11
  end
12
12
 
13
- def key?(key)
14
- @yaml.key? key
15
- end
13
+ protected
16
14
 
17
- def value(key)
18
- @yaml[key]
15
+ def store
16
+ @yaml
19
17
  end
20
18
 
21
19
  private
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: precursor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.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-06 00:00:00.000000000 Z
11
+ date: 2022-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: optparse
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: debug
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -31,11 +45,14 @@ executables: []
31
45
  extensions: []
32
46
  extra_rdoc_files: []
33
47
  files:
48
+ - lib/argv_vault.rb
34
49
  - lib/config_key_builder.rb
35
50
  - lib/config_root.rb
36
51
  - lib/config_root_builder.rb
37
52
  - lib/env_vault.rb
53
+ - lib/override_vault.rb
38
54
  - lib/precursor.rb
55
+ - lib/thread_local_override_vault.rb
39
56
  - lib/vault.rb
40
57
  - lib/version.rb
41
58
  - lib/yaml_vault.rb