2011-12-06 20:05:31 +00:00
|
|
|
require 'set'
|
|
|
|
|
|
|
|
module Docile
|
|
|
|
class FallbackContextProxy
|
2013-07-25 14:02:05 +00:00
|
|
|
NON_PROXIED_METHODS = Set[:__send__, :object_id, :__id__, :==, :equal?,
|
|
|
|
:"!", :"!=", :instance_exec, :instance_variables,
|
|
|
|
:instance_variable_get, :instance_variable_set,
|
2011-12-06 21:21:11 +00:00
|
|
|
:remove_instance_variable]
|
|
|
|
|
|
|
|
NON_PROXIED_INSTANCE_VARIABLES = Set[:@__receiver__, :@__fallback__]
|
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
|
|
|
|
|
|
|
|
def initialize(receiver, fallback)
|
|
|
|
@__receiver__ = receiver
|
|
|
|
@__fallback__ = fallback
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2011-12-06 20:05:31 +00:00
|
|
|
def method_missing(method, *args, &block)
|
2013-07-25 14:03:04 +00:00
|
|
|
@__receiver__.__send__(method.to_sym, *args, &block)
|
|
|
|
rescue ::NoMethodError => e
|
|
|
|
@__fallback__.__send__(method.to_sym, *args, &block)
|
2011-12-06 20:05:31 +00:00
|
|
|
end
|
|
|
|
end
|
2013-04-01 03:27:12 +00:00
|
|
|
end
|