gpt4 book ai didi

ruby-on-rails - 自动加载路径和嵌套服务类在 Ruby 中崩溃

转载 作者:数据小太阳 更新时间:2023-10-29 07:06:01 28 4
gpt4 key购买 nike

我在 Rails 5 项目的 app/services 文件夹下有多个加载/需要类的问题,我开始放弃这个问题。

首先要明确的是,services/ 是我在整个项目中使用的简单 PORO 类,用于从 Controller 、模型等中抽象出大部分业务逻辑。

树看起来像这样

app/
services/
my_service/
base.rb
funny_name.rb
my_service.rb
models/
funny_name.rb

失败 #1

首先,当我尝试使用 MyService.const_get('FunnyName') 时,它从我的模型目录中获取了 FunnyName。当我直接执行 MyService::FunnyName 时,它似乎没有相同的行为,但在我的大多数测试和更改中,它工作正常,这很奇怪。

我意识到 Rails config.autoload_paths 不会递归加载东西;第一个被捕获的 FunnyNamemodels/funny_name.rb 是有道理的,因为它确实被加载了,但另一个没有。

没关系,让我们找到解决方法。我将其添加到我的 application.rb 中:

config.autoload_paths += Dir[Rails.root.join('app', 'services', '**/')]

这会将服务的所有子目录添加到 config.autoload_paths 中。显然不推荐从 Rails 5 开始写这样的东西;但这个想法对我来说确实是正确的。

失败 #2

现在,当我启动我的应用程序时,它崩溃并输出类似这样的内容

Unable to autoload constant Base, expected /.../backend/app/services/my_service/base.rb to define it (LoadError)

名称已更改,但它是我之前编写的树的匹配路径

问题是,base.rb 是在错误引导我的确切文件中定义的,其中包含类似的内容

class MyService
class Base
end
end

糟糕的解决方案

所以我尝试了其他解决方法,尝试了很多,但都没有用。所以我最终完全删除了 autoload_paths 并将其直接添加到 application.rb

Dir[Rails.root.join('app', 'services', '**', '*.rb')].each { |file| require file }

现在 base.rb 已正确加载,MyService.const_get('FunnyName') 实际上会返回正确的类并且一切正常,但这是一个令人作呕的解决方法.此外,它尚未在 production 中进行测试,但它可能会根据环境产生问题。

application.rb 请求整棵树听起来是个坏主意,我不认为它可以这样保存。

在 Rails 中添加自定义 services/ 目录的最干净的方法是什么?它包含多个名称简单的子目录和类,它们也出现在应用程序的其他部分(模型、base.rb 等)

如何避免混淆 autoload_paths ?还有什么我不知道可以解决问题的东西吗?为什么 base.rb 甚至在这里崩溃?

最佳答案

工作解决方案

经过更深入的调查和尝试,我意识到我必须eager_load 服务以避免在调用诸如 const_get('MyClassWithModelName') 等元功能时获得错误的常量。

但事情是这样的:经典的 eager_load_paths 不会工作,因为出于某种原因,这些类显然会在整个 Rails 核心初始化之前加载,而简单的类名如 Base实际上会和核心混在一起,因此让一切崩溃。

有些人可能会说“然后将 Base 重命名为其他名称”,但我是否应该更改包装到 namespace 中的类名,因为 Rails 告诉我这样做?我不这么认为。 类名应该保持简单,我在自定义命名空间内所做的事情与 Rails 无关。

我必须仔细考虑并写下我自己的 Rails 配置钩子(Hook)。我们加载核心及其所有功能,然后递归地加载 service/

顺便说一下,它不会给生产环境增加任何负担,开发起来也很方便。

要添加的代码

将它放在 config/environment/development.rb 和所有其他你想要在没有 Rails 类冲突的情况下急切加载的环境(比如我的例子中的 test.rb)

# we eager load all services and subdirectories after Rails itself has been initializer
# why not use `eager_load_paths` or `autoload_paths` ? it makes conflict with the Rails core classes
# here we do eager them the same way but afterwards so it never crashes or has conflicts.
# see `initializers/after_eager_load_paths.rb` for more details
config.after_eager_load_paths = Dir[Rails.root.join('app', 'services', '**/')]

然后创建一个包含这个的新文件 initializers/after_eager_load_paths.rb

# this is a customized eager load system
# after Rails has been initialized and if the `after_eager_load_paths` contains something
# we will go through the directories recursively and eager load all ruby files
# this is to avoid constant mismatch on startup with `autoload_paths` or `eager_load_paths`
# it also prevent any autoload failure dû to deep recursive folders with subclasses
# which have similar name to top level constants.
Rails.application.configure do
if config.respond_to?(:after_eager_load_paths) && config.after_eager_load_paths.instance_of?(Array)
config.after_initialize do
config.after_eager_load_paths.each do |path|
Dir["#{path}/*.rb"].each { |file| require file }
end
end
end
end

像魅力一样工作。如果需要,您还可以通过 load 更改 require

关于ruby-on-rails - 自动加载路径和嵌套服务类在 Ruby 中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51566932/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com