lib/redis_eval/script.rb in redis_eval-0.1.0 vs lib/redis_eval/script.rb in redis_eval-0.2.0
- old
+ new
@@ -1,51 +1,46 @@
module RedisEval
class Script
- attr_reader :name, :script, :sha
+ attr_accessor :parent
+ attr_reader :source, :sha
- class << self
- # @param path [Pathname]
- # @return [RedisEval::Script]
- def build_from_path(path)
- name = path.basename(".*").to_s
- script = File.read(path)
- new(name, script)
- end
+ def self.build_from_parent(src, parent, with_load: true)
+ script = new(src, with_load: with_load)
+ script.parent = parent
+ script
+ end
- def load(script)
- redis.script(:load, script)
- end
+ def initialize(src, with_load: true)
+ @source = src
+ @sha = Digest::SHA1.hexdigest(@source)
+ @redis = nil
+ self.load if with_load
+ end
- def flush
- redis.script(:flush)
- end
-
- def redis
- RedisEval.config.redis
- end
+ def load
+ redis.script(:load, source)
end
- def initialize(name, script)
- @name = name
- @script = script
- @sha = Digest::SHA1.hexdigest(script)
+ def exist?
+ redis.script(:exists, sha)
end
def execute(keys = [], argv = [])
redis.evalsha(sha, Array(keys), Array(argv))
rescue Redis::CommandError => e
if e.message =~ /NOSCRIPT/
- redis.eval(script, Array(keys), Array(argv))
+ redis.eval(source, Array(keys), Array(argv))
else
raise
end
end
- def exist?
- redis.script(:exists, sha)
+ def redis
+ return @redis if @redis
+ parent ? parent.redis : Redis.current
end
- def redis
- self.class.redis
+ def redis=(conn)
+ @redis = conn
end
end
end