mirror of
https://github.com/ms-ati/docile
synced 2023-03-27 23:21:52 -04:00
Merge pull request #3 from ms-tg/simplify-and-clarify-proxy-class
Simplify and clarify proxy class
This commit is contained in:
commit
2783ff283c
1 changed files with 7 additions and 15 deletions
|
@ -2,16 +2,15 @@ require 'set'
|
||||||
|
|
||||||
module Docile
|
module Docile
|
||||||
class FallbackContextProxy
|
class FallbackContextProxy
|
||||||
NON_PROXIED_METHODS = Set[:object_id, :__send__, :__id__, :==, :equal?, :"!", :"!=", :instance_exec,
|
NON_PROXIED_METHODS = Set[:__send__, :object_id, :__id__, :==, :equal?,
|
||||||
:instance_variables, :instance_variable_get, :instance_variable_set,
|
:"!", :"!=", :instance_exec, :instance_variables,
|
||||||
|
:instance_variable_get, :instance_variable_set,
|
||||||
:remove_instance_variable]
|
:remove_instance_variable]
|
||||||
|
|
||||||
NON_PROXIED_INSTANCE_VARIABLES = Set[:@__receiver__, :@__fallback__]
|
NON_PROXIED_INSTANCE_VARIABLES = Set[:@__receiver__, :@__fallback__]
|
||||||
|
|
||||||
instance_methods.each do |method|
|
instance_methods.each do |method|
|
||||||
unless NON_PROXIED_METHODS.include?(method.to_sym)
|
undef_method(method) unless NON_PROXIED_METHODS.include?(method.to_sym)
|
||||||
undef_method(method)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(receiver, fallback)
|
def initialize(receiver, fallback)
|
||||||
|
@ -19,22 +18,15 @@ module Docile
|
||||||
@__fallback__ = fallback
|
@__fallback__ = fallback
|
||||||
end
|
end
|
||||||
|
|
||||||
# Special case to allow proxy instance variables
|
|
||||||
def instance_variables
|
def instance_variables
|
||||||
# Ruby 1.8.x returns string names, convert to symbols for compatibility
|
# Ruby 1.8.x returns string names, convert to symbols for compatibility
|
||||||
super.select { |v| !NON_PROXIED_INSTANCE_VARIABLES.include?(v.to_sym) }
|
super.select { |v| !NON_PROXIED_INSTANCE_VARIABLES.include?(v.to_sym) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def method_missing(method, *args, &block)
|
def method_missing(method, *args, &block)
|
||||||
__proxy_method__(method, *args, &block)
|
@__receiver__.__send__(method.to_sym, *args, &block)
|
||||||
end
|
rescue ::NoMethodError => e
|
||||||
|
@__fallback__.__send__(method.to_sym, *args, &block)
|
||||||
def __proxy_method__(method, *args, &block)
|
|
||||||
begin
|
|
||||||
@__receiver__.__send__(method.to_sym, *args, &block)
|
|
||||||
rescue ::NoMethodError => e
|
|
||||||
@__fallback__.__send__(method.to_sym, *args, &block)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue