Last active
January 4, 2016 21:09
-
-
Save garybernhardt/8678679 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Goal: put a non-Rails-aware Ruby library using normal `require`s in | |
# lib/sim. Have it transparently reloaded between requests like Rails app | |
# code is. | |
# | |
# The code here goes inside of your configure block in | |
# config/environments/development.rb. There are two parts, commented inline. | |
# Reload code whenever the simulator changes. | |
config.watchable_dirs["lib/sim"] = [:rb] | |
config.watchable_files << "lib/sim.rb" | |
# Manually unload and reload the simulator before every request. This assumes | |
# that there's a lib/sim.rb loading files from lib/sim/. | |
ActionDispatch::Reloader.to_prepare do | |
# If the library's top level module is currently loaded, unload it | |
if Object.const_defined?(:Sim) | |
Object.send(:remove_const, :Sim) | |
end | |
# Instruct ruby to "unrequire" all of the simulator's files. | |
# CAREFUL: make sure this only matches your library's files. | |
sim_base = File.expand_path(File.join(File.dirname(__FILE__), '../../lib/sim')) | |
$".delete_if do |s| | |
is_in_sim_dir = s.start_with?(sim_base + "/") | |
is_sim_file = s == sim_base + ".rb" | |
is_in_sim_dir || is_sim_file | |
end | |
# Re-require your library | |
# Note: because we removed all files previously required they will be reloaded | |
# even if you didn't use load/autoload in your library. | |
require_relative "../../lib/sim" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think @BrokenLadder is saying that objects which inherit from your lib might not act as expected.