Class: Aniview::Util::FolderListen::Listener
- Inherits:
-
Object
- Object
- Aniview::Util::FolderListen::Listener
- Defined in:
- lib/aniview/util/folder_listen.rb
Instance Method Summary collapse
- #changed? ⇒ Boolean
- #dir_hash ⇒ Object
-
#initialize(dirs, &block) ⇒ Listener
constructor
A new instance of Listener.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(dirs, &block) ⇒ Listener
Returns a new instance of Listener.
18 19 20 21 22 23 |
# File 'lib/aniview/util/folder_listen.rb', line 18 def initialize(dirs, &block) @dirs = dirs @block = block @sleep_duration = 1 @dir_hash = dir_hash end |
Instance Method Details
#changed? ⇒ Boolean
29 30 31 |
# File 'lib/aniview/util/folder_listen.rb', line 29 def changed? @dir_hash != dir_hash end |
#dir_hash ⇒ Object
25 26 27 |
# File 'lib/aniview/util/folder_listen.rb', line 25 def dir_hash @dirs.map { |dir| [dir, File.exist?(dir)]}.to_h end |
#start ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/aniview/util/folder_listen.rb', line 37 def start Thread.abort_on_exception = true @listen_thread = Thread.new do while true if changed? @block.call @dir_hash = dir_hash end sleep 1 end end end |
#stop ⇒ Object
33 34 35 |
# File 'lib/aniview/util/folder_listen.rb', line 33 def stop @listen_thread.exit if @listen_thread end |