Module: GemReloader

Defined in:
lib/gem_reloader.rb,
lib/gem_reloader/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.watch(gem_symbol) ⇒ Object

Watch a specific gem. Add something like ‘GemReloader.watch :my_gem’ to your development.rb.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gem_reloader.rb', line 18

def self.watch(gem_symbol)
  
  gem_path = gem_symbol.to_s
  klass = gem_path.classify
  
  app = Object.const_get(Rails.application.class.parent_name)
  app::Application.configure do
  
    ActionDispatch::Callbacks.to_prepare do
      
      # This code (slightly modified) comes almost entirely from
      # Tim Cardenas - http://timcardenas.com/automatically-reload-gems-in-rails-327-on-eve

      # If the gem's top level module is currently loaded, unload it
      if Object.const_defined?(klass)
        Object.send(:remove_const, klass)
      end

      # Instruct ruby to "unrequire" all of the gems files.
      # CAREFUL: make sure this only matches your gems files.
      $".delete_if {|s| s.include?(gem_path)}

      # Re-require your gem 
      # Note: because we removed all files previously required they will be reloaded
      # even if you didn't use load/autoload in your gem.
      require gem_path

    end
    
  end
end

.watch_all!Object

All local gems (that is, ones which have a :path attribute) will be automatically reloaded on each request.

Add something like ‘GemReloader.watch_all!’ to your development.rb.



10
11
12
13
14
# File 'lib/gem_reloader.rb', line 10

def self.watch_all!
  reloadable_gems.each do |gem|
    watch gem[:name]
  end
end