2012-07-20 59 views
4

嗨我正在開發一個簡單的api紅寶石使用intridea的葡萄。比方說,我們有這樣的:我們如何將葡萄api資源分成多個文件?

class API_v1 < Grape::API 
    resource :foo do 
    end 

    resource :bar do 
    end 

end 

我怎麼可能讓這個爲:foo:bar聲明是在單獨的文件?基本上,我想知道是否有可能有類似的軌道控制器,有多個文件來組織代碼。

我希望有人能給我一個關於如何實現這一點的見解。

回答

8

Ruby有open classes,所以你應該能夠簡單地將它們移動到單獨的文件中。

# foo.rb 
class API_v1 < Grape::API 
    resource :foo do 
    end 
end 

# bar.rb 
class API_v1 < Grape::API 
    resource :bar do 
    end 
end 
+0

究竟的基本知識,我需要!謝謝! – 2012-07-20 14:20:33

8

自述建議您使用mount

class Foo < Grape::API 
    resource :foo ... 
end 

class Bar < Grape::API 
    resource :bar ... 
end 

class API < Grape::API 
    mount Foo 
    mount Bar 
end