class Readthis::Scripts
The `Scripts` class is used to conveniently execute lua scripts. The first time a command is run it is stored on the server and subsequently referred to by its SHA. Each instance tracks SHAs separately, they are not global.
Attributes
loaded[R]
Public Class Methods
new()
click to toggle source
Creates a new Readthis::Scripts
instance.
# File lib/readthis/scripts.rb, line 11 def initialize @loaded = {} end
Public Instance Methods
run(command, store, keys, args = [])
click to toggle source
Run a named lua script with the provided keys and arguments.
@param [String] command The script to run, without a `.lua` extension @param [#Store] store A Redis client for storing and evaluating the script @param [Array] keys One or more keys to pass to the command @param [Array] args One or more args to pass to the command
@return [Any] The Redis converted value returned on the script
@example
scripts.run('mexpire', store, %w[a b c], 1) # => 'OK'
# File lib/readthis/scripts.rb, line 28 def run(command, store, keys, args = []) store.evalsha( sha(command, store), Array(keys), Array(args) ) end
Private Instance Methods
abs_path(filename)
click to toggle source
# File lib/readthis/scripts.rb, line 52 def abs_path(filename) dir = File.expand_path(__dir__) File.join(dir, '../../script', filename) end
load_script!(command, store)
click to toggle source
# File lib/readthis/scripts.rb, line 42 def load_script!(command, store) path = abs_path("#{command}.lua") File.open(path) do |file| loaded[command] = store.script(:load, file.read) end rescue Errno::ENOENT raise Readthis::UnknownCommandError, "unknown command '#{command}'" end
sha(command, store)
click to toggle source
# File lib/readthis/scripts.rb, line 38 def sha(command, store) loaded[command] ||= load_script!(command, store) end