2016-05-13 56 views
1

我很喜欢使用Rails,但我想了解更多关于框架之外的Ruby的知识,并想到为特定问题编写一个gem。我开始选择一对夫妇(dynamoid和mongoid)来跟随他们的模式。我已经堕落了,并会欢迎一些帮助。ActiveSupport的class_attribute undefined

我有一个模块,它看起来像这样:

module MyModule 
    module Document 
    extend ActiveSupport::Concern 
    include Components 

    included do 
     class_attribute :foo 
    end 
    end 
end 

这将导入另一个模块,它只是一个说服袋扩展

module MyModule 
    module Components 
    include Fields 
    end 
end 

最后包括另一模块,它看起来是这样的:

module MyModule 
    module Fields 
    extend ActiveSupport::Concern 

    included do 
     class_attribute :bar 
    end 
    end 
end 

这遵循其他宝石的模式。

class_attribute致电MyModule::Document正常工作,正如我所料。

我的问题是我在Fields模块中得到undefined method `class_attribute' for MyModule::Components:Module (NoMethodError)。现在,这似乎是有意义的,因为MyModule::Fields被包含在模块中,而不是类,并且模块没有class_attribute方法。我看不出这两个宝石是如何执行这个技巧的,或者是否有我失踪的成语?

回答

2

您需要让您的课程MyModule::Document成为MyModule::Fields的包装器,而不是模块MyModule::Components。这会工作吗?

module MyModule 
    module Components 
    extend ActiveSupport::Concern 
    included do 
     include Fields 
    end 
    end 
end 
+0

感谢您的建议,但这不起作用(因为我无法解决的问题,这给了我一个'''components.rb:6:in'included':错误数量的参数(给定0,expected 1)(ArgumentError)''' – razorhead

+0

听起来好像它使用内置的'included'方法而不是'ActiveSupport :: Concern'。你确定你在'Components'中扩展它吗?如果我 –

+0

实际上你几乎是对的,我错过了MyModule :: Components模块中的'''扩展ActiveSupport :: Concern''。我的头几个小时血腥的纸浆,这为我修好了。谢谢你的帮助。 – razorhead

相关问题