Class: Middleman::CoreExtensions::FileWatcher::API
- Inherits:
-
Object
- Object
- Middleman::CoreExtensions::FileWatcher::API
- Defined in:
- lib/middleman-core/core_extensions/file_watcher.rb
Overview
Core File Change API class
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#known_paths ⇒ Object
readonly
Returns the value of attribute known_paths.
Instance Method Summary collapse
-
#changed(matcher = nil, &block) ⇒ Array<Proc>
Add callback to be run on file change.
-
#deleted(matcher = nil, &block) ⇒ Array<Proc>
Add callback to be run on file deletion.
-
#did_change(path)
Notify callbacks that a file changed.
-
#did_delete(path)
Notify callbacks that a file was deleted.
- #exists?(path) ⇒ Boolean
-
#find_new_files(path)
Like reload_path, but only triggers events on new files.
-
#ignored?(path) ⇒ Boolean
Whether this path is ignored.
-
#initialize(app) ⇒ API
constructor
Initialize api and internal path cache.
-
#reload_path(path, only_new = false)
Manually trigger update events.
Constructor Details
#initialize(app) ⇒ API
Initialize api and internal path cache
67 68 69 70 71 72 73 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 67 def initialize(app) @app = app @known_paths = Set.new @_changed = [] @_deleted = [] end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
62 63 64 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 62 def app @app end |
#known_paths ⇒ Object (readonly)
Returns the value of attribute known_paths.
63 64 65 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 63 def known_paths @known_paths end |
Instance Method Details
#changed(matcher = nil, &block) ⇒ Array<Proc>
Add callback to be run on file change
79 80 81 82 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 79 def changed(matcher=nil, &block) @_changed << [block, matcher] if block_given? @_changed end |
#deleted(matcher = nil, &block) ⇒ Array<Proc>
Add callback to be run on file deletion
88 89 90 91 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 88 def deleted(matcher=nil, &block) @_deleted << [block, matcher] if block_given? @_deleted end |
#did_change(path)
This method returns an undefined value.
Notify callbacks that a file changed
97 98 99 100 101 102 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 97 def did_change(path) path = Pathname(path) logger.debug "== File Change: #{path}" @known_paths << path run_callbacks(path, :changed) end |
#did_delete(path)
This method returns an undefined value.
Notify callbacks that a file was deleted
108 109 110 111 112 113 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 108 def did_delete(path) path = Pathname(path) logger.debug "== File Deletion: #{path}" @known_paths.delete(path) run_callbacks(path, :deleted) end |
#exists?(path) ⇒ Boolean
148 149 150 151 152 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 148 def exists?(path) p = Pathname(path) p = p.relative_path_from(Pathname(@app.root)) unless p.relative? @known_paths.include?(p) end |
#find_new_files(path)
This method returns an undefined value.
Like reload_path, but only triggers events on new files
144 145 146 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 144 def find_new_files(path) reload_path(path, true) end |
#ignored?(path) ⇒ Boolean
Whether this path is ignored
157 158 159 160 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 157 def ignored?(path) path = path.to_s app.config[:file_watcher_ignore].any? { |r| path =~ r } end |
#reload_path(path, only_new = false)
This method returns an undefined value.
Manually trigger update events
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/middleman-core/core_extensions/file_watcher.rb', line 120 def reload_path(path, only_new=false) # chdir into the root directory so Pathname can work with relative paths Dir.chdir @app.root_path do path = Pathname(path) return unless path.exist? glob = (path + '**').to_s subset = @known_paths.select { |p| p.fnmatch(glob) } ::Middleman::Util.all_files_under(path, &method(:ignored?)).each do |filepath| next if only_new && subset.include?(filepath) subset.delete(filepath) did_change(filepath) end subset.each(&method(:did_delete)) unless only_new end end |