Class: Restruct::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/restruct/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Connection



4
5
6
7
8
9
# File 'lib/restruct/connection.rb', line 4

def initialize(*args)
  @args = args
  @redis = Redic.new *args
  @scripts = {}
  @nesting = ::Hash.new { |h,k| h[k] = 0 }
end

Instance Method Details

#batchObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/restruct/connection.rb', line 35

def batch
  incr_nesting
  begin
    result = yield
  ensure
    decr_nesting
  end
  commit unless nested?
rescue => ex   
  redis.clear unless nested?
  raise ex
end

#call(*args) ⇒ Object



11
12
13
14
15
16
# File 'lib/restruct/connection.rb', line 11

def call(*args)
  raise ArgumentError if args.empty?
  redis.call! *args
rescue RuntimeError => ex
  raise ConnectionErrorFactory.create(ex)
end

#cloneObject



52
53
54
# File 'lib/restruct/connection.rb', line 52

def clone
  Connection.new *@args
end

#lazy(*args) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/restruct/connection.rb', line 18

def lazy(*args)
  if nested?
    redis.queue *args
    nil
  else
    call *args
  end
end

#readObject



48
49
50
# File 'lib/restruct/connection.rb', line 48

def read
  redis.client.read
end

#script(lua_src, *args) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/restruct/connection.rb', line 27

def script(lua_src, *args)
  scripts[lua_src] ||= call 'SCRIPT', 'LOAD', lua_src
  call 'EVALSHA', scripts[lua_src], *args
rescue NoScriptError
  scripts.delete lua_src
  retry
end