Class: Aclatraz::Store::Riak
- Inherits:
-
Object
- Object
- Aclatraz::Store::Riak
show all
- Includes:
- Helpers
- Defined in:
- lib/aclatraz/store/riak.rb
Overview
The most optimal way to store roles in riak database is pack everything in a single key name, eg:
:suspect_id/:role_name
:suspect_id/:role_name/:ClassName
:suspect_id/:role_name/:ObjectClass/object_id
Instance Method Summary
collapse
Methods included from Helpers
#camelize, #suspect_id, #unpack
Constructor Details
#initialize(bucket_name, *args) ⇒ Riak
Returns a new instance of Riak.
14
15
16
17
18
19
20
|
# File 'lib/aclatraz/store/riak.rb', line 14
def initialize(bucket_name, *args)
@backend = if args.first.respond_to?(:bucket)
args.first.bucket(bucket_name)
else
::Riak::Client.new(*args).bucket(bucket_name)
end
end
|
Instance Method Details
#check(role, suspect, object = nil) ⇒ Object
41
42
43
44
45
|
# File 'lib/aclatraz/store/riak.rb', line 41
def check(role, suspect, object=nil)
@backend.exists?(pack(role.to_s, suspect_id(suspect), object)) or begin
object && !object.is_a?(Class) ? check(role, suspect, object.class) : false
end
end
|
#clear ⇒ Object
51
52
53
54
55
|
# File 'lib/aclatraz/store/riak.rb', line 51
def clear
@backend.keys.each {|key| @backend.delete(key) }
end
|
#delete(role, suspect, object = nil) ⇒ Object
47
48
49
|
# File 'lib/aclatraz/store/riak.rb', line 47
def delete(role, suspect, object=nil)
@backend.delete(pack(role.to_s, suspect_id(suspect), object))
end
|
#pack(role, suspect, object = nil) ⇒ Object
Pack given permission data.
pack("foo", 10) pack("foo", 10, "FooClass") pack("foo", 10, FooClass.new)
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/aclatraz/store/riak.rb', line 62
def pack(role, suspect, object=nil)
case object
when nil
[suspect, role]
when Class
[suspect, role, object.name]
else
[suspect, role, object.class.name, object.id]
end.join("/")
end
|
#roles(suspect = nil) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/aclatraz/store/riak.rb', line 29
def roles(suspect=nil)
roles = []
@backend.keys.each do |key|
@backend.exists?(key) ? perm = unpack(key) : next
if perm.size == 2 && (!suspect || (suspect && perm[0].to_s == suspect_id(suspect)))
roles.push(perm[1])
end
end
roles.compact.uniq
end
|
#set(role, suspect, object = nil) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/aclatraz/store/riak.rb', line 22
def set(role, suspect, object=nil)
obj = @backend.new(pack(role.to_s, suspect_id(suspect), object))
obj.content_type = "text/plain"
obj.data = "1"
obj.store
end
|