Class: Stack::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/stack/watcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator) ⇒ Watcher

Returns a new instance of Watcher.



9
10
11
12
13
14
15
16
17
# File 'lib/stack/watcher.rb', line 9

def initialize(generator)
  self.generator = generator
  self.source = self.generator.source
  self.target = self.generator.target
  
  self.keep_alive = true
  
  require 'directory_watcher'
end

Instance Attribute Details

#directory_watcherObject

Returns the value of attribute directory_watcher.



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

def directory_watcher
  @directory_watcher
end

#generatorObject

Returns the value of attribute generator.



4
5
6
# File 'lib/stack/watcher.rb', line 4

def generator
  @generator
end

#is_first_observeObject

Returns the value of attribute is_first_observe.



6
7
8
# File 'lib/stack/watcher.rb', line 6

def is_first_observe
  @is_first_observe
end

#keep_aliveObject

Returns the value of attribute keep_alive.



6
7
8
# File 'lib/stack/watcher.rb', line 6

def keep_alive
  @keep_alive
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/stack/watcher.rb', line 3

def source
  @source
end

#targetObject

Returns the value of attribute target.



3
4
5
# File 'lib/stack/watcher.rb', line 3

def target
  @target
end

#threadObject

Returns the value of attribute thread.



7
8
9
# File 'lib/stack/watcher.rb', line 7

def thread
  @thread
end

Instance Method Details

#observeObject



19
20
21
22
23
24
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
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/stack/watcher.rb', line 19

def observe
  dirs = ""
  
  Dir.chdir(self.source) do
    dirs = Dir['*'].select { |x| File.directory?(x) }
    dirs -= ['_stack']
    dirs = dirs.map { |x| "#{x}/**/*" }
    dirs += ['*']
  end
  
  self.directory_watcher = DirectoryWatcher.new(self.source)
  self.directory_watcher.interval = 1
  self.directory_watcher.glob = dirs
  
  self.directory_watcher.add_observer do |*args|
    if self.is_first_observe
      self.is_first_observe = false
    else
      time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
      puts "[#{time}] #{args.size} files changed."
      self.generator.process!
    
      self.generator.transform!
    
      time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
      puts "[#{time}] #{args.size} files processed."
    end
  end
  
  #self.directory_watcher.start
  
  self.thread = Thread.new {
    self.directory_watcher.start
  }
  
  trap("INT") { self.directory_watcher.stop }
  
  self.thread.join()
  
  #if self.keep_alive
  #  loop { sleep 1000 }
  #end
end