Class: Kaisoku::Watcher

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#configurationObject (readonly)

Returns the value of attribute configuration.



8
9
10
# File 'lib/kaisoku/watcher.rb', line 8

def configuration
  @configuration
end

#intervalObject (readonly)

Returns the value of attribute interval.



8
9
10
# File 'lib/kaisoku/watcher.rb', line 8

def interval
  @interval
end

#pathsObject (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_changeObject



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

#snapshotObject



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