Class: TypeGuessr::MCP::FileWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/type_guessr/mcp/file_watcher.rb

Overview

Watches a project directory for .rb file changes using mtime polling. Detects modified, added, and deleted files and invokes a callback.

Usage:

watcher = FileWatcher.new(project_path: "/path/to/project", interval: 2) do |modified, added, removed|
  modified.each { |f| reindex(f) }
  removed.each { |f| remove(f) }
end
watcher.start

Instance Method Summary collapse

Constructor Details

#initialize(project_path:, on_change:, interval: 2) ⇒ FileWatcher

Returns a new instance of FileWatcher.

Parameters:

  • project_path (String)

    Root directory to watch

  • interval (Numeric) (defaults to: 2)

    Polling interval in seconds (default: 2)

  • on_change (Proc)

    Callback receiving (modified, added, removed) arrays



18
19
20
21
22
23
24
# File 'lib/type_guessr/mcp/file_watcher.rb', line 18

def initialize(project_path:, on_change:, interval: 2)
  @project_path = project_path
  @interval = interval
  @on_change = on_change
  @thread = nil
  @running = false
end

Instance Method Details

#running?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/type_guessr/mcp/file_watcher.rb', line 39

def running?
  @running && @thread&.alive?
end

#startObject



26
27
28
29
30
31
# File 'lib/type_guessr/mcp/file_watcher.rb', line 26

def start
  @running = true
  @snapshot = take_snapshot
  @thread = Thread.new { poll_loop }
  @thread.abort_on_exception = true
end

#stopObject



33
34
35
36
37
# File 'lib/type_guessr/mcp/file_watcher.rb', line 33

def stop
  @running = false
  @thread&.join(5)
  @thread = nil
end