Class: Motel::Sources::Redis

Inherits:
Object
  • Object
show all
Defined in:
lib/motel/sources/redis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Redis

Returns a new instance of Redis.



10
11
12
13
14
15
16
# File 'lib/motel/sources/redis.rb', line 10

def initialize(config = {})
  @host                = config[:host]
  @port                = config[:port]
  @password            = config[:password]
  @path                = config[:path]
  @prefix_tenant_alias = (config[:prefix_tenant_alias] || 'tenant:')
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/motel/sources/redis.rb', line 8

def host
  @host
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/motel/sources/redis.rb', line 8

def password
  @password
end

#pathObject

Returns the value of attribute path.



8
9
10
# File 'lib/motel/sources/redis.rb', line 8

def path
  @path
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/motel/sources/redis.rb', line 8

def port
  @port
end

#prefix_tenant_aliasObject

Returns the value of attribute prefix_tenant_alias.



8
9
10
# File 'lib/motel/sources/redis.rb', line 8

def prefix_tenant_alias
  @prefix_tenant_alias
end

Instance Method Details

#add_tenant(name, spec) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/motel/sources/redis.rb', line 36

def add_tenant(name, spec)
  raise ExistingTenantError if tenant?(name)

  spec.each do |field, value|
    redis.hset(tenant_alias(name), field, value)
  end
end

#delete_tenant(name) ⇒ Object



52
53
54
55
56
57
# File 'lib/motel/sources/redis.rb', line 52

def delete_tenant(name)
  if tenant?(name)
    fields = redis.hkeys tenant_alias(name)
    redis.hdel(tenant_alias(name), [*fields])
  end
end

#tenant(name) ⇒ Object



27
28
29
30
# File 'lib/motel/sources/redis.rb', line 27

def tenant(name)
  spec = redis.hgetall(tenant_alias(name))
  spec if spec.any?
end

#tenant?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/motel/sources/redis.rb', line 32

def tenant?(name)
  !tenant(name).nil?
end

#tenantsObject



18
19
20
21
22
23
24
25
# File 'lib/motel/sources/redis.rb', line 18

def tenants
  redis.keys.inject({}) do |hash, tenant_als|
    if tenant_als.match("^#{prefix_tenant_alias}")
      hash[tenant_name(tenant_als)] = tenant(tenant_name(tenant_als))
    end
    hash
  end
end

#update_tenant(name, spec) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/motel/sources/redis.rb', line 44

def update_tenant(name, spec)
  raise NonexistentTenantError unless tenant?(name)

  spec.each do |field, value|
    redis.hset(tenant_alias(name), field, value)
  end
end