2018-01-14 23:55:16 +00:00
|
|
|
require "set"
|
2011-12-06 20:05:31 +00:00
|
|
|
|
|
|
|
module Docile
|
2013-07-29 04:13:50 +00:00
|
|
|
# @api private
|
|
|
|
#
|
2013-07-28 20:42:30 +00:00
|
|
|
# A proxy object with a primary receiver as well as a secondary
|
|
|
|
# fallback receiver.
|
|
|
|
#
|
|
|
|
# Will attempt to forward all method calls first to the primary receiver,
|
|
|
|
# and then to the fallback receiver if the primary does not handle that
|
|
|
|
# method.
|
2013-07-29 03:57:25 +00:00
|
|
|
#
|
|
|
|
# This is useful for implementing DSL evaluation in the context of an object.
|
|
|
|
#
|
2013-08-02 19:53:00 +00:00
|
|
|
# @see Docile.dsl_eval
|
2011-12-06 20:05:31 +00:00
|
|
|
class FallbackContextProxy
|
2013-07-28 20:42:30 +00:00
|
|
|
# The set of methods which will **not** be proxied, but instead answered
|
|
|
|
# by this object directly.
|
2013-07-25 14:02:05 +00:00
|
|
|
NON_PROXIED_METHODS = Set[:__send__, :object_id, :__id__, :==, :equal?,
|
2018-01-14 23:55:16 +00:00
|
|
|
:"!", :"!=", :instance_exec, :instance_variables,
|
2013-07-25 14:02:05 +00:00
|
|
|
:instance_variable_get, :instance_variable_set,
|
2011-12-06 21:21:11 +00:00
|
|
|
:remove_instance_variable]
|
|
|
|
|
2018-08-19 21:02:41 +00:00
|
|
|
# The set of methods which will **not** fallback from the block's context
|
|
|
|
# to the dsl object.
|
|
|
|
NON_FALLBACK_METHODS = Set[:class, :self, :respond_to?, :instance_of?]
|
|
|
|
|
2013-07-28 20:42:30 +00:00
|
|
|
# The set of instance variables which are local to this object and hidden.
|
|
|
|
# All other instance variables will be copied in and out of this object
|
|
|
|
# from the scope in which this proxy was created.
|
2011-12-06 21:21:11 +00:00
|
|
|
NON_PROXIED_INSTANCE_VARIABLES = Set[:@__receiver__, :@__fallback__]
|
2011-12-06 20:05:31 +00:00
|
|
|
|
2013-07-28 20:42:30 +00:00
|
|
|
# Undefine all instance methods except those in {NON_PROXIED_METHODS}
|
2011-12-06 20:05:31 +00:00
|
|
|
instance_methods.each do |method|
|
2013-07-25 14:03:04 +00:00
|
|
|
undef_method(method) unless NON_PROXIED_METHODS.include?(method.to_sym)
|
2011-12-06 20:05:31 +00:00
|
|
|
end
|
|
|
|
|
2013-07-28 20:42:30 +00:00
|
|
|
# @param [Object] receiver the primary proxy target to which all methods
|
|
|
|
# initially will be forwarded
|
|
|
|
# @param [Object] fallback the fallback proxy target to which any methods
|
|
|
|
# not handled by `receiver` will be forwarded
|
2011-12-06 20:05:31 +00:00
|
|
|
def initialize(receiver, fallback)
|
|
|
|
@__receiver__ = receiver
|
|
|
|
@__fallback__ = fallback
|
2018-02-02 19:05:36 +00:00
|
|
|
|
|
|
|
# Enables calling DSL methods from helper methods in the block's context
|
|
|
|
unless fallback.respond_to?(:method_missing)
|
|
|
|
# NOTE: There's no {#define_singleton_method} on Ruby 1.8.x
|
|
|
|
singleton_class = (class << fallback; self; end)
|
|
|
|
|
|
|
|
# instrument {#method_missing} on the block's context to fallback to
|
|
|
|
# the DSL object. This allows helper methods in the block's context to
|
|
|
|
# contain calls to methods on the DSL object.
|
|
|
|
singleton_class.
|
|
|
|
send(:define_method, :method_missing) do |method, *args, &block|
|
2018-08-19 21:02:41 +00:00
|
|
|
m = method.to_sym
|
2019-06-12 18:24:36 +00:00
|
|
|
if !NON_FALLBACK_METHODS.include?(m) && !fallback.respond_to?(m) && receiver.respond_to?(m)
|
2018-02-02 19:05:36 +00:00
|
|
|
receiver.__send__(method.to_sym, *args, &block)
|
|
|
|
else
|
|
|
|
super(method, *args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# instrument a helper method to remove the above instrumentation
|
|
|
|
singleton_class.
|
|
|
|
send(:define_method, :__docile_undo_fallback__) do
|
|
|
|
singleton_class.send(:remove_method, :method_missing)
|
|
|
|
singleton_class.send(:remove_method, :__docile_undo_fallback__)
|
|
|
|
end
|
|
|
|
end
|
2011-12-06 20:05:31 +00:00
|
|
|
end
|
|
|
|
|
2013-07-28 20:42:30 +00:00
|
|
|
# @return [Array<Symbol>] Instance variable names, excluding
|
|
|
|
# {NON_PROXIED_INSTANCE_VARIABLES}
|
|
|
|
#
|
|
|
|
# @note on Ruby 1.8.x, the instance variable names are actually of
|
|
|
|
# type `String`.
|
2011-12-06 21:21:11 +00:00
|
|
|
def instance_variables
|
2013-07-24 20:20:50 +00:00
|
|
|
# Ruby 1.8.x returns string names, convert to symbols for compatibility
|
|
|
|
super.select { |v| !NON_PROXIED_INSTANCE_VARIABLES.include?(v.to_sym) }
|
2011-12-06 21:21:11 +00:00
|
|
|
end
|
|
|
|
|
2013-07-28 20:42:30 +00:00
|
|
|
# Proxy all methods, excluding {NON_PROXIED_METHODS}, first to `receiver`
|
|
|
|
# and then to `fallback` if not found.
|
2011-12-06 20:05:31 +00:00
|
|
|
def method_missing(method, *args, &block)
|
2014-02-04 19:24:17 +00:00
|
|
|
if @__receiver__.respond_to?(method.to_sym)
|
2014-02-04 13:44:20 +00:00
|
|
|
@__receiver__.__send__(method.to_sym, *args, &block)
|
|
|
|
else
|
|
|
|
@__fallback__.__send__(method.to_sym, *args, &block)
|
|
|
|
end
|
2011-12-06 20:05:31 +00:00
|
|
|
end
|
|
|
|
end
|
2013-04-01 03:27:12 +00:00
|
|
|
end
|