Module: Redis::Lua

Included in:
Redis
Defined in:
lib/redis/lua.rb

Defined Under Namespace

Classes: Script

Constant Summary collapse

Version =
'0.1.0'

Instance Method Summary collapse

Instance Method Details

#register_script(name, text) ⇒ Object



28
29
30
31
32
# File 'lib/redis/lua.rb', line 28

def register_script(name, text)
  script = Script.new(name, text)
  scripts << script
  script
end

#register_script_files(glob) ⇒ Object



34
35
36
37
38
39
# File 'lib/redis/lua.rb', line 34

def register_script_files(glob)
  Dir[glob].map do |filename|
    script_name = filename.split('/').last.split('.').first
    register_script script_name, File.read(filename)
  end
end

#run_script(name, *args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/redis/lua.rb', line 8

def run_script(name, *args)
  raise "Nonexistent Lua script!" unless s = scripts.find { |script| script.name == name }

  case @client
  when Client
    begin
      evalsha s.sha, *args
    rescue CommandError => e
      raise unless e.message =~ /\ANOSCRIPT/
      script :load, s.text
      evalsha s.sha, *args
    end
  when Pipeline
    # When pipelining or in a transaction block, an error will abort the
    # whole thing and there's no way to check whether the script has
    # already been loaded. So, to be safe, just eval the script text.
    eval s.text, *args
  end
end

#scriptsObject



41
42
43
# File 'lib/redis/lua.rb', line 41

def scripts
  @scripts ||= []
end