1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
require 'singleton'
module Tins
SexySingleton = Singleton.dup
module SexySingleton
module SingletonClassMethods
end
end
class << SexySingleton
alias __old_singleton_included__ included
if RUBY_VERSION < "2.7"
def included(klass)
__old_singleton_included__(klass)
(class << klass; self; end).class_eval do
if Object.method_defined?(:respond_to_missing?)
def respond_to_missing?(name, *args)
instance.respond_to?(name) || super
end
else
def respond_to?(name, *args)
instance.respond_to?(name) || super
end
end
def method_missing(name, *args, &block)
if instance.respond_to?(name)
instance.__send__(name, *args, &block)
else
super
end
end
end
super
end
else
def included(klass)
__old_singleton_included__(klass)
(class << klass; self; end).class_eval do
if Object.method_defined?(:respond_to_missing?)
def respond_to_missing?(name, *args, **kwargs)
instance.respond_to?(name) || super
end
else
def respond_to?(name, *args, **kwargs)
instance.respond_to?(name) || super
end
end
def method_missing(name, *args, **kwargs, &block)
if instance.respond_to?(name)
instance.__send__(name, *args, **kwargs, &block)
else
super
end
end
end
super
end
end
end
end
|