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.
-
#status ⇒ Object
readonly
Status hash, used to track reloads.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
-
.start(*args, &block) ⇒ Object
Shortcut for Reloader.new(*args).start.
Instance Method Summary collapse
-
#check(lib) ⇒ Object
private
Check status and reload if out-of-date.
-
#get_status(file) ⇒ Object
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.
Options
:interval - seconds between updates
:verbose - true provides reload warning
:reprime - include $0 in reload list
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/autoreload/reloader.rb', line 26 def initialize(={}, &block) @interval = [:interval] || DEFAULT_INTERVAL @verbose = [:verbose] @reprime = [:reprime] @status = {} features = $".dup if block block.call @files = ($" - features).reverse else @files = [] end end |
Instance Attribute Details
#files ⇒ Object (readonly)
List of files provided to autoreload.
67 68 69 |
# File 'lib/autoreload/reloader.rb', line 67 def files @files end |
#interval ⇒ Object (readonly)
The periodic interval of reload.
73 74 75 |
# File 'lib/autoreload/reloader.rb', line 73 def interval @interval end |
#status ⇒ Object (readonly)
Status hash, used to track reloads.
70 71 72 |
# File 'lib/autoreload/reloader.rb', line 70 def status @status end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
64 65 66 |
# File 'lib/autoreload/reloader.rb', line 64 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) ⇒ Object (private)
Check status and reload if out-of-date.
117 118 119 120 121 122 123 124 |
# File 'lib/autoreload/reloader.rb', line 117 def check(lib) warn "reload: '#{lib}'" if verbose? begin load lib rescue LoadError # file has been removed end end |
#get_status(file) ⇒ Object (private)
Get library file status.
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/autoreload/reloader.rb', line 137 def get_status(file) if FileTest.exist?(file) [file, File.mtime(file).to_i] else warn "reload fail: library '#{lib}' not found" if verbose? #raise "The library '#{lib}' is not found." #$stdout.puts(message("The library '#{lib}' is not found.")) if @verbose [nil, nil] end end |
#libraries ⇒ Object (private)
The library files to autoreload. – ISSUE: Why include $0 ? –
91 92 93 94 95 96 97 |
# File 'lib/autoreload/reloader.rb', line 91 def libraries if @files.empty? @reprime ? [$0] + $" : $" else @files end end |
#reprime? ⇒ Boolean
Put $0 in the reload list?
81 82 83 |
# File 'lib/autoreload/reloader.rb', line 81 def reprime? @reprime end |
#start ⇒ Object
Start the reload thread.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/autoreload/reloader.rb', line 43 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 end |
#stop ⇒ Object
Kills the autoreload thread.
59 60 61 |
# File 'lib/autoreload/reloader.rb', line 59 def stop @thread.kill if @thread end |
#update ⇒ Object (private)
Iterate through all selection library files and reload if needed.
100 101 102 |
# File 'lib/autoreload/reloader.rb', line 100 def update libraries.each{ |lib| check(lib) } end |
#verbose? ⇒ Boolean
Provide warning on reload.
76 77 78 |
# File 'lib/autoreload/reloader.rb', line 76 def verbose? @verbose || $VERBOSE end |