Rails 最佳实践问题:共享代码应该放在哪里以及如何加载?
为了提供完整的示例,我一直关注的 Rails 书籍和网页都专注于非常简单的项目。 我正在从小型项目应用程序转向非浏览器客户端领域,并且需要决定将所有相关方共享的代码放在哪里。
非浏览器客户端是在任何可以连接到数据库的计算机上运行的脚本。 浏览器客户端将命令写入数据库,脚本检查数据库并决定要做什么。 完成后,脚本将其结果写回。 该脚本不是由 RoR 服务器启动,但可以访问其目录结构。
共享代码最适合存放的地方在哪里,RoR 加载器将如何处理它? 有问题的代码并不真正属于模型,否则我会将其放入其中并完成它。
The rails books and web pages I've been following have all stuck to very simple projects for the sake of providing complete examples. I'm moving away from the small project app and into a realm of non-browser clients and need to decide where to put code that is shared by all involved parties.
The non-browser client is a script that runs on any machine which can connect to the database. Browser clients write commands into the database, which the script examines and decides what to do. Upon completion, the script then writes its result back. The script is not started by the RoR server, but has access to its directory structure.
Where would be the best place for shared code to live, and how would the RoR loader handle it? The code in question doesn't really belong in a model, otherwise I'd drop it in there and be done with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将共享代码放在 Rails 项目的
/lib
目录中,并考虑将其设为自定义 Rake 任务。I'd put the shared code in the Rails project's
/lib
directory and consider making it a custom Rake task.这实际上取决于您使用此共享代码的程度。 如果您在任何地方都使用它,则将其放入 lib 文件夹中(正如此处已经说明的那样)。 如果您只在几个地方使用它,您可能需要考虑用它制作一个插件并仅在使用它的地方加载它。 只加载你需要的东西是件好事(这是我喜欢 merb 的原因之一)。
It really depends on how much you use this shared code. If you use it everywhere, then throw it in the lib folder (as has already been stated here). If you are only using it in a few places, you might want to consider making a plugin out of it and loading it only in the places that use it. It's nice to only load what you need (one of the reasons I'm loving merb).