确定给定的模块是rails引擎

时间:2022-10-12 23:22:25

Hi I have two modules

嗨,我有两个模块

  1. Admin
  2. 管理员
  3. Blog (Blog is a rails engine) where Admin is a module for namespacing admin features of app, but Blog is a module representing rails engine. Is there a better way to determine which among them is engine, like a function "is_engine?"
  4. 博客(博客是一个rails引擎),其中Admin是appspacing管理员功能的模块,但Blog是代表rails引擎的模块。有没有更好的方法来确定它们中的哪个是引擎,就像函数“is_engine?”

Admin.is_engine?

Admin.is_engine?

=> false

=>假

Blog.is_engine?

Blog.is_engine?

=> true

=>是的

Definately I can have a try catch thing to determine this

肯定我可以尝试一下来确定这一点

def is_engine? module
  module::Engine
  true
rescue NameError
  false
end

here

这里

is_engine? Admin

will return false

将返回false

is_engine? Blog

will return true

将返回true

Thanks

谢谢

1 个解决方案

#1


3  

I'm not sure I understand what you are trying to do: a Rails Engine is a class (a subclass of Rails::Engine), not a module.

我不确定我是否理解你要做的事情:Rails Engine是一个类(Rails :: Engine的子类),而不是模块。

If you have an instance, you could use:

如果您有实例,则可以使用:

admin.kind_of?(Rails::Engine)

If you have a class, you can use:

如果您有课程,可以使用:

Something.ancestors.include?(Rails::Engine)

If what you have is a module, then it cannot be a subclass of Rails::Engine, and it's not an engine.

如果你拥有的是一个模块,那么它就不能成为Rails :: Engine的子类,而且它不是一个引擎。

EDIT

If you have a module or constant something and want to know if there's a constant with a certain name in its namespace, you can use:

如果你有一个模块或常量的东西,并想知道它的命名空间中是否有一个具有某个名称的常量,你可以使用:

something.constants.include?(:Engine)

#1


3  

I'm not sure I understand what you are trying to do: a Rails Engine is a class (a subclass of Rails::Engine), not a module.

我不确定我是否理解你要做的事情:Rails Engine是一个类(Rails :: Engine的子类),而不是模块。

If you have an instance, you could use:

如果您有实例,则可以使用:

admin.kind_of?(Rails::Engine)

If you have a class, you can use:

如果您有课程,可以使用:

Something.ancestors.include?(Rails::Engine)

If what you have is a module, then it cannot be a subclass of Rails::Engine, and it's not an engine.

如果你拥有的是一个模块,那么它就不能成为Rails :: Engine的子类,而且它不是一个引擎。

EDIT

If you have a module or constant something and want to know if there's a constant with a certain name in its namespace, you can use:

如果你有一个模块或常量的东西,并想知道它的命名空间中是否有一个具有某个名称的常量,你可以使用:

something.constants.include?(:Engine)