2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/core_ext/hash/deep_merge"
|
2020-05-06 00:43:17 -04:00
|
|
|
require "active_support/core_ext/symbol/starts_ends_with"
|
2009-03-29 02:52:46 -04:00
|
|
|
|
2005-12-15 15:03:23 -05:00
|
|
|
module ActiveSupport
|
|
|
|
class OptionMerger #:nodoc:
|
2007-09-27 06:41:12 -04:00
|
|
|
instance_methods.each do |method|
|
2020-05-06 00:43:17 -04:00
|
|
|
undef_method(method) unless method.start_with?("__", "instance_eval", "class", "object_id")
|
2005-12-15 15:03:23 -05:00
|
|
|
end
|
2007-09-27 06:41:12 -04:00
|
|
|
|
2005-12-15 15:03:23 -05:00
|
|
|
def initialize(context, options)
|
|
|
|
@context, @options = context, options
|
|
|
|
end
|
2007-09-27 06:41:12 -04:00
|
|
|
|
2005-12-15 15:03:23 -05:00
|
|
|
private
|
|
|
|
def method_missing(method, *arguments, &block)
|
2019-09-13 06:19:22 -04:00
|
|
|
options = nil
|
2014-04-02 18:27:55 -04:00
|
|
|
if arguments.first.is_a?(Proc)
|
2008-11-15 10:48:14 -05:00
|
|
|
proc = arguments.pop
|
|
|
|
arguments << lambda { |*args| @options.deep_merge(proc.call(*args)) }
|
2019-09-06 14:46:28 -04:00
|
|
|
elsif arguments.last.respond_to?(:to_hash)
|
2019-09-13 06:19:22 -04:00
|
|
|
options = @options.deep_merge(arguments.pop)
|
2008-11-15 10:48:14 -05:00
|
|
|
else
|
2019-09-13 07:20:28 -04:00
|
|
|
options = @options
|
2008-11-15 10:48:14 -05:00
|
|
|
end
|
|
|
|
|
2020-02-05 03:22:25 -05:00
|
|
|
invoke_method(method, arguments, options, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
if RUBY_VERSION >= "2.7"
|
|
|
|
def invoke_method(method, arguments, options, &block)
|
|
|
|
if options
|
|
|
|
@context.__send__(method, *arguments, **options, &block)
|
|
|
|
else
|
|
|
|
@context.__send__(method, *arguments, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
def invoke_method(method, arguments, options, &block)
|
2020-05-23 17:47:48 -04:00
|
|
|
arguments << options.dup if options
|
2019-09-13 06:19:22 -04:00
|
|
|
@context.__send__(method, *arguments, &block)
|
|
|
|
end
|
2005-12-15 15:03:23 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|