Class: RbReloader::FileWatcher

Inherits:
Array
  • Object
show all
Defined in:
lib/rb_reloader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ FileWatcher

Returns a new instance of FileWatcher.



20
21
22
23
24
# File 'lib/rb_reloader.rb', line 20

def initialize(file)
  @reload, @file = true, file
  @mtime = File.exist?(file) ? File.mtime(file) : Time.at(0)
  super()
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/rb_reloader.rb', line 5

def file
  @file
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



5
6
7
# File 'lib/rb_reloader.rb', line 5

def mtime
  @mtime
end

Class Method Details

.each(&block) ⇒ Object



16
17
18
# File 'lib/rb_reloader.rb', line 16

def self.each(&block)
  @map.values.each(&block) 
end

.new(file) ⇒ Object Also known as: []



8
9
10
# File 'lib/rb_reloader.rb', line 8

def self.new(file)
  @map[file] ||= super      
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rb_reloader.rb', line 30

def changed?
  !File.exist? file or @mtime != File.mtime(file)
end

#reloadObject



34
35
36
# File 'lib/rb_reloader.rb', line 34

def reload
  reload! if reload?
end

#reload!Object



38
39
40
41
42
43
44
45
# File 'lib/rb_reloader.rb', line 38

def reload!
  $LOADED_FEATURES.delete file
  clear
  if File.exist? file
    @mtime = File.mtime(file)
    require file
  end
end

#reload?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/rb_reloader.rb', line 26

def reload?
  @reload and changed?
end