Class: Kaisoku::Watcher
- Inherits:
-
Object
- Object
- Kaisoku::Watcher
- Defined in:
- lib/kaisoku/watcher.rb
Constant Summary collapse
- DEFAULT_PATHS =
%w[app lib spec test config db].freeze
- IGNORED_PARTS =
%w[.git .kaisoku].freeze
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
- #changed_paths(before, after) ⇒ Object
- #each_change ⇒ Object
-
#initialize(configuration: Configuration.new, paths: DEFAULT_PATHS, interval: 0.2) ⇒ Watcher
constructor
A new instance of Watcher.
- #snapshot ⇒ Object
Constructor Details
#initialize(configuration: Configuration.new, paths: DEFAULT_PATHS, interval: 0.2) ⇒ Watcher
Returns a new instance of Watcher.
10 11 12 13 14 |
# File 'lib/kaisoku/watcher.rb', line 10 def initialize(configuration: Configuration.new, paths: DEFAULT_PATHS, interval: 0.2) @configuration = configuration @paths = Array(paths).empty? ? DEFAULT_PATHS : Array(paths) @interval = interval.to_f end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
8 9 10 |
# File 'lib/kaisoku/watcher.rb', line 8 def configuration @configuration end |
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
8 9 10 |
# File 'lib/kaisoku/watcher.rb', line 8 def interval @interval end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
8 9 10 |
# File 'lib/kaisoku/watcher.rb', line 8 def paths @paths end |
Instance Method Details
#changed_paths(before, after) ⇒ Object
22 23 24 |
# File 'lib/kaisoku/watcher.rb', line 22 def changed_paths(before, after) (before.keys | after.keys).reject { |path| before[path] == after[path] }.sort end |
#each_change ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/kaisoku/watcher.rb', line 26 def each_change previous = snapshot loop do sleep interval current = snapshot changed = changed_paths(previous, current) yield changed unless changed.empty? previous = current end end |
#snapshot ⇒ Object
16 17 18 19 20 |
# File 'lib/kaisoku/watcher.rb', line 16 def snapshot watched_files.to_h do |file| [configuration.relative_path(file), File.mtime(file).to_f] end end |