Class: FileReload::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lre/file_reload.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#instance_nameObject

Returns the value of attribute instance_name.



9
10
11
# File 'lib/lre/file_reload.rb', line 9

def instance_name
  @instance_name
end

#on_every_fileObject

Returns the value of attribute on_every_file.



9
10
11
# File 'lib/lre/file_reload.rb', line 9

def on_every_file
  @on_every_file
end

#scriptObject

Returns the value of attribute script.



9
10
11
# File 'lib/lre/file_reload.rb', line 9

def script
  @script
end

Instance Method Details

#process_file(f, &b) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lre/file_reload.rb', line 26

def process_file(f,&b)
  if !FileTest.exists?(f)
    puts "file doesn't exist #{f}"
    return
  end
  if should_load?(f)
    on_every_file[File.expand_path(f)] if on_every_file
    puts "loading #{f}"
    b[f]
  else
    puts "didn't load #{f}"
  end
  print ">"
rescue => exp
  puts "error loading #{f}"
  puts exp.message + "\n" + exp.backtrace.join("\n")
rescue SyntaxError => exp
  puts "syntax error loading #{f}"
  puts exp.message + "\n" + exp.backtrace.join("\n")
rescue RuntimeError => exp
  puts "runtime error"
  puts exp.message
end

#run!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lre/file_reload.rb', line 60

def run!
  require 'watchr'
  require 'pathname'
  self.script = Watchr::Script.new
  watches.each do |file_match,proc|
    script.watch(file_match) do |md|
      f = md[0]
      process_file(f,&proc)
    end
  end
  
  self.threads.each { |x| x.kill }
  self.threads = []
  
  LRE.watch_dirs.uniq.each do |d|
    c = Watchr::Controller.new(script, Watchr.handler.new)
    c.base_path = d
    puts "adding thread #{d}"
    self.threads << Thread.new { c.run }
  end
end

#should_load?(f) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/lre/file_reload.rb', line 11

def should_load?(f)
  str = File.read(f)
  if str =~ /--dontload({.+})?/
    if !$1
      false
    else
      n = $1[1..-2]
      !(instance_name.to_s == n)
    end
  elsif str =~ /--onlyload{(.+)}/
    $1 == instance_name.to_s
  else
    true
  end
end

#stop!Object



49
50
51
52
# File 'lib/lre/file_reload.rb', line 49

def stop!
  self.threads.each { |x| x.kill }
  self.threads = []
end