Module: ModuleFunctions

Defined in:
lib/module_functions/module_functions.rb

Overview

Useful functions for modules.

Class Method Summary collapse

Class Method Details

.import_public_methods(receiver, sender) ⇒ Object

Imports public methods from one module into another.

Input

receiver : Module

This module will have new methods added to it.

sender : Module

This module will have its methods called from receiver.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/module_functions/module_functions.rb', line 43

def import_public_methods(receiver, sender)
  unless receiver.class == Module && sender.class == Module
    raise ArgumentError, "Invalid parameter for #{__method__}. " +
      "A #{Module} was expected, but was a #{receiver.class}."
  end

  # Create public methods of the same name in the receiving module
  # that will call the original module's methods.
  (sender.public_methods - sender.class.public_methods).each do |method|
    receiver.module_eval(<<-EOT, __FILE__, __LINE__)
      def self.#{method}(*args)
        if args.length == 0
          #{sender.name}.#{method}
        else
          #{sender.name}.#{method}(*args)
        end
      end
    EOT
  end
end