Class: Raw::Reloader

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

Overview

Autoreload classes and templates. Mainly useful while debugging. Should be turned off in production servers to avoid the severe performance penalty.

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ Reloader

Returns a new instance of Reloader.



9
10
11
12
# File 'lib/raw/compiler/reloader.rb', line 9

def initialize(application)
  @application = application
  @mtimes = Hash.new(Time.now)
end

Instance Method Details

#is_dirty?(file) ⇒ Boolean

Is a file modified on disk?

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
# File 'lib/raw/compiler/reloader.rb', line 71

def is_dirty?(file)
  if (mtime = File.stat(file).mtime) > @mtimes[file]
    Logger.debug "File '#{file}' is modified" if $DBG
    @mtimes[file] = mtime
    return true
  else
    return false
  end
end

#reload_controllersObject

When a template is modified, remove all generated methods from the controllers.



84
85
86
87
88
89
90
91
92
# File 'lib/raw/compiler/reloader.rb', line 84

def reload_controllers
  controllers = @application.dispatcher.controllers.values
  
  for c in controllers
    for m in c.instance_methods.grep(/(___control$)|(___view$)/)
      c.send(:remove_method, m) rescue nil
    end
  end
end

#start(interval) ⇒ Object

Start the monitor thread. This thread monitors code and template files. – gmosx: the thread accesses the reloader variables through the closure. ++



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
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/raw/compiler/reloader.rb', line 21

def start(interval)
  @interval = interval
  @thread = Thread.new do 
    begin
      loop do
        sleep(@interval)

        dirty = false          

        # Check code files.
        
        for feature in $LOADED_FEATURES
          for path in $LOAD_PATH
            file = File.join(path, feature)
            if File.exist?(file) and is_dirty?(file)
              begin
                dirty = true
                load(feature)
              rescue Exception => ex
                Logger.info ex.inspect
              end
            end
          end
        end

        # Check template files.
        
        for template in @application.compiler.templates
          if is_dirty? template
            dirty = true
            break
          end
        end
        
        reload_controllers() if dirty
      end # loop
    rescue => ex
      Logger.info ex
    end
  end
end

#stopObject

Stop the monitor thread.



65
66
67
# File 'lib/raw/compiler/reloader.rb', line 65

def stop
  @thread.exit
end