Class: Core::Watch::Snapshot
- Inherits:
-
Object
- Object
- Core::Watch::Snapshot
- Defined in:
- lib/core/watch/snapshot.rb
Overview
- public
-
Snapshot of a watched system that can generate diffs.
Class Method Summary collapse
Instance Method Summary collapse
-
#diff ⇒ Object
- public
-
Detect changes, update the snapshot, and return a diff.
-
#ignore?(path) ⇒ Boolean
- public
-
Return ‘true` if `path` is ignored.
-
#initialize(*paths, ignore: [], strategy: self.class.default_strategy) ⇒ Snapshot
constructor
A new instance of Snapshot.
Constructor Details
#initialize(*paths, ignore: [], strategy: self.class.default_strategy) ⇒ Snapshot
Returns a new instance of Snapshot.
18 19 20 21 22 23 24 |
# File 'lib/core/watch/snapshot.rb', line 18 def initialize(*paths, ignore: [], strategy: self.class.default_strategy) @ignore = ignore @watched = {} @strategy = strategy.new track(*paths) end |
Class Method Details
.default_strategy ⇒ Object
11 12 13 14 15 |
# File 'lib/core/watch/snapshot.rb', line 11 def default_strategy require_relative "strategies/digest" Strategies::Digest end |
Instance Method Details
#diff ⇒ Object
- public
-
Detect changes, update the snapshot, and return a diff.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/core/watch/snapshot.rb', line 36 def diff trackable = [] untrackable = [] diff = Diff.new @watched.each do |path, object| case object[:type] when :directory # The mtime of the parent directory changes when its contents have changed (e.g. a file is added or removed). # So, do the minimum amount of work necessary to detect added/removed files, then detect file changes below. # if @strategy.identify(path) != object[:identity] path.glob("*") do |each_path| unless @watched.include?(each_path) next if ignore?(each_path) diff.added(each_path) trackable << each_path end end trackable << path end when :file if @strategy.identify(path) != object[:identity] diff.changed(path) trackable << path end end rescue Errno::ENOENT diff.removed(path) untrackable << path end track(*trackable) untrack(*untrackable) diff end |
#ignore?(path) ⇒ Boolean
- public
-
Return ‘true` if `path` is ignored.
28 29 30 31 32 |
# File 'lib/core/watch/snapshot.rb', line 28 def ignore?(path) @ignore.any? do |ignore| path.fnmatch?(ignore) end end |