Class: Aclatraz::Store::Redis

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/aclatraz/store/redis.rb

Overview

List of global roles are stored in ROLES set. Each suspect has its own key, which contains list of assigned roles. Roles are stored in following format:

suspect.{:suspect_id}.roles:
  "role_name"
  "role_name/ClassName"
  "role_name/ObjectClass/object_id"

Constant Summary collapse

ROLES_KEY =
"roles"
SUSPECT_ROLES_KEY =
"suspect.%s.roles"

Instance Method Summary collapse

Methods included from Helpers

#camelize, #pack, #suspect_id, #unpack

Constructor Details

#initialize(*args) ⇒ Redis

Returns a new instance of Redis.



19
20
21
22
23
24
25
# File 'lib/aclatraz/store/redis.rb', line 19

def initialize(*args)
  @backend = if args.first.respond_to?(:sadd)
    args.first
  else
    ::Redis.new(*args)
  end
end

Instance Method Details

#check(role, suspect, object = nil) ⇒ Object



45
46
47
48
49
# File 'lib/aclatraz/store/redis.rb', line 45

def check(role, suspect, object=nil)
  @backend.sismember(SUSPECT_ROLES_KEY % suspect_id(suspect), pack(role.to_s, object)) or begin
    object && !object.is_a?(Class) ? check(role, suspect, object.class) : false
  end
end

#clearObject



55
56
57
# File 'lib/aclatraz/store/redis.rb', line 55

def clear
  @backend.flushdb
end

#delete(role, suspect, object = nil) ⇒ Object



51
52
53
# File 'lib/aclatraz/store/redis.rb', line 51

def delete(role, suspect, object=nil)
  @backend.srem(SUSPECT_ROLES_KEY % suspect_id(suspect), pack(role.to_s, object))
end

#roles(suspect = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/aclatraz/store/redis.rb', line 34

def roles(suspect=nil)
  if suspect
    @backend.smembers(SUSPECT_ROLES_KEY % suspect_id(suspect)).map {|role|
      role = unpack(role)
      role[0] if role.size == 1
    }.compact.uniq
  else
    @backend.smembers(ROLES_KEY)
  end
end

#set(role, suspect, object = nil) ⇒ Object



27
28
29
30
31
32
# File 'lib/aclatraz/store/redis.rb', line 27

def set(role, suspect, object=nil)
  @backend.multi do
    @backend.sadd(ROLES_KEY, role.to_s) unless object
    @backend.sadd(SUSPECT_ROLES_KEY % suspect_id(suspect), pack(role.to_s, object))
  end
end