lib/redis_eval/script.rb in redis_eval-0.2.2 vs lib/redis_eval/script.rb in redis_eval-0.3.0
- old
+ new
@@ -1,21 +1,21 @@
module RedisEval
class Script
attr_accessor :parent
attr_reader :source, :sha
+ attr_writer :redis
def self.build_from_parent(src, parent, with_load: true)
- script = new(src, with_load: false)
+ script = new(src, with_load: false)
script.parent = parent
script.load if with_load
script
end
def initialize(src, with_load: true)
@source = src
- @sha = Digest::SHA1.hexdigest(@source)
- @redis = nil
+ @sha = Digest::SHA1.hexdigest(@source)
self.load if with_load
end
def load
redis_without_namespace.script(:load, source)
@@ -26,23 +26,19 @@
end
def execute(keys = [], argv = [])
redis.evalsha(sha, Array(keys), Array(argv))
rescue Redis::CommandError => e
- if e.message =~ /NOSCRIPT/
- redis.eval(source, Array(keys), Array(argv))
- else
- raise
- end
+ raise unless e.message =~ /NOSCRIPT/
+
+ redis.eval(source, Array(keys), Array(argv))
end
def redis
- return @redis if @redis
- parent ? parent.redis : Redis.current
- end
+ return @redis if instance_variable_defined?(:@redis)
+ return parent.redis unless parent.nil?
- def redis=(conn)
- @redis = conn
+ Redis.current
end
private
def redis_without_namespace