Class: TypeGuessr::MCP::FileWatcher
- Inherits:
-
Object
- Object
- TypeGuessr::MCP::FileWatcher
- 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
-
#initialize(project_path:, on_change:, interval: 2) ⇒ FileWatcher
constructor
A new instance of FileWatcher.
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(project_path:, on_change:, interval: 2) ⇒ FileWatcher
Returns a new instance of FileWatcher.
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
39 40 41 |
# File 'lib/type_guessr/mcp/file_watcher.rb', line 39 def running? @running && @thread&.alive? end |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |