20/07/2009
Very often when looking at the code in rails plugins you can run into this:
module Taggable
def self.included(base)
base.extend(ClassMethods)
end
module module ClassMethods
#methods here
end
end
This is a part of a bigger pattern which is shown below:
module ModuleA
def self.included(base)
# add class methods from ModuleB
base.extend(ModuleB)
end
end
module ModuleB
def act_as_hello
p "hello from module B"
end
end
class ClassC
#class body here
end
# include moduleA in classC
ClassC.send(:include, ModuleA)
class ClassD < ClassC
act_as_hello
end
classD = ClassD.new
The pattern is used often when developing plugins with ActiveRecord. What we gain by inheriting from ClassC (class ClassD < ClassC) are instance methods from ModuleA. This is done by:
ClassC.send(:include, ModuleA)
Moreover since ModuleA is included in ClassC, ModuleA’s initializer def self.included(base) will be invoked at the time ModuleA is mixed with ClassC. The invocation will call base.extend(ModuleB). In this case base represents ClassC which will be extended by adding class methods from ModuleB. The ModuleA’s init method is shown again below:
def self.included(base)
# add class methods from ModuleB to ClassC
base.extend(ModuleB)
end
At the end our ClassD has now access to all class methods defined in ModuleB. act_as_hello will be called during ClassD initialization:
class ClassD < ClassC
act_as_hello
end