Class: IfChanged::Observer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ifchanged/observer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObserver

Returns a new instance of Observer.



19
20
21
22
23
24
25
# File 'lib/ifchanged/observer.rb', line 19

def initialize
  @interval = 1
  @work = true
  @file_infos = {}
  @hooks = []
  @prompt = '> '
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



17
18
19
# File 'lib/ifchanged/observer.rb', line 17

def debug
  @debug
end

#filesObject

Returns the value of attribute files.



17
18
19
# File 'lib/ifchanged/observer.rb', line 17

def files
  @files
end

#intervalObject

Returns the value of attribute interval.



17
18
19
# File 'lib/ifchanged/observer.rb', line 17

def interval
  @interval
end

#promptObject

Returns the value of attribute prompt.



17
18
19
# File 'lib/ifchanged/observer.rb', line 17

def prompt
  @prompt
end

Class Method Details

.add_hook(&block) ⇒ Object



9
10
11
# File 'lib/ifchanged/observer.rb', line 9

def self.add_hook(&block)
  instance.add_hook(&block)
end

.run(options = {}) ⇒ Object



13
14
15
# File 'lib/ifchanged/observer.rb', line 13

def self.run(options = {})
  instance.run(options)
end

Instance Method Details

#add_hook(&block) ⇒ Object



67
68
69
# File 'lib/ifchanged/observer.rb', line 67

def add_hook(&block)
  @hooks << block
end

#exitObject



61
62
63
64
65
# File 'lib/ifchanged/observer.rb', line 61

def exit
  puts
  puts 'exiting...'
  @work = false
end

#run(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ifchanged/observer.rb', line 27

def run(options = {})
  options.each do |k, v|
    self.__send__("#{k.to_s}=".to_sym, v) if self.respond_to?(k)
  end

  puts "# initializing..."

  trap(:INT) do
    self.exit
  end

  files.each do |file|
    pathname = Pathname.new(file)
    @file_infos[pathname] = FileInfo.new(pathname)
  end

  puts '# Press Ctrl-C to exit.'

  @observe_thread = Thread.new do
    while @work
      begin
        puts 'checking files...' if debug
        check_modifies
      rescue => e
        handle_error(e)
      ensure
        sleep @interval
      end
    end
  end

  @observe_thread.join
end