Class: AutoReload::Reloader

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Reloader

New Reloader.

Parameters:

  • options (defaults to: {})

    The Hash options used to refine the reloader (default: {}):

Options Hash (options):

  • :interval (Object)

    Seconds between updates.

  • :verbose (Object)

    True provides reload warning.

  • :reprime (Object)

    Include $0 in reload list.



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(options={}, &block)
  @interval   = (options[:interval] || DEFAULT_INTERVAL).to_i
  @verbose    = (options[:verbose])
  @reprime    = (options[:reprime])
  @watch_gems = (options[: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

#filesObject (readonly)

List of files provided to autoreload.



77
78
79
# File 'lib/autoreload/reloader.rb', line 77

def files
  @files
end

#intervalObject (readonly)

The periodic interval of reload in seconds.



83
84
85
# File 'lib/autoreload/reloader.rb', line 83

def interval
  @interval
end

#statusObject (readonly)

Status hash, used to track reloads.



80
81
82
# File 'lib/autoreload/reloader.rb', line 80

def status
  @status
end

#threadObject (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.

Parameters:

  • lib

    A library file.

Returns:

  • (file, mtime)

    Returns Array of file, mtime.



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.

Parameters:

  • file

    The file path from which to get status.

Returns:

  • (file, mtime)

    Returns Array of file, mtime or [nil, nil] if file not found.



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

#librariesObject (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?

Returns:

  • (Boolean)

    true/false whether to include $0.



95
96
97
# File 'lib/autoreload/reloader.rb', line 95

def reprime?
  @reprime
end

#startObject

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

#stopObject

Kills the autoreload thread.



69
70
71
# File 'lib/autoreload/reloader.rb', line 69

def stop
  @thread.kill if @thread
end

#updateObject (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.

Returns:

  • (Boolean)

    true/false if versboe mode.



88
89
90
# File 'lib/autoreload/reloader.rb', line 88

def verbose?
  @verbose || $VERBOSE
end