- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我在 Rails 5 项目的 app/services
文件夹下有多个加载/需要类的问题,我开始放弃这个问题。
首先要明确的是,services/
是我在整个项目中使用的简单 PORO 类,用于从 Controller 、模型等中抽象出大部分业务逻辑。
树看起来像这样
app/
services/
my_service/
base.rb
funny_name.rb
my_service.rb
models/
funny_name.rb
首先,当我尝试使用 MyService.const_get('FunnyName')
时,它从我的模型目录中获取了 FunnyName
。当我直接执行 MyService::FunnyName
时,它似乎没有相同的行为,但在我的大多数测试和更改中,它工作正常,这很奇怪。
我意识到 Rails config.autoload_paths
不会递归加载东西;第一个被捕获的 FunnyName
是 models/funny_name.rb
是有道理的,因为它确实被加载了,但另一个没有。
没关系,让我们找到解决方法。我将其添加到我的 application.rb
中:
config.autoload_paths += Dir[Rails.root.join('app', 'services', '**/')]
这会将服务的所有子目录添加到 config.autoload_paths
中。显然不推荐从 Rails 5 开始写这样的东西;但这个想法对我来说确实是正确的。
现在,当我启动我的应用程序时,它崩溃并输出类似这样的内容
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/
我有一段代码看起来像这样: void update_clock(uint8_t *time_array) { time_t time = *((time_t *) &time_array[0]
应用程序崩溃了 :( 请帮助我.. 在这方面失败了。我找不到错误?该应用程序可以连接到 iTunesConnect 但它会出错。 谁能根据下面的崩溃报告判断问题出在哪里? share_with_app
小二是新来的实习生,作为技术 leader,我给他安排了一个非常简单的练手任务,把前端 markdown 编辑器里上传的图片保存到服务器端,结果他真的就把图片直接保存到了服务器上,这下可把我气坏了,就
我正在创建一个函数,它将目录路径作为参数传递,或者如果它留空,则提示用户输入。 我已经设置了我的 PATH_MAX=100 和 if 语句来检查 if ((strlen(folder path) +
我已将“arial.ttf”文件(从我的/Windows/Fonts 文件夹中获取)加载到内存中,但是将其传递到 FT_New_Memory_Face 时会崩溃(在 FT_Open_Face 中的某处
我正在尝试在我的计算机上的两个控制台之间进行 rtsp 流。 在控制台 1 上,我有: ffmpeg -rtbufsize 100M -re -f dshow -s 320x240 -i video=
我正在尝试使用 scio_beast在一个项目中。我知道它还没有完成,但这并不重要。我已经设法让它工作得很好。 我现在正在尝试连接到 CloudFlare 后面的服务器,我知道我需要 SNI 才能工作
我有一个带有关联宏的下拉列表,如下所示: Sub Drop() If Range("Hidden1!A1") = "1" Then Sheets("Sheet1").Se
我对 bash 很陌生。我要做的就是运行这个nvvp -vm /usr/lib64/jvm/jre-1.8.0/bin/java无需记住最后的路径。我认为 instafix 就是这样做...... n
我在 Windows 上使用 XAMPP 已经两年左右了,它运行完美,没有崩溃没有问题。 (直到四个月前。) 大约四个月前,我们将服务器/系统升级到了更快的规范。 这是旧规范的内容 - Windows
我面临着一个非常烦人的 android 崩溃,它发生在大约 1% 的 PRODUCTION session 中,应用程序始终在后台运行。 Fatal Exception: android.app.Re
尝试使用下面的函数: public void createObjectType() { try { mCloudDB.createObjectType(ObjectTypeIn
由于我正在进行的一个项目,我在 CF11 管理员中弄乱了类路径,我设法使服务器崩溃,以至于我唯一得到的是一个漂亮的蓝屏和 500 错误.我已经检查了日志,我会把我能做的贴在帖子的底部,但我希望有人会启
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 10 个月前关闭。 Improve
我最近从 xcode 3.x 更新到 4.2,当我在 4.2 中运行应用程序时,我遇到了核心数据问题。我还更新到了 iOS 5,所以问题可能就在那里,我不太确定。 这些应用程序在 3.x 中运行良好,
我是一个相对较新的 iPhone 应用程序开发人员,所以我的知识有点粗略,所以如果这是一个微不足道的问题,请原谅我。 我有一个导航应用程序,它通过在navigationController对象上调用p
if ([MFMailComposeViewController canSendMail]) { MFMailComposeViewController *mailViewController
你能帮我吗? 我正在设置 UILocalNotification,当我尝试设置其 userInfo 字典时,它崩溃了。 fetchedObjects 包含 88 个对象。 这是代码: NSDi
为什么我的代码中突然出现 NSFastEnumeration Mutation Handler 崩溃。我很茫然为什么会突然出现这个崩溃以及如何解决它。 最佳答案 崩溃错误: **** 由于未捕获的异常
当我从表中删除行时,我的应用程序崩溃了。这是我检测到错误和堆栈跟踪的来源。谢谢! //delete row from database - (void)tableView:(UITableView *
我是一名优秀的程序员,十分优秀!