Module: RequireReloader

Defined in:
lib/require_reloader.rb,
lib/require_reloader/helper.rb,
lib/require_reloader/version.rb

Defined Under Namespace

Modules: ActionPackInfectorMethods Classes: Helper

Constant Summary collapse

VERSION =
"0.2.1"

Class Method Summary collapse

Class Method Details

.watch(gem_name, opts = {}) ⇒ Object

Reload a specific gem or a gem-like .rb file automatically on each request.

In Rails 3.2+, reload happens only when a watchable file is modified.

To use it, add ‘RequireReloader.watch :my_gem’ to your config/environments/development.rb.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/require_reloader.rb', line 34

def watch(gem_name, opts={})
  gem            = gem_name.to_s
  watchable_dir  = expanded_gem_path(gem, opts[:path])
  watchable_exts = opts[:exts] ? Array(opts[:exts]) : [:rb]
  helper         = Helper.new

  app = Object.const_get(Rails.application.class.parent_name)
  app::Application.configure do

    if watchable_dir && config.respond_to?(:watchable_dirs)
      config.watchable_dirs[watchable_dir] = watchable_exts
    end

    # based on Tim Cardenas's solution:
    # http://timcardenas.com/automatically-reload-gems-in-rails-327-on-eve
    helper.to_prepare do
      if opts[:module_name]
        helper.remove_module_if_defined(opts[:module_name])
      else
        helper.remove_gem_module_if_defined(gem)
      end
      $".delete_if {|s| s.include?(gem)}
      require gem
      opts[:callback].call(gem) if opts[:callback]
    end
  end
end

.watch_local_gems!Object Also known as: watch_all!

Reload all local gems (that is, ones which have a :path attribute) automatically on each request.

To use it, add ‘RequireReloader.watch_local_gems!’ to your config/environments/development.rb.



14
15
16
17
18
19
20
21
# File 'lib/require_reloader.rb', line 14

def watch_local_gems!
  local_gems.each do |gem|
    # never reload itself for now, causing error raised in integration test
    next if gem.name == 'require_reloader'

    watch gem.name, :path => gem.source.path.to_s, :module_name => gem.['module_name']
  end
end