File: sexy_singleton.rb

package info (click to toggle)
ruby-tins 1.32.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,248 kB
  • sloc: ruby: 6,659; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download
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