在没有 Rails 的情况下预加载类?

发布于 2024-11-25 23:04:40 字数 154 浏览 1 评论 0原文

我正在开发一个大项目,我意识到其中几个组件是一组类,我可以将它们转变为服务并从 Rails 中剥离。但现在我已经做到了,我意识到在没有 Spork 的情况下加载类的缓慢并不是 Rails 缓慢的原因,而是 Ruby 缓慢的原因。有没有像 Spork 这样的东西可以在非 Rails 项目中工作?

I am working on a big project, and I realized that several of the components were groups of classes that I could turn into services and strip from Rails. But now that I've done that I realize that the slowness of loading classes without Spork isn't a function of Rails being slow, but a function of Ruby being slow. Is there something like Spork that will work in non Rails projects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁吢 2024-12-02 23:04:40

Spork 应该适用于任何 ruby​​ 项目,它只需要更多的设置。

假设您使用的是 rspec 2.xspork 0.9,请创建一个 spec_helper.rb,如下所示:

require 'spork'

# the rspec require seems to be necessary, 
# without it you get "Missing or uninitialized constant: Object::RSpec" errors
require 'rspec' 

Spork.prefork do

  # do expensive one-time setup here
  require 'mylibrary'
  MyLibrary.setup_lots_of_stuff

end

Spork.each_run do

  # do setup that must be done on each test run here (setting up external state, etc):
  MyLibrary.reset_db

end

中的所有内容Spork.prefork 块只会运行一次(在 spork 启动时),其余部分将在每次测试调用时运行。

如果您有很多特定于框架的设置,那么您最好为您的库制作一个 AppFramework。有关示例,请参阅 padrino AppFramework

Spork should work just fine for any ruby project, it just requires a bit more setup.

Assuming you're using rspec 2.x and spork 0.9, make a spec_helper.rb that looks something like:

require 'spork'

# the rspec require seems to be necessary, 
# without it you get "Missing or uninitialized constant: Object::RSpec" errors
require 'rspec' 

Spork.prefork do

  # do expensive one-time setup here
  require 'mylibrary'
  MyLibrary.setup_lots_of_stuff

end

Spork.each_run do

  # do setup that must be done on each test run here (setting up external state, etc):
  MyLibrary.reset_db

end

Everything in the Spork.prefork block will only be run once (at spork startup), the rest will run on every test invocation.

If you have lots of framework-specific setup, you'd probably be better off making an AppFramework for your library. See the padrino AppFramework for an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文