I need to inject a callbacks in every child class of a Parent class. So, method with callbacks must be called first, and all present chain later:
it is possible to achive thought alias_method (or alias_method_chain):
module ChildMod1
def save
puts "save ChildMod1"
super
end
end
module ChildMod2
def save
puts "save ChildMod2"
super
end
end
class Parent
def save
puts "save Parent"
end
end
class Child < Parent
include ChildMod1
include ChildMod2
def save
puts "save Child"
super
end
alias_method :old_save, :save
module_eval <<-R
def save
puts "save Callback"
old_save
end
R
end
c = Child.new
c.save
output
save Callback
save Child
save ChildMod2
save ChildMod1
save Parent
but is it possible to achieve this via inheritance? like in ChildMod1 or ChildMod2. I whant to execute a code inside a module space to get all benefits from inheritance
module ChildMod1
def save
puts "save ChildMod1"
super
end
end
module ChildMod2
def save
puts "save ChildMod2"
super
end
end
class Parent
def save
puts "save Parent"
end
end
class Child < Parent
include ChildMod1
include ChildMod2
def save
puts "save Child"
super
end
module_eval <<-R
def save
puts "save Callback"
super
end
R
end
c = Child.new
c.save
Output
save Callback
save ChildMod2
save ChildMod1
save Parent
as you see, it just overwrite Child
UPDATE wdebeaum solution is good, but what if i need to create a lot of methods dynamically thought module_eval or analog and redefine them inside a class? I cannot create a separate module for them.
class TestEval
def redefine_me
puts "Test method"
super # I expect that it will call Eval method, but module_eval just overwrite it
end
module_eval <<-R
def redefine_me
puts "Eval method"
end
R
end
UPDATE2 using a singleton class i'll got wrong chain Eval => Test instead of Test => Eval
class TestEval
def initialize
class << self
def redefine_me
puts "Eval method"
super
end
end
end
def redefine_me
puts "Test method"
end
end
TestEval.new.redefine_me
Let assume, that I have a class method "field", that ad开发者_开发知识库d some instance methods to a Datastream (for ex it'll add setter and getter methods) and I whant to redefine one of this methods, like this:
class Datastream
field :name
def name=(value)
puts "redefined!"
super
end
end
You could put the callback method in its own module, and rewrite the Parent's initialize method to extend
that module (using alias_method
if necessary). This will put the callback method before the Child's method, by linking it to each Child instance's singleton class. Just remove the module_eval
part from your second code example, and add this before c = Child.new
:
module Callback
def save
puts "save Callback"
super
end
end
class Parent
alias_method :old_initialize, :initialize
def initialize
old_initialize
extend Callback
end
end
Output:
save Callback
save Child
save ChildMod2
save ChildMod1
save Parent
精彩评论