Class: Savvy::Configurators::Redis

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(config: Savvy.config) ⇒ Redis

Returns a new instance of Redis.

Parameters:

  • config (Savvy::Config) (defaults to: Savvy.config)


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

#dbInteger (readonly)

The database number used by redis.

Returns:

  • (Integer)


47
48
49
50
51
# File 'lib/savvy/configurators/redis.rb', line 47

def db
  check_validity!

  @db
end

#hostString (readonly)

Returns:

  • (String)


55
56
57
58
59
# File 'lib/savvy/configurators/redis.rb', line 55

def host
  check_validity!

  @host
end

#namespace_prefixString? (readonly)

This is the namespace portion of a redis URI that is possibly set by the environment and will prefix the Savvy-generated namespace.

Returns:

  • (String, nil)


13
14
15
# File 'lib/savvy/configurators/redis.rb', line 13

def namespace_prefix
  @namespace_prefix
end

#portInteger (readonly)

Returns:

  • (Integer)


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

Returns:

  • (Redis::Namespace)

Raises:



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

Returns:

  • (ConnectionPool)

Raises:



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

Parameters:

  • parts (<String>)
  • without_namespace (Boolean) (defaults to: false)

    return a configuration hash only as defined

Returns:

  • ({Symbol => Object})


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

Parameters:

  • parts (<String>)

    parts to add to the namespace

Returns:

  • (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

#urlString

The url as configured by the environment (sans namespace)

Returns:

  • (String)


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

Returns:

  • (Boolean)


108
109
110
# File 'lib/savvy/configurators/redis.rb', line 108

def valid?
  @error.nil?
end