Class: CoffeeWithoutNodejs::CoffeeWatcher

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/coffee_without_nodejs/watcher.rb

Instance Method Summary collapse

Constructor Details

#initializeCoffeeWatcher

Returns a new instance of CoffeeWatcher.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/coffee_without_nodejs/watcher.rb', line 11

def initialize
  @notifier = INotify::Notifier.new
  @path = File.expand_path('.')

  Signal.trap(2) {|sig| Process.exit }

  start_watch_files

  @notifier.watch(@path, :moved_from, :moved_to, :create, :delete, :onlydir, :recursive) do |event|
    # skip temp file.
    next if event.name =~ /^\.#|^#.*\#$/

      start_watch_files
  end

  coffee_files.each do |file|
    CoffeeCompiler.compile_file(file, true, true)
  end

  # start loop.
  @notifier.run
  puts 'CoffeeWatcher start successful.'
end

Instance Method Details

#coffee_filesObject



44
45
46
47
48
49
50
# File 'lib/coffee_without_nodejs/watcher.rb', line 44

def coffee_files
  all_files = Dir["#@path/**/*.coffee"]
  if ENV['COFFEE_NOT_WATCH_PATTERN']
    all_files.reject! {|e| e =~ Regexp.union(ENV['COFFEE_NOT_WATCH_PATTERN']) }
  end
  all_files
end

#start_watch_filesObject

watch all exist files modify event.



36
37
38
39
40
41
42
# File 'lib/coffee_without_nodejs/watcher.rb', line 36

def start_watch_files
  coffee_files.each do |file|
    @notifier.watch(file, :modify) do
      CoffeeCompiler.compile_file(file, true, true)
    end
  end
end