[英]Extending a module that extends a class
Is there a way to "flatten" nested modules so that all of their methods can be used when extending another class or module? 有没有一种方法可以“展平”嵌套模块,以便在扩展另一个类或模块时可以使用它们的所有方法? For example:
例如:
class User
extend UserStats
end
module UserStats
module Current
def active
where('status = "active"')
end
end
end
I want to be able to extend UserStats (or User) such that the methods in UserStats::Current are available as class methods for User. 我希望能够扩展UserStats(或User),以便UserStats :: Current中的方法可用作User的类方法。
I tried "extend Current" in UserStats, but that doesn't seem to work. 我在UserStats中尝试了“ extended Current”,但这似乎不起作用。 Is there any way to do this?
有什么办法吗?
为什么不仅仅extend UserStats::Current
呢?
Do you mean something like this? 你的意思是这样吗?
module UserStats
def self.extended(klass)
klass.send(:extend, Current)
end
module Current
def active
puts "test"
end
end
end
class User
extend ::UserStats
end
puts User.active
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.