Class: Filewatcher

Inherits:
Object
  • Object
show all
Includes:
Cycles, Snapshots
Defined in:
lib/filewatcher.rb,
lib/filewatcher/cycles.rb,
lib/filewatcher/version.rb,
lib/filewatcher/snapshot.rb,
lib/filewatcher/snapshots.rb,
lib/filewatcher/spec_helper.rb,
lib/filewatcher/spec_helper/watch_run.rb,
lib/filewatcher/spec_helper/ruby_watch_run.rb

Overview

Helpers in Filewatcher class itself

Defined Under Namespace

Modules: Cycles, Snapshots, SpecHelper Classes: Snapshot

Constant Summary collapse

VERSION =
'2.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Snapshots

#found_filenames

Constructor Details

#initialize(unexpanded_filenames, options = {}) ⇒ Filewatcher

Returns a new instance of Filewatcher.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/filewatcher.rb', line 26

def initialize(unexpanded_filenames, options = {})
  @unexpanded_filenames = unexpanded_filenames
  @unexpanded_excluded_filenames = options[:exclude]
  @keep_watching = false
  @pausing = false
  @immediate = options[:immediate]
  @interval = options.fetch(:interval, 0.5)
  @logger = options.fetch(:logger, Logger.new($stdout, level: :info))

  after_initialize unexpanded_filenames, options
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



14
15
16
# File 'lib/filewatcher.rb', line 14

def interval
  @interval
end

#keep_watchingObject (readonly)

Returns the value of attribute keep_watching.



15
16
17
# File 'lib/filewatcher.rb', line 15

def keep_watching
  @keep_watching
end

Class Method Details



18
19
20
21
22
23
# File 'lib/filewatcher.rb', line 18

def print_version
  system 'ruby -v'
  puts "Filewatcher #{self::VERSION}"

  super if defined? super
end

Instance Method Details

#finalize(&on_update) ⇒ Object

Calls the update block repeatedly until all changes in the current snapshot are dealt with



87
88
89
90
91
92
93
94
95
96
# File 'lib/filewatcher.rb', line 87

def finalize(&on_update)
  on_update = @on_update unless block_given?

  while file_system_updated?(@end_snapshot || current_snapshot)
    finalizing
    trigger_changes(on_update)
  end

  @end_snapshot = nil
end

#pauseObject



55
56
57
58
59
60
61
62
# File 'lib/filewatcher.rb', line 55

def pause
  @pausing = true

  before_pause_sleep

  # Ensure we wait long enough to enter pause loop in #watch
  sleep @interval
end

#resumeObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/filewatcher.rb', line 64

def resume
  raise "Can't resume unless #watch and #pause were first called" if !@keep_watching || !@pausing

  @last_snapshot = current_snapshot # resume with fresh snapshot
  @pausing = false

  before_resume_sleep

  sleep @interval # Wait long enough to exit pause loop in #watch
end

#stopObject

Ends the watch, allowing any remaining changes to be finalized. Used mainly in multi-threaded situations.



77
78
79
80
81
82
83
# File 'lib/filewatcher.rb', line 77

def stop
  @keep_watching = false

  after_stop

  nil
end

#watch {|{ '' => '' }| ... } ⇒ Object

Yields:

  • ({ '' => '' })


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/filewatcher.rb', line 38

def watch(&on_update)
  ## The set of available signals depends on the OS
  ## Windows doesn't support `HUP` signal, for example
  (%w[HUP INT TERM] & Signal.list.keys).each do |signal|
    trap(signal) { exit }
  end

  @on_update = on_update
  @keep_watching = true
  yield({ '' => '' }) if @immediate

  main_cycle

  @end_snapshot = current_snapshot
  finalize(&on_update)
end