Class: Savvy::Configurators::Redis
- Inherits:
-
Object
- Object
- Savvy::Configurators::Redis
- Defined in:
- lib/savvy/configurators/redis.rb
Constant Summary collapse
- DEFAULT_SEPARATOR =
?:- PATH_PATTERN =
%r{\A/(?<db>\d{1,2})(?:/(?<namespace_prefix>[^/]+))?}
Instance Attribute Summary collapse
-
#db ⇒ Integer
readonly
The database number used by redis.
- #host ⇒ String readonly
-
#namespace_prefix ⇒ String?
readonly
This is the namespace portion of a redis URI that is possibly set by the environment and will prefix the Savvy-generated namespace.
- #password ⇒ String? readonly
- #port ⇒ Integer readonly
- #scheme ⇒ "rediss", "redis" readonly
- #userinfo ⇒ String? readonly
Instance Method Summary collapse
- #build_connection(*namespace_parts) ⇒ Redis::Namespace
- #build_connection_pool(*namespace_parts, size: 5, timeout: 5) ⇒ ConnectionPool
- #config_hash(*parts, without_namespace: false) ⇒ {Symbol => Object} (also: #to_h)
-
#initialize(config: Savvy.config) ⇒ Redis
constructor
A new instance of Redis.
- #namespace(*parts, separator: DEFAULT_SEPARATOR) ⇒ Object
- #namespaced_url(*parts) ⇒ String
-
#url ⇒ String
The url as configured by the environment (sans namespace).
- #uses_ssl? ⇒ Boolean (also: #has_ssl?)
- #valid? ⇒ Boolean
Constructor Details
#initialize(config: Savvy.config) ⇒ Redis
Returns a new instance of Redis.
16 17 18 19 20 21 22 23 |
# File 'lib/savvy/configurators/redis.rb', line 16 def initialize(config: Savvy.config) @config = config @db = 0 @namespace_prefix = nil parse_url! end |
Instance Attribute Details
#db ⇒ Integer (readonly)
The database number used by redis.
50 51 52 53 54 |
# File 'lib/savvy/configurators/redis.rb', line 50 def db check_validity! @db end |
#host ⇒ String (readonly)
58 59 60 61 62 |
# File 'lib/savvy/configurators/redis.rb', line 58 def host check_validity! @host end |
#namespace_prefix ⇒ String? (readonly)
This is the namespace portion of a redis URI that is possibly set by the environment and will prefix the Savvy-generated namespace.
13 14 15 |
# File 'lib/savvy/configurators/redis.rb', line 13 def namespace_prefix @namespace_prefix end |
#password ⇒ String? (readonly)
80 81 82 83 84 |
# File 'lib/savvy/configurators/redis.rb', line 80 def password check_validity! @password end |
#port ⇒ Integer (readonly)
88 89 90 91 92 |
# File 'lib/savvy/configurators/redis.rb', line 88 def port check_validity! @port end |
#scheme ⇒ "rediss", "redis" (readonly)
96 97 98 99 100 |
# File 'lib/savvy/configurators/redis.rb', line 96 def scheme check_validity! @scheme end |
#userinfo ⇒ String? (readonly)
113 114 115 116 117 |
# File 'lib/savvy/configurators/redis.rb', line 113 def userinfo check_validity! @userinfo end |
Instance Method Details
#build_connection(*namespace_parts) ⇒ Redis::Namespace
135 136 137 138 139 |
# File 'lib/savvy/configurators/redis.rb', line 135 def build_connection(*namespace_parts) raise Savvy::RedisError, 'Requires redis-namespace gem' unless defined?(::Redis::Namespace) ::Redis::Namespace.new(namespace(*namespace_parts), redis: ::Redis.new(url: url)) end |
#build_connection_pool(*namespace_parts, size: 5, timeout: 5) ⇒ ConnectionPool
126 127 128 129 130 131 132 |
# File 'lib/savvy/configurators/redis.rb', line 126 def build_connection_pool(*namespace_parts, size: 5, timeout: 5) raise Savvy::RedisError, 'Requires connection_pool gem' unless defined?(ConnectionPool) ::ConnectionPool.new size: size, timeout: timeout do build_connection(*namespace_parts) end end |
#config_hash(*parts, without_namespace: false) ⇒ {Symbol => Object} Also known as: to_h
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/savvy/configurators/redis.rb', line 28 def config_hash(*parts, without_namespace: false) check_validity! { host: @host, port: @port, db: @db, scheme: @scheme, password: @password, ssl: uses_ssl?, }.tap do |h| ns = without_namespace ? @namespace_prefix : namespace(*parts) h[:namespace] = ns unless ns.nil? end end |
#namespace(*parts, separator: DEFAULT_SEPARATOR) ⇒ Object
64 65 66 67 68 |
# File 'lib/savvy/configurators/redis.rb', line 64 def namespace(*parts, separator: DEFAULT_SEPARATOR) check_validity! @config.build_namespace(*parts, prefix: @namespace_prefix, separator: separator) end |
#namespaced_url(*parts) ⇒ String
72 73 74 75 76 |
# File 'lib/savvy/configurators/redis.rb', line 72 def namespaced_url(*parts) check_validity! build_redis_url *parts end |
#url ⇒ String
The url as configured by the environment (sans namespace)
105 106 107 108 109 |
# File 'lib/savvy/configurators/redis.rb', line 105 def url check_validity! build_redis_url without_namespace: true end |
#uses_ssl? ⇒ Boolean Also known as: has_ssl?
119 120 121 |
# File 'lib/savvy/configurators/redis.rb', line 119 def uses_ssl? @scheme == "rediss" end |
#valid? ⇒ Boolean
141 142 143 |
# File 'lib/savvy/configurators/redis.rb', line 141 def valid? @error.nil? end |