Module: Modulation::ExportFromReceiver

Defined in:
lib/modulation/export_from_receiver.rb

Overview

Functionality related to export from receiver

Constant Summary collapse

RE_RESERVED_METHOD =
/^__/.freeze

Class Method Summary collapse

Class Method Details

.copy_constants(mod, receiver) ⇒ Array

Returns list of receiver constants.

Returns:

  • (Array)

    list of receiver constants



37
38
39
40
41
# File 'lib/modulation/export_from_receiver.rb', line 37

def copy_constants(mod, receiver)
  receiver.constants(false).each do |c|
    mod.singleton_class.const_set(c, receiver.const_get(c))
  end
end

.create_forwarding_methods(mod, receiver) ⇒ Array

Returns list of receiver methods.

Returns:

  • (Array)

    list of receiver methods



16
17
18
19
20
21
22
# File 'lib/modulation/export_from_receiver.rb', line 16

def create_forwarding_methods(mod, receiver)
  receiver_methods(receiver).each do |m|
    mod.singleton_class.send(:define_method, m) do |*args, &block|
      receiver.send(m, *args, &block)
    end
  end
end

.from_const(mod, name) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/modulation/export_from_receiver.rb', line 7

def from_const(mod, name)
  receiver = mod.singleton_class.const_get(name)

  methods = create_forwarding_methods(mod, receiver)
  consts = copy_constants(mod, receiver)
  methods + consts
end

.receiver_methods(receiver) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/modulation/export_from_receiver.rb', line 26

def receiver_methods(receiver)
  ignored_klass = case receiver
                  when Class, Module then receiver.class
                  else Object
                  end

  methods = receiver.methods.reject { |m| m =~ RE_RESERVED_METHOD }
  methods - ignored_klass.instance_methods
end