Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/extended_include.rb

Overview

Extend class Module to support additional “include” functionality.

Instance Method Summary collapse

Instance Method Details

#extended_include(*modules) ⇒ Object

Include additional modules.

Unlike a traditional #include, the modules’ ::included methods (if present) will be called when the current module is included if they have not already been previously included by the including object’s ancestors.

Another difference is that multiple modules are always included first-to-last, so it doesn’t matter if you “extended_include M1, M2, M3” or “extended_include M1; extended_include M2; extended_include M3” or any other variant with the same reference order. Methods will always be sought in last-to-first order (M3, M2, M1).



92
93
94
# File 'lib/extended_include.rb', line 92

def extended_include (*modules)
	Extended_Include.add_includes self, *modules
end

#include_class_methods(*modules, &block) ⇒ Object

Extend class methods into the including object when including this module.

include_class_methods        # from sub-module ClassMethods, if present
include_class_methods M1, M2 # from specified sub-modules
include_class_methods do     # defined in a block
  def some_class_method; end
end

As usual, sub-modules must be defined before reference.



106
107
108
109
110
111
# File 'lib/extended_include.rb', line 106

def include_class_methods (*modules, &block)
	if !block && modules.empty? && const_defined?(:ClassMethods)
 Extended_Include.include_class_methods self, self::ClassMethods
	else Extended_Include.include_class_methods self, *modules, &block
	end
end