module Extended_Include
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:
-
stackoverflow.com/questions/15905270/can-you-extend-self-included
-
www.kappacs.com/2014/ruby-sub-classes-inheritance-include-extend/
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 Brian Katzung (briank@kappacs.com), Kappa Computer Solutions, LLC @license Public Domain
Constants
- VERSION
Public Class Methods
Backend for {Module#extended_include}.
# 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
Return a module’s class method list.
# File lib/extended_include.rb, line 38 def self.class_methods_for (base) (@class_methods[base] ||= []).uniq! @class_methods[base] end
Backend for {Module#include_class_methods}.
# 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
Return a module’s extended_include list.
# File lib/extended_include.rb, line 51 def self.includes_for (base) (@include_list[base] ||= []).uniq! @include_list[base] end
Public Instance Methods
This method is automatically invoked whenever a module that uses extended_include or include_class_methods is included in another module.
# 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