Rails自动读取完全大写的名称,如API

时间:2022-05-05 20:24:03

I have some structure

我有一些结构

  • /lib/dokuwiki.rb
  • /lib/dokuwiki.rb
  • /lib/dokuwiki/exception.rb
  • /lib/dokuwiki/exception.rb
  • /lib/dokuwiki/api/connection.rb
  • /lib/dokuwiki/api/connection.rb

dokuwiki.rb

dokuwiki.rb

require 'dokuwiki/exception.rb'

module Dokuwiki
  ...

dokuwiki/api/connection.rb

dokuwiki / api / connection.rb

module Dokuwiki
  module API
    class Connection
      ...

Now, when I try to call Dokuwiki::API::Connection.new from a controller (without any require), Rails default constants autoloading fails. I believe this is because the ::API module should have a folder named /a_p_i/ instead of /api/ but that's ugly.

现在,当我尝试调用Dokuwiki::API::Connection时。新从控制器(没有任何要求),Rails默认常量自动读取失败。我认为这是因为:::API模块应该有一个名为/a_p_i/的文件夹,而不是/ API /,但这很难看。

Of course I could require 'dokuwiki/api/connection.rb', in the main '/lib/dokuwiki.rb' file, but then it wouldn't reload this class automatically (which is kinda annoying in a dev context)

当然,我需要“dokuwiki/api/连接”。rb,在main '/lib/dokuwiki中。rb的文件,但是它不会自动重载这个类(这在开发环境中有点烦人)

What can I do to keep the nice /api/ folder name and be able to do some live modifications to /lib/dokuwiki/api/connection.rb without having to restart my server ?

我可以做什么来保持nice /api/文件夹的名称,并能够对/lib/dokuwiki/api/connection进行一些实时修改。不需要重新启动服务器的rb ?

1 个解决方案

#1


29  

There's a file called config/initializers/inflections.rb.

有一个文件叫做config/initializers/ inflection. rb。

Add in it

加入它

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'API' 
end

and the API namespace will be available as a directory called api

API名称空间将作为一个名为API的目录可用

#1


29  

There's a file called config/initializers/inflections.rb.

有一个文件叫做config/initializers/ inflection. rb。

Add in it

加入它

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'API' 
end

and the API namespace will be available as a directory called api

API名称空间将作为一个名为API的目录可用