Class: TypedEnums::Watcher

Inherits:
Object
  • Object
show all
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

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_foregroundObject



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(**options)
  mutex.synchronize do
    return instance if instance&.started?

    @instance = new(**options)
    @instance.start
  end
end

.started?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/typed_enums/watcher.rb', line 27

def started?
  instance&.started? || false
end

.stopObject



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

#startObject



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

Returns:

  • (Boolean)


78
79
80
# File 'lib/typed_enums/watcher.rb', line 78

def started?
  @started
end

#stopObject



72
73
74
75
76
# File 'lib/typed_enums/watcher.rb', line 72

def stop
  listener&.stop
  @started = false
  self
end