Module: Extended_Include

Defined in:
lib/extended_include.rb

Overview

Extended_Include - Deals with some of the finer details of the extend-on-included idiom for adding both class and instance methods on module import.

Based on these posts:

and hundreds of other posts about the ::included extend ClassMethods hack.

The Extended_Include module is a back-end support module. See the Module module extensions for the user interface.

Version 0.0.3, 2014-05-19

Author:

License:

  • Public Domain

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_includes(base, *modules) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/extended_include.rb', line 29

def self.add_includes (base, *modules)
	(@include_list[base] ||= []).concat modules
	base.class_exec do
 modules.each { |mod| include mod }
 extend Extended_Include
	end
end

.class_methods_for(base) ⇒ Object

Return a module’s class method list.



38
39
40
41
# File 'lib/extended_include.rb', line 38

def self.class_methods_for (base)
	(@class_methods[base] ||= []).uniq!
	@class_methods[base]
end

.include_class_methods(base, *modules, &block) ⇒ Object



44
45
46
47
48
# File 'lib/extended_include.rb', line 44

def self.include_class_methods (base, *modules, &block)
	(@class_methods[base] ||= []).concat modules
	@class_methods[base].push Module.new(&block) if block
	base.class_exec { extend Extended_Include }
end

.includes_for(base) ⇒ Object

Return a module’s extended_include list.



51
52
53
54
# File 'lib/extended_include.rb', line 51

def self.includes_for (base)
	(@include_list[base] ||= []).uniq!
	@include_list[base]
end

Instance Method Details

#included(base) ⇒ Object

This method is automatically invoked whenever a module that uses #extended_include or #include_class_methods is included in another module.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/extended_include.rb', line 59

def included (base)
	# self is the included module; base is the including module
	Extended_Include.includes_for(self).each do |mod|
 mod.included base if mod.respond_to?(:included) &&
   (!base.respond_to?(:superclass) ||
   !base.superclass.include?(mod))
	end

	sources = Extended_Include.class_methods_for self
	base.class_exec { sources.each { |mod| extend mod } } unless
	  sources.empty?

	super base rescue nil	# chain any other #included methods
end