Cyril Duchon-Doris Cyril Duchon-Doris - 1 month ago 11
Ruby Question

Rails autoloading fully capitalized name like API

I have some structure


  • /lib/dokuwiki.rb

  • /lib/dokuwiki/exception.rb

  • /lib/dokuwiki/api/connection.rb



dokuwiki.rb

require 'dokuwiki/exception.rb'

module Dokuwiki
...


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.

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)

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 ?

Answer

There's a file called config/initializers/inflections.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

Comments