There is an answer to the question about how to make classes in lib
folder be reloaded in development mode without having to restart server and worked partially.
So, I inserted following lines to development.rb
file:
ActiveSupport::Dependencies.autoload_paths << File::join( Rails.root, 'lib')
ActiveSupport::Dependencies.explicitly_unloadable_constants.concat(['MyClass1', 'MyClass2'])
and yes, the class is reloaded on first source change, but on second change I get an exception:
Circular dependency detected while autoloading constant MyClass2
Why the exception is thrown about MyClass2
and not MyClass1
? Because I instantiate it directly, and MyClass1
is used by MyClass2
later.
Here is some code:
The classes in lib\my_class1.rb
:
require 'httpclient/include_client'
class MyClass2
attr_reader :id, :login, :money, :info
def initialize(parameters)
@id = parameters['idUsers']
@login = parameters['login']
@money = parameters['money']
@info = parameters['info']
end
end
class MyClass1
def initialize(parameters)
@base_url = parameters[:base_url]
@version = parameters[:version]
end
def get_test_data
{ huhu: "testvalue7"}
end
end
Controller:
require 'my_class1'
class MyClassTestController < ApplicationController
skip_before_filter :authenticate_user!
def get_test_data_test
ls = MyClass1.new({ base_url: 'http://192.168.0.25', version: '1'} )
@res = ls.get_test_data
end
end