Class: Listener

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

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Listener

Returns a new instance of Listener.



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

def initialize(base)
  @base = base
end

Instance Method Details

#listen(&block) ⇒ Object



9
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/insup/listener.rb', line 9

def listen(&block)
  return if @listener

  @listener = Listen.to(tracked_locations) 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, flags|
      pn = Pathname.new(f)
      basepn = Pathname.new(@base)
      file = pn.relative_path_from(basepn).to_s

      next if ignore_matcher.matched?(file)

      case flags
      when 1
        res << Insup::TrackedFile.new(file, Insup::TrackedFile::DELETED)
      when 2
        res << Insup::TrackedFile.new(file, Insup::TrackedFile::MODIFIED)
      when 3
        res << Insup::TrackedFile.new(file, Insup::TrackedFile::DELETED)
      when 4
        res << Insup::TrackedFile.new(file, Insup::TrackedFile::NEW)
      when 5
        if File.exist?(file)
          res << Insup::TrackedFile.new(file, Insup::TrackedFile::UNSURE)
        end
      when 6
        res << Insup::TrackedFile.new(file, Insup::TrackedFile::NEW)
      when 7
        if File.exist?(file)
          res << Insup::TrackedFile.new(file, Insup::TrackedFile::UNSURE)
        end
      end
    end

    yield res
  end

  @listener.start
end

#stopObject



64
65
66
67
68
# File 'lib/insup/listener.rb', line 64

def stop
  if @listener
    @listener.stop
  end
end