Class: Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/insup/listener.rb

Instance Method Summary collapse

Constructor Details

#initialize(base, settings) ⇒ Listener

Returns a new instance of Listener.



5
6
7
8
# File 'lib/insup/listener.rb', line 5

def initialize(base, settings)
  @base = base
  @settings = settings
end

Instance Method Details

#listen(&block) ⇒ Object



10
11
12
13
14
15
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
# File 'lib/insup/listener.rb', line 10

def listen(&block)
  return if @listener

  @listener = Listen.to(tracked_locations, force_polling: @settings.options['force_polling']) do |modified, added, removed|
    flags = {}

    added.each do |file|
      flags[file] = flags[file] ? flags[file] | 4 : 4
    end

    modified.each do |file|
      flags[file] = flags[file] ? flags[file] | 2 : 2
    end

    removed.each do |file|
      flags[file] = flags[file] ? flags[file] | 1 : 1
    end

    res = []

    flags.each do |f, flag|
      pn = Pathname.new(f)
      basepn = Pathname.new(@base)
      file = pn.relative_path_from(basepn).to_s
      next if ignore_matcher.matched?(file)
      tracked_file = create_tracked_file(flag, file)
      res << tracked_file if !tracked_file.nil?
    end
    yield res
  end

  @listener.start
end

#stopObject



44
45
46
47
48
# File 'lib/insup/listener.rb', line 44

def stop
  if @listener
    @listener.stop
  end
end