简体   繁体   English

应用程序之间共享的模型:DataMapper Rails 其他

[英]Models shared among applications : DataMapper Rails Other

I have a set of models I need to share among several rails3/sinatra/etc applications.我有一组模型需要在几个 rails3/sinatra/etc 应用程序之间共享。 I haven't seen anything like this yet and I'm curious what is the most effective way to go about this in a DRY manner?我还没有看到这样的东西,我很好奇 go 以干燥的方式对此最有效的方法是什么? Can I specify a central area for models, can I create a gem that only brings in the models I need?我可以为模型指定一个中心区域,我可以创建一个只引入我需要的模型的 gem 吗? How have other people approached this issue.其他人是如何处理这个问题的。 I was thinking of making a smallish gem or something I don't yet know about to handle this.我正在考虑制作一个小宝石或我还不知道的东西来处理这个问题。

I'm using Datamapper, but this is also a more general issue of structuring multiple rails/non rails ruby apps.我正在使用 Datamapper,但这也是构建多个导轨/非导轨 ruby 应用程序的更普遍问题。

You might find it's easier to have a submodule in your repository that contains your models and associated unit tests than to create a full-fledged gem.您可能会发现在存储库中包含模型和相关单元测试的子模块比创建成熟的 gem 更容易。 This way you can patch from one app to the other instead of having to drive everything top down.这样,您可以从一个应用程序修补到另一个应用程序,而不必自上而下地驱动所有内容。

You can create public API to models you want to use in other apps.您可以为要在其他应用程序中使用的模型创建公共 API。

In rails you should already have REST controllers, so you can use ActiveResource in the other apps.在 Rails 中,您应该已经拥有 REST 控制器,因此您可以在其他应用程序中使用 ActiveResource。

You can pack your ActiveResource classes in a gem just to simplify thing and to keep things DRY.您可以将 ActiveResource 类打包到 gem 中,以简化事情并保持事情干燥。

You can check: http://api.rubyonrails.org/classes/ActiveResource/Base.html for some examples.您可以查看: http://api.rubyonrails.org/classes/ActiveResource/Base.html获取一些示例。

And it doesn't matter if you're using ActiveRecord or DataMapper.使用 ActiveRecord 或 DataMapper 也没关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM