Class: FaaStRuby::Local::Listener

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/faastruby/local/listeners/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#debug, #print, puts, #puts

Constructor Details

#initialize(directory:, queue:) ⇒ Listener

Returns a new instance of Listener.



6
7
8
9
10
11
# File 'lib/faastruby/local/listeners/listener.rb', line 6

def initialize(directory:, queue:)
  debug "initialize(directory: #{directory.inspect}, queue: #{queue.inspect})"
  @directory = directory
  @queue = queue
  @listener = ::Listen.to(directory, &callback)
end

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



5
6
7
# File 'lib/faastruby/local/listeners/listener.rb', line 5

def directory
  @directory
end

#listenerObject

Returns the value of attribute listener.



5
6
7
# File 'lib/faastruby/local/listeners/listener.rb', line 5

def listener
  @listener
end

#queueObject

Returns the value of attribute queue.



5
6
7
# File 'lib/faastruby/local/listeners/listener.rb', line 5

def queue
  @queue
end

Instance Method Details

#callbackObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/faastruby/local/listeners/listener.rb', line 23

def callback
  debug "callback"
  Proc.new do |modified, added, removed|
    begin
      modified.each do |file|
        queue.push ListenerEvent.new(type: :modified, full_path: file, listened_directory: @directory)
      end
      added.each do |file|
        queue.push ListenerEvent.new(type: :added, full_path: file, listened_directory: @directory)
      end
      removed.each do |file|
        queue.push ListenerEvent.new(type: :removed, full_path: file, listened_directory: @directory)
      end
    rescue StandardError => e
      String.disable_colorization = true
      STDOUT.puts e.full_message
      String.disable_colorization = false
      next
    end
  end
end

#startObject



13
14
15
16
# File 'lib/faastruby/local/listeners/listener.rb', line 13

def start
  debug "start"
  listener.start
end

#stopObject



18
19
20
21
# File 'lib/faastruby/local/listeners/listener.rb', line 18

def stop
  debug "stop"
  listener.stop
end