2016-05-13 32 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

相關問題