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"
|
2009-03-29 02:52:46 -04:00
|
|
|
|
2005-12-15 15:03:23 -05:00
|
|
|
module ActiveSupport
|
2021-07-29 17:18:07 -04:00
|
|
|
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
|
|
|
|
|
2021-02-06 22:20:18 -05:00
|
|
|
if options
|
|
|
|
@context.__send__(method, *arguments, **options, &block)
|
|
|
|
else
|
2019-09-13 06:19:22 -04:00
|
|
|
@context.__send__(method, *arguments, &block)
|
|
|
|
end
|
2005-12-15 15:03:23 -05:00
|
|
|
end
|
2021-07-02 11:42:27 -04:00
|
|
|
|
|
|
|
def respond_to_missing?(*arguments)
|
|
|
|
@context.respond_to?(*arguments)
|
|
|
|
end
|
2005-12-15 15:03:23 -05:00
|
|
|
end
|
|
|
|
end
|