Class: Goat::Autoreload
Class Method Summary collapse
Instance Method Summary collapse
- #filter_loadpaths ⇒ Object
-
#initialize(dir, interval) ⇒ Autoreload
constructor
A new instance of Autoreload.
- #maybe_reload ⇒ Object
- #reload ⇒ Object
- #schedule ⇒ Object
- #update_cache ⇒ Object
- #watchpaths ⇒ Object
Constructor Details
#initialize(dir, interval) ⇒ Autoreload
Returns a new instance of Autoreload.
46 47 48 49 50 |
# File 'lib/goat/autoreload.rb', line 46 def initialize(dir, interval) @dir = dir @interval = interval @cache = {} end |
Class Method Details
.command ⇒ Object
42 43 44 |
# File 'lib/goat/autoreload.rb', line 42 def self.command @command || "ruby #{$0} #{ARGV.join(' ')}" end |
.command=(c) ⇒ Object
38 39 40 |
# File 'lib/goat/autoreload.rb', line 38 def self.command=(c) @command = c end |
.full_loadpaths ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/goat/autoreload.rb', line 5 def self.full_loadpaths paths = [] $LOADED_FEATURES.each do |f| next if f =~ /\.(so|bundle)$/ if f =~ /^\// paths << f else maybe = [] $LOAD_PATH.each do |p| if File.exists?(File.join(p, f)) maybe << File.(File.join(p, f)) end end if maybe.empty? $stderr.puts "Couldn't find #{f}" elsif maybe.size > 1 # ignore ambiguous files -- usually a gem or something else paths << maybe.first end end end paths end |
.schedule(dir, interval) ⇒ Object
34 35 36 |
# File 'lib/goat/autoreload.rb', line 34 def self.schedule(dir, interval) self.new(dir, interval).schedule end |
Instance Method Details
#filter_loadpaths ⇒ Object
52 53 54 |
# File 'lib/goat/autoreload.rb', line 52 def filter_loadpaths self.class.full_loadpaths.select {|f| f.downcase.start_with?(@dir.downcase)} end |
#maybe_reload ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/goat/autoreload.rb', line 72 def maybe_reload watchpaths.each do |path| if File.stat(path).mtime > @cache[path] $stderr.puts "Reloading after change in #{path}..." reload end end end |
#reload ⇒ Object
66 67 68 69 70 |
# File 'lib/goat/autoreload.rb', line 66 def reload EM.stop IO.closefrom(3) Kernel.exec(self.class.command) end |
#schedule ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/goat/autoreload.rb', line 81 def schedule update_cache EM.next_tick do EM.add_periodic_timer(@interval) do maybe_reload end end end |
#update_cache ⇒ Object
60 61 62 63 64 |
# File 'lib/goat/autoreload.rb', line 60 def update_cache watchpaths.each do |path| @cache[path] = File.stat(path).mtime end end |
#watchpaths ⇒ Object
56 57 58 |
# File 'lib/goat/autoreload.rb', line 56 def watchpaths @paths ||= filter_loadpaths end |