Sha256: e77e64c7ebf66d194bb5564514fa2d1ed92dbc521f2aec185149d0944fb8c5a2

Contents?: true

Size: 1.51 KB

Versions: 8

Compression:

Stored size: 1.51 KB

Contents

module Perennial
  class Reloading
    
    cattr_accessor :mapping, :mtimes
    self.mapping = {}
    self.mtimes = {}
    
    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
    
    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
    
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
Sutto-perennial-0.2.4.5 lib/perennial/reloading.rb
Sutto-perennial-0.2.4.6 lib/perennial/reloading.rb
Sutto-perennial-1.0.0.0 lib/perennial/reloading.rb
Sutto-perennial-1.0.0.1 lib/perennial/reloading.rb
Sutto-perennial-1.0.0.2 lib/perennial/reloading.rb
perennial-1.0.1 lib/perennial/reloading.rb
perennial-1.0.0.2 lib/perennial/reloading.rb
perennial-1.0.0.1 lib/perennial/reloading.rb