module ModuleFunctions

Useful functions for modules.

Public Class Methods

import_public_methods(receiver, sender) click to toggle source

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.

# 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