2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/slavable.rb', line 2
def switch(*method_names)
options = method_names.pop
unless options.is_a?(Hash)
raise ArgumentError, 'Unable to detect "to" option, usage: "switch :method, :other, ..., to: :connection_name"'
end
method_names.each do |method|
aliased_method, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
with_name = "#{aliased_method}_with_connection#{punctuation}"
without_name = "#{aliased_method}_without_connection#{punctuation}"
connection = options.with_indifferent_access.fetch(:to)
class_eval " def \#{with_name}(*args, &block)\n ::ActiveRecord::Base.within(:\#{connection}) { send(:\#{without_name}, *args, &block) }\n end\n eoruby\n\n alias_method without_name, method\n alias_method method, with_name\n end\nend\n", __FILE__, __LINE__ + 1
|