1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
# frozen_string_literal: true
class RedisClient
module Decorator
class << self
def create(commands_mixin)
client_decorator = Class.new(Client)
client_decorator.include(commands_mixin)
pipeline_decorator = Class.new(Pipeline)
pipeline_decorator.include(commands_mixin)
client_decorator.const_set(:Pipeline, pipeline_decorator)
client_decorator
end
end
module CommandsMixin
def initialize(client)
@client = client
end
%i(call call_v call_once call_once_v blocking_call blocking_call_v).each do |method|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method}(*args, &block)
@client.#{method}(*args, &block)
end
ruby2_keywords :#{method} if respond_to?(:ruby2_keywords, true)
RUBY
end
end
class Pipeline
include CommandsMixin
end
class Client
include CommandsMixin
def initialize(_client)
super
@_pipeline_class = self.class::Pipeline
end
def with(*args)
@client.with(*args) { |c| yield self.class.new(c) }
end
ruby2_keywords :with if respond_to?(:ruby2_keywords, true)
def pipelined(exception: true)
@client.pipelined(exception: exception) { |p| yield @_pipeline_class.new(p) }
end
def multi(**kwargs)
@client.multi(**kwargs) { |p| yield @_pipeline_class.new(p) }
end
%i(close scan hscan sscan zscan).each do |method|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{method}(*args, &block)
@client.#{method}(*args, &block)
end
ruby2_keywords :#{method} if respond_to?(:ruby2_keywords, true)
RUBY
end
%i(id config size connect_timeout read_timeout write_timeout pubsub).each do |reader|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{reader}
@client.#{reader}
end
RUBY
end
%i(timeout connect_timeout read_timeout write_timeout).each do |writer|
class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{writer}=(value)
@client.#{writer} = value
end
RUBY
end
end
end
end
|