service_variables 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/service_variables.rb +24 -11
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9917573589bdffb40472076662e882113636a230
4
- data.tar.gz: c4ff4c6ae49c45b67c9d9658fe5a630cb108b6ae
3
+ metadata.gz: 7799d8f19f920e3736dc926274f6856dcaf2e533
4
+ data.tar.gz: 15255d19aaab87e8b8d799c6cdbdac42a8454f4d
5
5
  SHA512:
6
- metadata.gz: bc15b66bb5a9794ceacfd55a03fef2689e52be9c205355f82f73bb86bc9912b7599ea9817f4af9edabbff1146da0fbac188320b491ae9be88681106e8716ef5d
7
- data.tar.gz: 43ce7b6f14f95be633ae240b36b348b774a01ac0f2df5869e39a8ea83830327971c08dd12526215427d488d3b11ab87f3cfd18d0539df675cc049beee1e6c1d1
6
+ metadata.gz: 5ac95d7463716ed8d09f02959548a60289f7364340b9e40435867ac35fab98717da8426e6fed5a7c1d7b8b05abbd30e5ade24864e3c8bc21e37de7acb4631dac
7
+ data.tar.gz: 4aa7eeeeabfee449d12b1adf86a0be28e3c6f8b6ef38f1c773a2e30fe879d1817f76f8945982aefd785ceff7f3c5e37961586f8af65ebe1b239d5e444fc95e3d
@@ -9,22 +9,27 @@ module ServiceVariables
9
9
 
10
10
  DEFAULT_REDIS_KEY = 'service_variables_redis_key'
11
11
  BOOLEAN_VALUES = [true, false, 'true', 'false'].freeze
12
+ FAILURE_MODES = %i[raise_exception use_default use_last_value].freeze
12
13
 
13
14
  attr_writer :service_variables_redis_client, :redis_key
14
15
 
15
- def configure(redis_client:, redis_key: nil)
16
+ def configure(redis_client:, redis_key: nil, failure_mode: :raise_exception)
16
17
  @service_variables_redis_client = redis_client
17
18
  @redis_key = redis_key
19
+
20
+ raise InvalidValueError unless FAILURE_MODES.include?(failure_mode)
21
+ @failure_mode = failure_mode
22
+ @last_value_map = {}
18
23
  end
19
24
 
20
- def boolean_option(name, default: nil)
25
+ def boolean_option(name, default: nil, failure_mode: @failure_mode)
21
26
  getter_method_name = "#{name}?".to_sym
22
27
  setter_method_name = "#{name}=".to_sym
23
28
 
24
29
  define_singleton_method getter_method_name do
25
30
  # Note that all values are stored in Redis as strings.
26
- value = get(name)
27
- value.nil? ? default : get(name) == 'true'
31
+ value = get(name, failure_mode)
32
+ value.nil? ? default : get(name, failure_mode) == 'true'
28
33
  end
29
34
 
30
35
  # Allow for `name` and `name?` accessors of boolean options.
@@ -36,9 +41,9 @@ module ServiceVariables
36
41
  end
37
42
  end
38
43
 
39
- def integer_option(name, default: nil, min: nil, max: nil)
44
+ def integer_option(name, default: nil, min: nil, max: nil, failure_mode: @failure_mode)
40
45
  define_singleton_method name do
41
- get(name)&.to_i || default
46
+ get(name, failure_mode)&.to_i || default
42
47
  end
43
48
 
44
49
  define_singleton_method "#{name}=" do |value|
@@ -49,9 +54,9 @@ module ServiceVariables
49
54
  end
50
55
  end
51
56
 
52
- def float_option(name, default: nil, min: nil, max: nil)
57
+ def float_option(name, default: nil, min: nil, max: nil, failure_mode: @failure_mode)
53
58
  define_singleton_method name do
54
- get(name)&.to_f || default
59
+ get(name, failure_mode)&.to_f || default
55
60
  end
56
61
 
57
62
  define_singleton_method "#{name}=" do |value|
@@ -62,9 +67,9 @@ module ServiceVariables
62
67
  end
63
68
  end
64
69
 
65
- def string_option(name, default: nil, enum: nil)
70
+ def string_option(name, default: nil, enum: nil, failure_mode: @failure_mode)
66
71
  define_singleton_method name do
67
- get(name) || default
72
+ get(name, failure_mode) || default
68
73
  end
69
74
 
70
75
  define_singleton_method "#{name}=" do |value|
@@ -75,8 +80,14 @@ module ServiceVariables
75
80
 
76
81
  private
77
82
 
78
- def get(key)
83
+ def get(key, failure_mode)
79
84
  redis.hget(redis_hash_key, key)
85
+ rescue Redis::BaseConnectionError => e
86
+ raise e if failure_mode == :raise_exception
87
+ # If Redis returns a nil, then the default value is used, so we do the same with :use_default
88
+ return nil if failure_mode == :use_default
89
+ return @last_value_map[key] if failure_mode == :use_last_value
90
+ raise InvalidValueError unless FAILURE_MODES.include?(failure_mode)
80
91
  end
81
92
 
82
93
  def set(key, value)
@@ -85,6 +96,8 @@ module ServiceVariables
85
96
  else
86
97
  redis.hset(redis_hash_key, key, value)
87
98
  end
99
+
100
+ @last_value_map[key] = value
88
101
  end
89
102
 
90
103
  def redis
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: service_variables
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - coinbase
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.41'
61
+ version: 0.49.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.41'
68
+ version: 0.49.0
69
69
  description: Wrapper for redis backed configuration variables accessible to multiple
70
70
  servers.
71
71
  email: opensource@coinbase.com
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  version: '0'
95
95
  requirements: []
96
96
  rubyforge_project:
97
- rubygems_version: 2.6.13
97
+ rubygems_version: 2.6.11
98
98
  signing_key:
99
99
  specification_version: 4
100
100
  summary: Service level variables.