rails ActiveSupport::Concern封装了包含并扩展技术,解决了链式包含的问题一个模块可以通过扩展Concern模块来实现包含并扩展的功能。
concern.rb 代码如下
module ActiveSupport
module Concern
class MultipleIncludedBlocks < StandardError #:nodoc:
def initialize
super "Cannot define multiple 'included' blocks for a Concern"
end
end
def self.extended(base) #:nodoc:
base.instance_variable_set(:@_dependencies, [])
end
def append_features(base)
if base.instance_variable_defined?(:@_dependencies)
base.instance_variable_get(:@_dependencies) << self
false
else
return false if base < self
@_dependencies.each { |dep| base.include(dep) }
super
base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
base.class_eval(&@_included_block) if instance_variable_defined?(:@_included_block)
end
end
def included(base = nil, &block)
if base.nil?
raise MultipleIncludedBlocks if instance_variable_defined?(:@_included_block)
@_included_block = block
else
super
end
end
def class_methods(&class_methods_module_definition)
mod = const_defined?(:ClassMethods, false) ?
const_get(:ClassMethods) :
const_set(:ClassMethods, Module.new)
mod.module_eval(&class_methods_module_definition)
end
end
end
append_features 是在模块被include时调用,该方法实现把模块加入base的祖先链中,通过@_dependencies记录模块的继承关系,分别并把 class_methods 或 ClassMethods 里的块使用module_eval定义为类方法,进而实现扩展
使用如下
require 'active_support'
module A
def a_class_method
puts 'a_class_method'
end
end
module B
extend ActiveSupport::Concern
class_methods do
def b_class_method
puts 'b_class_method'
end
include A
end
end
class X
include B
end
X.a_class_method
X.b_class_method
完美实现了链式包含的问题