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.
- #port ⇒ Integer 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).
- #valid? ⇒ Boolean
Constructor Details
Instance Attribute Details
#db ⇒ Integer (readonly)
The database number used by redis.
47 48 49 50 51 |
# File 'lib/savvy/configurators/redis.rb', line 47 def db check_validity! @db end |
#host ⇒ String (readonly)
55 56 57 58 59 |
# File 'lib/savvy/configurators/redis.rb', line 55 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 |
#port ⇒ Integer (readonly)
77 78 79 80 81 |
# File 'lib/savvy/configurators/redis.rb', line 77 def port check_validity! @port end |
Instance Method Details
#build_connection(*namespace_parts) ⇒ Redis::Namespace
102 103 104 105 106 |
# File 'lib/savvy/configurators/redis.rb', line 102 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
93 94 95 96 97 98 99 |
# File 'lib/savvy/configurators/redis.rb', line 93 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 |
# File 'lib/savvy/configurators/redis.rb', line 28 def config_hash(*parts, without_namespace: false) check_validity! { host: @host, port: @port, db: @db, }.tap do |h| ns = without_namespace ? @namespace_prefix : namespace(*parts) h[:namespace] = ns unless ns.nil? end end |
#namespace(*parts, separator: DEFAULT_SEPARATOR) ⇒ Object
61 62 63 64 65 |
# File 'lib/savvy/configurators/redis.rb', line 61 def namespace(*parts, separator: DEFAULT_SEPARATOR) check_validity! @config.build_namespace(*parts, prefix: @namespace_prefix, separator: separator) end |
#namespaced_url(*parts) ⇒ String
69 70 71 72 73 |
# File 'lib/savvy/configurators/redis.rb', line 69 def namespaced_url(*parts) check_validity! build_redis_url *parts end |
#url ⇒ String
The url as configured by the environment (sans namespace)
86 87 88 89 90 |
# File 'lib/savvy/configurators/redis.rb', line 86 def url check_validity! build_redis_url without_namespace: true end |
#valid? ⇒ Boolean
108 109 110 |
# File 'lib/savvy/configurators/redis.rb', line 108 def valid? @error.nil? end |