Class: AutoReload::Reloader
- Inherits:
-
Object
- Object
- AutoReload::Reloader
- Defined in:
- lib/autoreload/reloader.rb
Overview
Reload class does all the heavy lifting for AutoReload library.
Constant Summary collapse
- DEFAULT_INTERVAL =
Default interval is one second.
1
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
List of files provided to autoreload.
-
#interval ⇒ Object
readonly
The periodic interval of reload in seconds.
-
#status ⇒ Object
readonly
Status hash, used to track reloads.
-
#thread ⇒ Object
readonly
References the reload thread.
Class Method Summary collapse
-
.start(*args, &block) ⇒ Object
Shortcut for Reloader.new(*args).start.
Instance Method Summary collapse
-
#check(lib) ⇒ file, mtime
private
Check status and reload if out-of-date.
-
#get_status(file) ⇒ file, mtime
private
Get library file status.
-
#initialize(options = {}, &block) ⇒ Reloader
constructor
New Reloader.
-
#libraries ⇒ Object
private
The library files to autoreload.
-
#reprime? ⇒ Boolean
Put $0 in the reload list?.
-
#start ⇒ Object
Start the reload thread.
-
#stop ⇒ Object
Kills the autoreload thread.
-
#update ⇒ Object
private
Iterate through all selection library files and reload if needed.
-
#verbose? ⇒ Boolean
Provide warning on reload.
Constructor Details
#initialize(options = {}, &block) ⇒ Reloader
New Reloader.
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 |
# File 'lib/autoreload/reloader.rb', line 25 def initialize(={}, &block) @interval = ([:interval] || DEFAULT_INTERVAL).to_i @verbose = ([:verbose]) @reprime = ([:reprime]) @watch_gems = ([:watch_gems]) @status = {} features = $".dup if block block.call @files = ($" - features).select do |f| (@watch_gems ? true : f.start_with?(Dir.pwd)) && f.end_with?(".rb") end.reverse if @verbose puts "watching files:" puts "---------------" puts @files puts "---------------" end else @files = [] end end |
Instance Attribute Details
#files ⇒ Object (readonly)
List of files provided to autoreload.
77 78 79 |
# File 'lib/autoreload/reloader.rb', line 77 def files @files end |
#interval ⇒ Object (readonly)
The periodic interval of reload in seconds.
83 84 85 |
# File 'lib/autoreload/reloader.rb', line 83 def interval @interval end |
#status ⇒ Object (readonly)
Status hash, used to track reloads.
80 81 82 |
# File 'lib/autoreload/reloader.rb', line 80 def status @status end |
#thread ⇒ Object (readonly)
References the reload thread.
74 75 76 |
# File 'lib/autoreload/reloader.rb', line 74 def thread @thread end |
Class Method Details
.start(*args, &block) ⇒ Object
Shortcut for Reloader.new(*args).start.
11 12 13 |
# File 'lib/autoreload/reloader.rb', line 11 def self.start(*args, &block) self.new(*args, &block).start end |
Instance Method Details
#check(lib) ⇒ file, mtime (private)
Check status and reload if out-of-date.
We can’t check mtime under 1.8 b/c $LOADED_FEATURES does not store the full path.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/autoreload/reloader.rb', line 125 def check(lib) if RUBY_VERSION < '1.9' warn "reload: '#{lib}'" if verbose? begin load lib rescue LoadError # file has been removed end else file, mtime = @status[lib] if file if FileTest.exist?(file) curtime = File.mtime(file).to_i if mtime < curtime warn "reload: '#{file}'" if verbose? load file @status[lib] = [file, curtime] end else # file has been removed end else @status[lib] = get_status(lib) end end end |
#get_status(file) ⇒ file, mtime (private)
Get library file status.
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/autoreload/reloader.rb', line 157 def get_status(file) if FileTest.exist?(file) [file, File.mtime(file).to_i] else warn "reload fail: library '#{file}' not found" if verbose? #raise "The library '#{file}' is not found." #$stdout.puts(message("The library '#{file}' is not found.")) if @verbose [nil, nil] end end |
#libraries ⇒ Object (private)
The library files to autoreload.
104 105 106 107 108 109 110 |
# File 'lib/autoreload/reloader.rb', line 104 def libraries if @files.empty? @reprime ? [$0] + $" : $" else @files end end |
#reprime? ⇒ Boolean
Put $0 in the reload list?
95 96 97 |
# File 'lib/autoreload/reloader.rb', line 95 def reprime? @reprime end |
#start ⇒ Object
Start the reload thread.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/autoreload/reloader.rb', line 52 def start update # prime the path loads @thread = Thread.new do loop do begin update rescue Exception warn "autoreload failed unexpectedly: #{$!}" end sleep @interval end end @thread.abort_on_exception = true @thread end |
#stop ⇒ Object
Kills the autoreload thread.
69 70 71 |
# File 'lib/autoreload/reloader.rb', line 69 def stop @thread.kill if @thread end |
#update ⇒ Object (private)
Iterate through all selection library files and reload if needed.
113 114 115 |
# File 'lib/autoreload/reloader.rb', line 113 def update libraries.each{ |lib| check(lib) } end |
#verbose? ⇒ Boolean
Provide warning on reload.
88 89 90 |
# File 'lib/autoreload/reloader.rb', line 88 def verbose? @verbose || $VERBOSE end |