在我的Ruby on Rails项目中,有一个名为obelisk
的模块。在模块obelisk
中,我有一个名为node
的类。我有许多类型的节点,例如if
,hangup
,playback
等。
因此,在我的app/models/node.rb
中,我有以下声明:
require_dependency Obelisk::Engine.root.join('app', 'models', 'obelisk/node.rb').to_s
# Extend the model here.
module Obelisk
class Node
end
end
在app/models/obelisk
中,我有一个子目录app/models/obelisk/node
,用于存放与所有这些不同类型的节点相关的文件。例如,我有一个app/models/obelisk/node/hangup.rb
并带有以下声明:
require_dependency Obelisk::Engine.root.join('app', 'models', 'obelisk/node/hangup.rb').to_s
# Extend the model here.
module Obelisk
class Node::Hangup
end
end
我想创建一个表单,以便用户可以选择他们要创建的节点的类型。为此,我认为需要在Obelisk模块中获取所有不同类型节点的名称。也就是说,获取使用['hangup', 'if', 'playback'...]
,class Node::Hangup
,Class Node::If
...
Class Node::Playback
我已经尝试过ObjectSpace.each_object(Class).select {|c| c.included_modules.include? Obelisk}
我也像How to get all class names in a namespace in Ruby?
Obelisk::Node.constants.select {|c| Obelisk::Node.const_get(c).is_a? Class}
此输出为:
[:StringKeyedHashAccessor, :IndifferentHashAccessor, :IndifferentCoder, :HashAccessor, :RuntimeReflection, :AggregateReflection, :HasManyReflection, :HasOneReflection, :BelongsToReflection, :ThroughReflection, :MacroReflection, :PolymorphicReflection, :AbstractReflection, :HasAndBelongsToManyReflection, :AssociationReflection, :TooManyRecords, :BelongsToPolymorphicAssociation, :HasManyAssociation, :HasManyThroughAssociation, :HasOneAssociation, :HasOneThroughAssociation, :Preloader, :JoinDependency, :AssociationScope, :AliasTracker, :CollectionProxy, :Association, :SingularAssociation, :CollectionAssociation, :BelongsToAssociation, :TimeZoneConverter, :GeneratedAttributeMethods, :TypeDecorator, :PresenceValidator, :LengthValidator, :UniquenessValidator, :AbsenceValidator, :AssociatedValidator, :CallbackSequence, :Callback, :CallTemplate, :CallbackChain, :NumericalityValidator, :WithValidator, :ConfirmationValidator, :ExclusionValidator, :FormatValidator, :AcceptanceValidator, :InclusionValidator, :ScopeRegistry]
,这不是我想要的。
Obelisk::Node.constants.select{|c| Obelisk::Node.const_get(c).is_a? Class}
的输出为
[:StringKeyedHashAccessor, :IndifferentHashAccessor, :IndifferentCoder, :HashAccessor, :RuntimeReflection, :AggregateReflection, :HasManyReflection, :HasOneReflection, :BelongsToReflection, :ThroughReflection, :MacroReflection, :PolymorphicReflection, :AbstractReflection, :HasAndBelongsToManyReflection, :AssociationReflection, :TooManyRecords, :BelongsToPolymorphicAssociation, :HasManyAssociation, :HasManyThroughAssociation, :HasOneAssociation, :HasOneThroughAssociation, :Preloader, :JoinDependency, :AssociationScope, :AliasTracker, :CollectionProxy, :Association, :SingularAssociation, :CollectionAssociation, :BelongsToAssociation, :TimeZoneConverter, :GeneratedAttributeMethods, :TypeDecorator, :PresenceValidator, :LengthValidator, :UniquenessValidator, :AbsenceValidator, :AssociatedValidator, :CallbackSequence, :Callback, :CallTemplate, :CallbackChain, :NumericalityValidator, :WithValidator, :ConfirmationValidator, :ExclusionValidator, :FormatValidator, :AcceptanceValidator, :InclusionValidator, :ScopeRegistry]
,同样不是我想要的。
但是到目前为止没有任何效果。
谢谢。