Class: Vagrant::Syncer::Listeners::Listen

Inherits:
Object
  • Object
show all
Defined in:
lib/syncer/listeners/listen.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths, excludes, settings, callback) ⇒ Listen

Returns a new instance of Listen.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/syncer/listeners/listen.rb', line 24

def initialize(paths, excludes, settings, callback)
  @paths = paths
  @settings = settings
  @callback = Proc.new do |mod, add, rem|
    callback.call(mod + add + rem)
  end

  if excludes.any?
    @settings[:ignore!] = []
    excludes.each do |pattern|
      @settings[:ignore!] << self.class.excludes_to_listen(pattern.to_s)
    end
  end
end

Class Method Details

.excludes_to_listen(exclude) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/syncer/listeners/listen.rb', line 8

def self.excludes_to_listen(exclude)
  exclude = exclude.gsub('**', '"GLOBAL"')
  exclude = exclude.gsub('*', '"PATH"')

  if exclude.start_with?('/')
    pattern = "^#{Regexp.escape(exclude[1..-1])}"
  else
    pattern = Regexp.escape(exclude)
  end

  pattern = pattern.gsub('"PATH"', "[^/]*")
  pattern = pattern.gsub('"GLOBAL"', ".*")

  Regexp.new(pattern)
end

Instance Method Details

#runObject



39
40
41
42
# File 'lib/syncer/listeners/listen.rb', line 39

def run
  ::Listen.to(*@paths, @settings, &@callback).start
  sleep
end