service_variables 1.0.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 +7 -0
  2. data/lib/service_variables.rb +98 -0
  3. metadata +101 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4186bf753f9a2e976662c1769dbebb5ca3c91626
4
+ data.tar.gz: 05d05c05bde1756f7567c6f71ce2e87e7d26ecd4
5
+ SHA512:
6
+ metadata.gz: 7a8f84e85755c7f20c2e397fc3f662e2a21f2b721b4ae8d3653ee44b3c1a90680f62293508f4b5a2777a2bb8610f4826b419fae2703bb1db6d227a71266cde06
7
+ data.tar.gz: 38295d1ea48bf8a9379e6340ed481e9339deb9791f5ca754e384dff5859c8ff80a7d909f60d68e035be93537e22efbca460b3c5657024111d31b0f694640f5f1
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'redis'
4
+
5
+ # Module to create service level variables stored in Redis.
6
+ module ServiceVariables
7
+ class NotConfiguredError < StandardError; end
8
+ class InvalidValueError < StandardError; end
9
+
10
+ DEFAULT_REDIS_KEY = 'service_variables_redis_key'
11
+ BOOLEAN_VALUES = [true, false, 'true', 'false'].freeze
12
+
13
+ attr_writer :service_variables_redis_client, :redis_key
14
+
15
+ def configure(redis_client:, redis_key: nil)
16
+ @service_variables_redis_client = redis_client
17
+ @redis_key = redis_key
18
+ end
19
+
20
+ def boolean_option(name, default: nil)
21
+ getter_method_name = "#{name}?".to_sym
22
+ setter_method_name = "#{name}=".to_sym
23
+
24
+ define_singleton_method getter_method_name do
25
+ # Note that all values are stored in Redis as strings.
26
+ value = get(name)
27
+ value.nil? ? default : get(name) == 'true'
28
+ end
29
+
30
+ # Allow for `name` and `name?` accessors of boolean options.
31
+ singleton_class.send(:alias_method, name, getter_method_name)
32
+
33
+ define_singleton_method setter_method_name do |value|
34
+ raise InvalidValueError, "Value isn't `true` or `false`" unless BOOLEAN_VALUES.include?(value)
35
+ set(name, value.to_s)
36
+ end
37
+ end
38
+
39
+ def integer_option(name, default: nil, min: nil, max: nil)
40
+ define_singleton_method name do
41
+ get(name)&.to_i || default
42
+ end
43
+
44
+ define_singleton_method "#{name}=" do |value|
45
+ value = Integer(value) # raises ArgumentError if non integer value
46
+ raise InvalidValueError, "Value too small. min = #{min.to_i}" if min && min > value
47
+ raise InvalidValueError, "Value too large. max = #{max.to_i}" if max && max < value
48
+ set(name, value&.to_s)
49
+ end
50
+ end
51
+
52
+ def float_option(name, default: nil, min: nil, max: nil)
53
+ define_singleton_method name do
54
+ get(name)&.to_f || default
55
+ end
56
+
57
+ define_singleton_method "#{name}=" do |value|
58
+ value = Float(value) # raises ArgumentError if non float value
59
+ raise InvalidValueError, "Value too small. min = #{min.to_f}" if min && min > value
60
+ raise InvalidValueError, "Value too large. max = #{max.to_f}" if max && max < value
61
+ set(name, value&.to_s)
62
+ end
63
+ end
64
+
65
+ def string_option(name, default: nil, enum: nil)
66
+ define_singleton_method name do
67
+ get(name) || default
68
+ end
69
+
70
+ define_singleton_method "#{name}=" do |value|
71
+ raise InvalidValueError, "Only #{enum} values are allowed." if enum && !enum.include?(value)
72
+ set(name, value)
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ def get(key)
79
+ redis.hget(redis_hash_key, key)
80
+ end
81
+
82
+ def set(key, value)
83
+ if value.nil?
84
+ redis.hdel(redis_hash_key, key)
85
+ else
86
+ redis.hset(redis_hash_key, key, value)
87
+ end
88
+ end
89
+
90
+ def redis
91
+ return @service_variables_redis_client if @service_variables_redis_client.is_a?(Redis)
92
+ raise NotConfiguredError, 'Redis client not given.'
93
+ end
94
+
95
+ def redis_hash_key
96
+ @redis_key.nil? ? DEFAULT_REDIS_KEY : "#{DEFAULT_REDIS_KEY}:#{@redis_key}"
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: service_variables
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - coinbase
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.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.41'
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: Wrapper for redis backed configuration variables accessible to multiple
70
+ servers.
71
+ email: opensource@coinbase.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/service_variables.rb
77
+ homepage: https://github.com/coinbase/service_variables
78
+ licenses:
79
+ - Apache-2.0
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.6.13
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Service level variables.
101
+ test_files: []