Class: ActiveSupport::EventedFileUpdateChecker
- Defined in:
- lib/active_support/evented_file_update_checker.rb
Overview
Allows you to “listen” to changes in a file system. The evented file updater does not hit disk when checking for updates. Instead, it uses platform-specific file system events to trigger a change in state.
The file checker takes an array of files to watch or a hash specifying directories and file extensions to watch. It also takes a block that is called when EventedFileUpdateChecker#execute is run or when EventedFileUpdateChecker#execute_if_updated is run and there have been changes to the file system.
Example:
checker = ActiveSupport::EventedFileUpdateChecker.new(["/tmp/foo"]) { puts "changed" }
checker.updated?
# => false
checker.execute_if_updated
# => nil
FileUtils.touch("/tmp/foo")
checker.updated?
# => true
checker.execute_if_updated
# => "changed"
Defined Under Namespace
Classes: Core
Instance Method Summary collapse
- #execute ⇒ Object
- #execute_if_updated ⇒ Object
-
#initialize(files, dirs = {}, &block) ⇒ EventedFileUpdateChecker
constructor
:nodoc: all.
- #inspect ⇒ Object
- #updated? ⇒ Boolean
Constructor Details
#initialize(files, dirs = {}, &block) ⇒ EventedFileUpdateChecker
:nodoc: all
36 37 38 39 40 41 42 43 44 |
# File 'lib/active_support/evented_file_update_checker.rb', line 36 def initialize(files, dirs = {}, &block) unless block raise ArgumentError, "A block is required to initialize an EventedFileUpdateChecker" end @block = block @core = Core.new(files, dirs) ObjectSpace.define_finalizer(self, @core.finalizer) end |
Instance Method Details
#execute ⇒ Object
59 60 61 62 |
# File 'lib/active_support/evented_file_update_checker.rb', line 59 def execute @core.updated.make_false @block.call end |
#execute_if_updated ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/active_support/evented_file_update_checker.rb', line 64 def execute_if_updated if updated? yield if block_given? execute true end end |
#inspect ⇒ Object
46 47 48 |
# File 'lib/active_support/evented_file_update_checker.rb', line 46 def inspect "#<ActiveSupport::EventedFileUpdateChecker:#{object_id} @files=#{@core.files.to_a.inspect}" end |
#updated? ⇒ Boolean
50 51 52 53 54 55 56 57 |
# File 'lib/active_support/evented_file_update_checker.rb', line 50 def updated? if @core.restart? @core.thread_safely(&:restart) @core.updated.make_true end @core.updated.true? end |