Class: Aclatraz::Store::Cassandra

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

Overview

List of global roles are stored in ‘roles => all` key. Each suspect has its own key, which contains list of assigned roles. Roles are stored in following format:

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

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Helpers

#camelize, #pack, #suspect_id, #unpack

Constructor Details

#initialize(*args) ⇒ Cassandra

Returns a new instance of Cassandra.



20
21
22
23
24
25
26
27
# File 'lib/aclatraz/store/cassandra.rb', line 20

def initialize(*args)
  @family  = args.shift
  @backend = if args.first.respond_to?(:keyspace)
    args.first
  else
    ::Cassandra.new(*args)
  end
end

Instance Method Details

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



46
47
48
49
50
# File 'lib/aclatraz/store/cassandra.rb', line 46

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

#clearObject



57
58
59
# File 'lib/aclatraz/store/cassandra.rb', line 57

def clear
  @backend.remove(@family, ROLES_KEY)
end

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



52
53
54
55
# File 'lib/aclatraz/store/cassandra.rb', line 52

def delete(role, suspect, object=nil)
  @backend.remove(@family, ROLES_KEY, SUSPECT_ROLES_KEY % suspect_id(suspect), \
    pack(role.to_s, object))
end

#roles(suspect = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/aclatraz/store/cassandra.rb', line 36

def roles(suspect=nil)
  if suspect
    data = @backend.get(@family, ROLES_KEY, SUSPECT_ROLES_KEY % suspect_id(suspect))
    data ? data.keys.map {|role| unpack(role) }.flatten.compact.uniq : []
  else
    data = @backend.get(@family, ROLES_KEY, ALL_ROLES_KEY)
    data ? data.keys.flatten : []
  end
end

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



29
30
31
32
33
34
# File 'lib/aclatraz/store/cassandra.rb', line 29

def set(role, suspect, object=nil)
  data = {}
  data[ALL_ROLES_KEY] = [role.to_s] unless object
  data[SUSPECT_ROLES_KEY % suspect_id(suspect)] = [pack(role.to_s, object)]
  @backend.insert(@family, ROLES_KEY, data)
end