Skip to content

Commit

Permalink
Refactor #enable_patch
Browse files Browse the repository at this point in the history
  • Loading branch information
mirakui committed Sep 9, 2024
1 parent 5dc155e commit b73597b
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions lib/arproxy/connection_adapter_patches/base_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit b73597b

Please sign in to comment.