Module: SuperModule

Defined in:
lib/super_module.rb

Overview

This module allows defining class methods and method invocations the same way a super class does without using def included(base).

Constant Summary collapse

EXCLUDED_SINGLETON_METHODS =
[
  :__super_module_class_methods,
  :__invoke_super_module_class_method_calls,
  :__define_super_module_class_methods,
  :__restore_original_method_missing,
  :included, :method_missing,
  :singleton_method_added
]

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
# File 'lib/super_module.rb', line 18

def self.included(base)
  base.class_eval do
    class << self

      def include(base, &block)
        method_missing('include', base, &block)
      end

      def __super_module_class_method_calls
        @__super_module_class_method_calls ||= []
      end

      def __super_module_class_methods
        @__super_module_class_methods ||= []
      end

      def singleton_method_added(method_name)
        __super_module_class_methods << [method_name, method(method_name)] unless EXCLUDED_SINGLETON_METHODS.include?(method_name)
        super
      end

      def method_missing(method_name, *args, &block)
        __super_module_class_method_calls << [method_name, args, block]
      end

      def __invoke_super_module_class_method_calls(base)
        __super_module_class_method_calls.each do |method_name, args, block|
          base.class_eval do
            send(method_name, *args, &block)
          end
        end
      end

      def __define_super_module_class_methods(base)
        __super_module_class_methods.each do |method_name, method|
          base.class_eval do
            self.class.send(:define_method, method_name, &method)
          end
        end
      end

      def included(base)
        __invoke_super_module_class_method_calls(base)
        __define_super_module_class_methods(base)
      end
    end
  end
end