Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 265
Cyril Duchon-Doris Avatar asked Mar 11 '15 15:03

Cyril Duchon-Doris


1 Answers

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

like image 199
gmaliar Avatar answered Oct 13 '22 12:10

gmaliar