Sha256: 952ca6c959b78c286e3a14f2dbf9fa903510cf873be37eb48e6188cf2a3aafcb

Contents?: true

Size: 1.68 KB

Versions: 7

Compression:

Stored size: 1.68 KB

Contents

require 'tins/string_version'
require 'singleton'

module Tins

  SexySingleton = Singleton.dup

  module SexySingleton
    module SingletonClassMethods
    end
  end

  class << SexySingleton
    alias __old_singleton_included__ included

    if Tins::StringVersion.compare(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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tins-1.38.0 lib/tins/sexy_singleton.rb
tins-1.37.1 lib/tins/sexy_singleton.rb
tins-1.37.0 lib/tins/sexy_singleton.rb
tins-1.36.1 lib/tins/sexy_singleton.rb
tins-1.36.0 lib/tins/sexy_singleton.rb
tins-1.35.0 lib/tins/sexy_singleton.rb
tins-1.34.0 lib/tins/sexy_singleton.rb