Class: Aclatraz::Store::Riak

Inherits:
Object
  • Object
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.



12
13
14
15
16
17
18
19
20
21
# File 'lib/aclatraz/store/riak.rb', line 12

def initialize(bucket_name, *args)
  require "riak" rescue raise LoadError, \
    "You must install the `riak-client` gem to use Riak store"
  
  @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



42
43
44
45
46
# File 'lib/aclatraz/store/riak.rb', line 42

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

#clearObject



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

def clear
  # not optimal... yea but there is no other way to clear all keys 
  # in the riak bucket -_-"
  @backend.keys.each {|key| @backend.delete(key) }
end

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



48
49
50
# File 'lib/aclatraz/store/riak.rb', line 48

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)               # => "10/foo"
pack("foo", 10, "FooClass")   # => "10/foo/FooClass"
pack("foo", 10, FooClass.new) # => "10/foo/FooClass/{foo_object_ID}"


63
64
65
66
67
68
69
70
71
72
# File 'lib/aclatraz/store/riak.rb', line 63

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



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aclatraz/store/riak.rb', line 30

def roles(suspect=nil)
  roles = []
  # Also this can be a little bit slow...
  @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



23
24
25
26
27
28
# File 'lib/aclatraz/store/riak.rb', line 23

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