Search code examples
ruby-on-railsinitializer

Rails initializer - what is wrong


This is my first initializer and I do not understand what is wrong.

config\initializers\other_server.rb:

OtherServer.setup do |config|
  config.first_server_login = 'qwertyuiop'
  config.first_server_password = '12345'
end

lib\other_server_base.rb:

module OtherServer
  mattr_accessor :first_server_login
  @@first_server_login = nil

  mattr_accessor :first_server_password
  @@first_server_password = nil

  def self.setup
    yield self
  end

  class Base
    def self.session_id
      # ................
      # How to access first_server_login and first_server_password here?
      res = authenticate({
          login: first_server_login,
          pass: first_server_password })
      # ................
    end
  end
end

lib\other_server.rb:

module OtherServer
  class OtherServer < Base
    # ................
  end
end

How to access first_server_login and first_server_password in OtherServer::Base class?

If I call OtherServer.first_server_login it complains about absence of OtherServer::OtherServer.first_server_login member.


Solution

  • I've found the right answer:

    A module and a class inside of it CAN have same names. In my case the module members should be referenced as:

    ::OtherServer.first_server_login
    

    That's all.