Class: Gotta::Mod::Listener

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(working_directory:) ⇒ Listener

Returns a new instance of Listener.



7
8
9
10
11
# File 'lib/gotta/mod/listener.rb', line 7

def initialize(working_directory:)
  @working_directory = working_directory
  @queue = Queue.new
  @listener = ::Listen.to(working_directory, &callback)
end

Instance Attribute Details

#listenerObject (readonly)

Returns the value of attribute listener.



6
7
8
# File 'lib/gotta/mod/listener.rb', line 6

def listener
  @listener
end

#queueObject (readonly)

Returns the value of attribute queue.



6
7
8
# File 'lib/gotta/mod/listener.rb', line 6

def queue
  @queue
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



6
7
8
# File 'lib/gotta/mod/listener.rb', line 6

def working_directory
  @working_directory
end

Instance Method Details

#callbackObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gotta/mod/listener.rb', line 21

def callback
  Proc.new do |modified, added, removed|
    begin
      modified.each do |file|
        queue.push Event.new(type: :modified,
                             absolute_path: file,
                             working_directory: working_directory)
      end
      added.each do |file|
        queue.push Event.new(type: :added,
                             absolute_path: file,
                             working_directory: working_directory)
      end
      removed.each do |file|
        queue.push Event.new(type: :removed,
                             absolute_path: file,
                             working_directory: working_directory)
      end
    rescue StandardError => e
      puts e.full_message
      next
    end
  end
end

#startObject



13
14
15
# File 'lib/gotta/mod/listener.rb', line 13

def start
  listener.start
end

#stopObject



17
18
19
# File 'lib/gotta/mod/listener.rb', line 17

def stop
  listener.stop
end