Class: Trestle::Reloader

Inherits:
Object
  • Object
show all
Defined in:
lib/trestle/reloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(files, dirs = {}) ⇒ Reloader

Returns a new instance of Reloader.



5
6
7
# File 'lib/trestle/reloader.rb', line 5

def initialize(files, dirs = {})
  @files, @dirs = files, dirs
end

Instance Method Details

#clearObject



28
29
30
# File 'lib/trestle/reloader.rb', line 28

def clear
  Trestle.registry.reset!
end

#install(app) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/trestle/reloader.rb', line 36

def install(app)
  reloader = self

  app.reloaders << reloader

  app.reloader.to_run do
    reloader.execute_if_updated
  end

  reloader.execute
end

#load_pathsObject



32
33
34
# File 'lib/trestle/reloader.rb', line 32

def load_paths
  Trestle.config.load_paths.map { |path| path.respond_to?(:call) ? path.call : path }.flatten.map(&:to_s)
end

#updaterObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/trestle/reloader.rb', line 9

def updater
  @updater ||= ActiveSupport::FileUpdateChecker.new(@files, @dirs) do
    begin
      clear

      load_paths.each do |load_path|
        matcher = /\A#{Regexp.escape(load_path.to_s)}\/(.*)\.rb\Z/
        Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
          require_dependency file.sub(matcher, '\1')
        end
      end
    ensure
      # Ensure that routes are reloaded even if an exception occurs
      # when reading an admin definition file.
      Rails.application.reload_routes!
    end
  end
end