Class: Kaisoku::ListenWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/kaisoku/listen_watcher.rb

Constant Summary collapse

DEFAULT_PATHS =
Watcher::DEFAULT_PATHS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: Configuration.new, paths: DEFAULT_PATHS, interval: 0.2) ⇒ ListenWatcher

Returns a new instance of ListenWatcher.



10
11
12
13
14
# File 'lib/kaisoku/listen_watcher.rb', line 10

def initialize(configuration: Configuration.new, paths: DEFAULT_PATHS, interval: 0.2)
  @configuration = configuration
  @paths = Array(paths).empty? ? DEFAULT_PATHS : Array(paths)
  @interval = interval
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/kaisoku/listen_watcher.rb', line 8

def configuration
  @configuration
end

#pathsObject (readonly)

Returns the value of attribute paths.



8
9
10
# File 'lib/kaisoku/listen_watcher.rb', line 8

def paths
  @paths
end

Instance Method Details

#each_changeObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/kaisoku/listen_watcher.rb', line 16

def each_change
  listener = Listen.to(*existing_paths, latency: @interval) do |modified, added, removed|
    changed = (modified + added + removed).map { |path| configuration.relative_path(path) }.sort
    yield changed unless changed.empty?
  end
  listener.start
  sleep
ensure
  listener&.stop
end