Class: Middleman::CoreExtensions::FileWatcher::API
- Inherits:
-
Object
- Object
- Middleman::CoreExtensions::FileWatcher::API
- Defined in:
- middleman-core/lib/middleman-core/core_extensions/file_watcher.rb
Overview
Core File Change API class
Instance Attribute Summary (collapse)
-
- (Object) instance
Returns the value of attribute instance.
-
- (Object) known_paths
Returns the value of attribute known_paths.
Instance Method Summary (collapse)
-
- (Array<Proc>) changed(matcher = nil, &block)
Add callback to be run on file change.
-
- (Array<Proc>) deleted(matcher = nil, &block)
Add callback to be run on file deletion.
-
- (void) did_change(path)
Notify callbacks that a file changed.
-
- (void) did_delete(path)
Notify callbacks that a file was deleted.
-
- (void) find_new_files(path)
Like reload_path, but only triggers events on new files.
-
- (API) initialize
constructor
Initialize api and internal path cache.
-
- (void) reload_path(path)
Manually trigger update events.
Constructor Details
- (API) initialize
Initialize api and internal path cache
59 60 61 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 59 def initialize self.known_paths = Set.new end |
Instance Attribute Details
- (Object) instance
Returns the value of attribute instance
56 57 58 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 56 def instance @instance end |
- (Object) known_paths
Returns the value of attribute known_paths
56 57 58 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 56 def known_paths @known_paths end |
Instance Method Details
- (Array<Proc>) changed(matcher = nil, &block)
Add callback to be run on file change
67 68 69 70 71 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 67 def changed(matcher=nil, &block) @_changed ||= [] @_changed << [block, matcher] if block_given? @_changed end |
- (Array<Proc>) deleted(matcher = nil, &block)
Add callback to be run on file deletion
77 78 79 80 81 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 77 def deleted(matcher=nil, &block) @_deleted ||= [] @_deleted << [block, matcher] if block_given? @_deleted end |
- (void) did_change(path)
This method returns an undefined value.
Notify callbacks that a file changed
87 88 89 90 91 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 87 def did_change(path) puts "== File Change: #{path}" if instance.logging? && !::Middleman::Watcher.ignore_list.any? { |r| path.match(r) } self.known_paths << path self.run_callbacks(path, :changed) end |
- (void) did_delete(path)
This method returns an undefined value.
Notify callbacks that a file was deleted
97 98 99 100 101 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 97 def did_delete(path) puts "== File Deletion: #{path}" if instance.logging? && !::Middleman::Watcher.ignore_list.any? { |r| path.match(r) } self.known_paths.delete(path) self.run_callbacks(path, :deleted) end |
- (void) find_new_files(path)
This method returns an undefined value.
Like reload_path, but only triggers events on new files
128 129 130 131 132 133 134 135 136 137 138 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 128 def find_new_files(path) relative_path = path.sub("#{self.instance.root}/", "") subset = self.known_paths.select { |p| p.match(%r{^#{relative_path}}) } Find.find(path) do |file| next if File.directory?(file) next if Middleman::Watcher.ignore_list.any? { |r| path.match(r) } relative_path = file.sub("#{self.instance.root}/", "") self.did_change(relative_path) unless subset.include?(relative_path) end if File.exists?(path) end |
- (void) reload_path(path)
This method returns an undefined value.
Manually trigger update events
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'middleman-core/lib/middleman-core/core_extensions/file_watcher.rb', line 107 def reload_path(path) relative_path = path.sub("#{self.instance.root}/", "") subset = self.known_paths.select { |p| p.match(%r{^#{relative_path}}) } Find.find(path) do |path| next if File.directory?(path) next if Middleman::Watcher.ignore_list.any? { |r| path.match(r) } relative_path = path.sub("#{self.instance.root}/", "") subset.delete(relative_path) self.did_change(relative_path) end if File.exists?(path) subset.each do |removed_path| self.did_delete(removed_path) end end |