Class: Qtrix::Namespacing::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/qtrix/namespacing.rb

Overview

Manages namespaces. Uses Redis::Namespace to impose the namespacing on calls to redis, and maintains the known config namespaces within redis. Should not be working directly with this too much, it should be transparen when mixing in the Qtrix::Namespacing module.

Constant Summary collapse

NAMESPACING_KEY =
:namespacing
DEFAULT_NAMESPACE =
:default

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connection_config(opts = {}) ⇒ Object (readonly)

Returns the value of attribute connection_config.



77
78
79
# File 'lib/qtrix/namespacing.rb', line 77

def connection_config
  @connection_config
end

Instance Method Details

#add_namespace(namespace) ⇒ Object



88
89
90
91
# File 'lib/qtrix/namespacing.rb', line 88

def add_namespace(namespace)
  validate namespace
  namespacing_redis.sadd(:namespaces, namespace)
end

#change_current_namespace(namespace) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/qtrix/namespacing.rb', line 109

def change_current_namespace(namespace)
  unless namespaces.include? namespace
    raise "Unknown namespace: #{namespace}"
  end
  if not_ready?(namespace)
    raise "#{namespace} is empty"
  end
  namespacing_redis.set(:current_namespace, namespace)
end

#current_namespaceObject



119
120
121
122
123
124
# File 'lib/qtrix/namespacing.rb', line 119

def current_namespace
  if no_current_namespace?
    self.change_current_namespace DEFAULT_NAMESPACE
  end
  unpack(namespacing_redis.get(:current_namespace))
end

#namespacesObject



102
103
104
105
106
107
# File 'lib/qtrix/namespacing.rb', line 102

def namespaces
  if default_namespace_does_not_exist?
    namespacing_redis.sadd(:namespaces, DEFAULT_NAMESPACE)
  end
  namespacing_redis.smembers(:namespaces).map{|ns| unpack(ns)}
end

#redis(*namespaces) ⇒ Object



83
84
85
86
# File 'lib/qtrix/namespacing.rb', line 83

def redis(*namespaces)
  namespaced_client = Redis::Namespace.new(:qtrix, redis: client)
  namespaced_redis({redis: namespaced_client}, *evaluate(namespaces.uniq))
end

#remove_namespace!(namespace) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/qtrix/namespacing.rb', line 93

def remove_namespace!(namespace)
  raise "Cannot remove default namespace" if namespace == :default
  raise "Cannot remove current namespace" if namespace == current_namespace
  namespacing_redis.srem(:namespaces, namespace)
  Qtrix::Override.clear!(namespace)
  Qtrix::Queue.clear!(namespace)
  Qtrix::Matrix.clear!(namespace)
end