Class: Perennial::Reloading

Inherits:
Object show all
Includes:
Loggable
Defined in:
lib/perennial/reloading.rb

Class Method Summary collapse

Methods included from Loggable

included, #logger

Class Method Details

.reload!Object



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
# File 'lib/perennial/reloading.rb', line 18

def self.reload!
  self.mapping.each_pair do |constant, file|
    next unless File.mtime(file) > self.mtimes[file]
    begin
      # Get a relative name and namespace
      parts = constant.split("::")
      name = parts.pop
      ns = parts.inject(Object) { |a, c| a.const_get(c) }
      # Notify object pre-reload.
      final = ns.const_get(name)
      final.reloading! if final.respond_to?(:reloading!)
      final = nil
      # Remove the constant
      ns.send(:remove_const, name)
      load(file)
      # Notify the object it was reloaded...
      final = ns.const_get(name)
      final.reloaded! if final.respond_to?(:reloaded!)
      # Finally, update the mtime
      self.mtimes[file] = File.mtime(file)
    rescue Exception => e
      logger.fatal "Exception reloading #{file} (for #{constant})"
      Perennial::ExceptionTracker.log(e)
    end
  end
end

.watch(file, relative_to = File.dirname(file)) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
# File 'lib/perennial/reloading.rb', line 9

def self.watch(file, relative_to = File.dirname(file))
  file = File.expand_path(file)
  raise ArgumentError, "You must provide the path to a file" unless File.file?(file)
  relative = file.gsub(/^#{File.expand_path(relative_to)}\//, '')
  name = relative.gsub(/\.rb$/, '').split("/").map { |part| part.camelize }.join("::")
  self.mapping[name] = file
  self.mtimes[file] = File.mtime(file)
end