Class: Filetter::Observer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObserver

Returns a new instance of Observer.



22
23
24
25
26
27
28
29
# File 'lib/filetter/observer.rb', line 22

def initialize
  @patterns = './**/*'
  @interval = 1
  @work = true
  @file_infos = {}
  @hooks = {}
  @prompt = '> '
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



20
21
22
# File 'lib/filetter/observer.rb', line 20

def debug
  @debug
end

#intervalObject

Returns the value of attribute interval.



20
21
22
# File 'lib/filetter/observer.rb', line 20

def interval
  @interval
end

#patternsObject

Returns the value of attribute patterns.



20
21
22
# File 'lib/filetter/observer.rb', line 20

def patterns
  @patterns
end

#promptObject

Returns the value of attribute prompt.



20
21
22
# File 'lib/filetter/observer.rb', line 20

def prompt
  @prompt
end

Class Method Details

.add_hook(*args, &block) ⇒ Object

deplecated



12
13
14
# File 'lib/filetter/observer.rb', line 12

def self.add_hook(*args, &block)
  instance.add_hook(*args, &block)
end

.run(options = {}) ⇒ Object



16
17
18
# File 'lib/filetter/observer.rb', line 16

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

Instance Method Details

#add_hook(*args, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/filetter/observer.rb', line 77

def add_hook(*args, &block)
  unless args.empty?
    args.each do |arg|
      hook_name = normalize_as_hook_name(arg)
      @hooks[hook_name] ||= []
      @hooks[hook_name] << block
    end
  else
    @hooks[:any] ||= []
    @hooks[:any] << block
  end
end

#exitObject



72
73
74
75
# File 'lib/filetter/observer.rb', line 72

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

#run(options = {}) ⇒ Object



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
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/filetter/observer.rb', line 31

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

  puts "=> initializing..."
  collect_files

  @input_thread = Thread.new do
    Readline.completion_proc = lambda {|input|
      self.methods.map{|i|i.to_s}.grep(/^#{Regexp.quote(input)}/)
    }
    puts '=> Enter "exit" to exit.'
    while @work && line = Readline.readline(@prompt, true)
      begin
        eval(line) unless line.empty?
      rescue => e
        handle_error(e)
      end
    end
  end

  sleep @interval

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

  @observe_thread.join
  @input_thread.join
end