Ruby在模块中获取所有类

时间:2018-08-16 17:45:42

标签: ruby-on-rails ruby model-view-controller

在我的Ruby on Rails项目中,有一个名为obelisk的模块。在模块obelisk中,我有一个名为node的类。我有许多类型的节点,例如ifhangupplayback等。 因此,在我的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::HangupClass 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] ,同样不是我想要的。

但是到目前为止没有任何效果。

谢谢。

0 个答案:

没有答案