Module: MaimaiNet::ModuleExt::HaveClassMethods

Included in:
AddInternalMutex, AutoInitialize, ExtendedInspect, MethodCache
Defined in:
lib/maimai_net/module_ext.rb

Overview

Note:

it’s not valid to use this outside this file.

enables capability of having class_method block definition for a module.

Instance Method Summary collapse

Instance Method Details

#class_method(&block) ⇒ void

This method returns an undefined value.

defines an internal module to be inherited into calling class’s eugenclass.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/maimai_net/module_ext.rb', line 14

def class_method(&block)
  ref = self
  @_class_module ||= Module.new
  @_class_module.instance_eval <<~EOF
    def to_s
      "#{ref}::ClassMethod"
    end
    alias inspect to_s
  EOF
  @_class_module.class_exec(&block)
end

#included(cls) ⇒ void

This method returns an undefined value.

defines a hook to automatically extend target class with internal module from #class_method



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/maimai_net/module_ext.rb', line 30

def included(cls)
  super

  base = self
  cls.instance_exec do
    next unless Class === cls
    next unless base.instance_variable_defined?(:@_class_module)
    next unless Module === base.instance_variable_get(:@_class_module)
    ext_class = base.instance_variable_get(:@_class_module)
    # the use of singleton_class.include and extend are the same.
    extend ext_class
    # singleton_class.send(:include, ext_class)
  end
end