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.



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

def initialize(generator)
  self.generator = generator
  self.source = self.generator.source
  self.target = self.generator.target
  
  self.keep_alive = true
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

#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

Instance Method Details

#observeObject



16
17
18
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
# File 'lib/stack/watcher.rb', line 16

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|
    self.generator.process!
    self.generator.transform!
    
    time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
    puts "[#{time}] #{args.size} files changed and processed."
  end
  
  self.directory_watcher.start
  
  if self.keep_alive
    loop { sleep 1000 }
  end
end