From b73597b614f0eddba5e0ef8472e1e0e684c88ee4 Mon Sep 17 00:00:00 2001 From: Issei Naruta Date: Mon, 9 Sep 2024 19:16:16 +0900 Subject: [PATCH] Refactor #enable_patch --- .../connection_adapter_patches/base_patch.rb | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/arproxy/connection_adapter_patches/base_patch.rb b/lib/arproxy/connection_adapter_patches/base_patch.rb index 6de85e0..c546335 100644 --- a/lib/arproxy/connection_adapter_patches/base_patch.rb +++ b/lib/arproxy/connection_adapter_patches/base_patch.rb @@ -24,7 +24,7 @@ def disable! end end - protected + private def enable_patches(*target_methods) target_methods.each do |target_method| enable_patch(target_method) @@ -35,29 +35,26 @@ def enable_patches(*target_methods) def enable_patch(target_method) return if @enabled_patches.include?(target_method) - case target_method - when :raw_execute - adapter_class.class_eval do + adapter_class.class_eval do + case target_method + when :raw_execute def raw_execute_with_arproxy(sql, name=nil, **kwargs) ::Arproxy.proxy_chain.connection = self _sql, _name = *::Arproxy.proxy_chain.head.execute(sql, name) self.send(:raw_execute_without_arproxy, _sql, _name, **kwargs) end - alias_method :raw_execute_without_arproxy, :raw_execute - alias_method :raw_execute, :raw_execute_with_arproxy - end - when :internal_exec_query - adapter_class.class_eval do + when :internal_exec_query def internal_exec_query_with_arproxy(sql, name=nil, binds=[], **kwargs) ::Arproxy.proxy_chain.connection = self _sql, _name = *::Arproxy.proxy_chain.head.execute(sql, name) self.send(:internal_exec_query_without_arproxy, _sql, _name, binds, **kwargs) end - alias_method :internal_exec_query_without_arproxy, :internal_exec_query - alias_method :internal_exec_query, :internal_exec_query_with_arproxy + else + raise ArgumentError, "Unsupported method to patch: #{target_method}" end - else - raise ArgumentError, "Unsupported method to patch: #{target_method}" + + alias_method :"#{target_method}_without_arproxy", target_method + alias_method target_method, :"#{target_method}_with_arproxy" end @enabled_patches << target_method