Class: Listen::Change

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/listen/change.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(listener) ⇒ Change

Returns a new instance of Change.



10
11
12
# File 'lib/listen/change.rb', line 10

def initialize(listener)
  @listener = listener
end

Instance Attribute Details

#listenerObject

Returns the value of attribute listener.



8
9
10
# File 'lib/listen/change.rb', line 8

def listener
  @listener
end

Instance Method Details

#_log(type, message) ⇒ Object (private)



54
55
56
# File 'lib/listen/change.rb', line 54

def _log(type, message)
  Celluloid.logger.send(type, message)
end

#change(type, watched_dir, rel_path, options = {}) ⇒ Object



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
# File 'lib/listen/change.rb', line 14

def change(type, watched_dir, rel_path, options = {})
  change = options[:change]
  cookie = options[:cookie]

  if !cookie && listener.silencer.silenced?(Pathname(rel_path), type)
    _log :debug, "(silenced): #{rel_path.inspect}"
    return
  end

  path = watched_dir + rel_path

  log_details = options[:silence] && 'recording' || change || 'unknown'
  _log :debug, "#{log_details}: #{type}:#{path} (#{options.inspect})"

  if change
    # TODO: move this to Listener to avoid Celluloid overhead
    # from caller
    options = cookie ? { cookie: cookie } : {}
    listener.queue(type, change, watched_dir, rel_path, options)
  else
    return unless (record = listener.sync(:record))

    if type == :dir
      return unless (change_queue = listener.async(:change_pool))
      Directory.scan(change_queue, record, watched_dir, rel_path, options)
    else
      change = File.change(record, watched_dir, rel_path)
      return if !change || options[:silence]
      listener.queue(:file, change, watched_dir, rel_path)
    end
  end
rescue Celluloid::Task::TerminatedError
  _log :debug, "Change#change was terminated: #{$!.inspect}"
rescue RuntimeError
  _log :error, "Change#change crashed #{$!.inspect}:#{[email protected]("\n")}"
  raise
end