Class: TypedEnums::Watcher
- Inherits:
-
Object
- Object
- TypedEnums::Watcher
- Defined in:
- lib/typed_enums/watcher.rb
Constant Summary collapse
- MODEL_FILE_PATTERN =
/\.rb\z/- DEFAULT_LATENCY =
0.25
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(root: Rails.root, logger: Rails.logger, listener_factory: nil, generator: TypedEnums) ⇒ Watcher
constructor
A new instance of Watcher.
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
Constructor Details
#initialize(root: Rails.root, logger: Rails.logger, listener_factory: nil, generator: TypedEnums) ⇒ Watcher
Returns a new instance of Watcher.
49 50 51 52 53 54 55 |
# File 'lib/typed_enums/watcher.rb', line 49 def initialize(root: Rails.root, logger: Rails.logger, listener_factory: nil, generator: TypedEnums) @root = Pathname(root) @logger = logger @listener_factory = listener_factory @generator = generator @started = false end |
Class Method Details
.run_foreground ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/typed_enums/watcher.rb', line 31 def run_foreground(**) watcher = start(**) sleep rescue Interrupt nil ensure watcher&.stop end |
.start(**options) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/typed_enums/watcher.rb', line 11 def start(**) mutex.synchronize do return instance if instance&.started? @instance = new(**) @instance.start end end |
.started? ⇒ Boolean
27 28 29 |
# File 'lib/typed_enums/watcher.rb', line 27 def started? instance&.started? || false end |
.stop ⇒ Object
20 21 22 23 24 25 |
# File 'lib/typed_enums/watcher.rb', line 20 def stop mutex.synchronize do instance&.stop @instance = nil end end |
Instance Method Details
#start ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/typed_enums/watcher.rb', line 57 def start return self if started? unless watch_path.directory? logger&.warn("typed_enums: model watch path does not exist: #{watch_path}") return self end @listener = build_listener listener.start @started = true logger&.info("typed_enums: watching #{watch_path}") self end |
#started? ⇒ Boolean
78 79 80 |
# File 'lib/typed_enums/watcher.rb', line 78 def started? @started end |
#stop ⇒ Object
72 73 74 75 76 |
# File 'lib/typed_enums/watcher.rb', line 72 def stop listener&.stop @started = false self end |