Created
February 6, 2017 15:06
-
-
Save bogdan/bf3a4de43fa4bef7edea1f9c53182776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def self._delegator_method(obj, accessor, method, ali) | |
accessor = accessor.to_s unless Symbol === accessor | |
if Module === obj ? | |
obj.method_defined?(accessor) || obj.private_method_defined?(accessor) : | |
obj.respond_to?(accessor, true) | |
accessor = "#{accessor}()" | |
end | |
method_call = ".__send__(:#{method}, *args, &block)" | |
if _valid_method?(method) | |
loc, = caller_locations(2,1) | |
pre = "_ =" | |
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method " | |
method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}" | |
begin; | |
unless ::Kernel.instance_method(:respond_to?).bind(_).call(:"#{method}") | |
::Kernel.warn "\#{caller_locations(1)[0]}: "#{mesg.dump}"\#{_.class}"'##{method}' | |
_#{method_call} | |
else | |
_.#{method}(*args, &block) | |
end | |
end; | |
end | |
_compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1) | |
begin; | |
proc do | |
def #{ali}(*args, &block) | |
#{pre} | |
begin | |
#{accessor} | |
end#{method_call}#{FILTER_EXCEPTION} | |
end | |
end | |
end; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment