Class: Rascut::FileObserver

Inherits:
Object
  • Object
show all
Defined in:
lib/rascut/file_observer.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :interval => 1,
  :ignore_files => [],
  :ignore_dirs => [/\/.svn/],
  :logger => Logger.new(STDOUT),
  :dir_counter => 5,
  :ext => nil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, options) ⇒ FileObserver

Returns a new instance of FileObserver.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rascut/file_observer.rb', line 16

def initialize(files, options)
  @files = {}
  @dirs = {}
  @options = DEFAULT_OPTIONS.merge options
  @update_handlers = []
  @th = nil

  if options[:update_handler]
    add_update_handler options.delete(:update_handler)
  end

  observe files
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



29
30
31
# File 'lib/rascut/file_observer.rb', line 29

def options
  @options
end

Instance Method Details

#add_update_handler(handler) ⇒ Object



48
49
50
51
52
# File 'lib/rascut/file_observer.rb', line 48

def add_update_handler(handler)
  unless @update_handlers.include? handler
    @update_handlers << handler
  end
end

#loggerObject



31
32
33
# File 'lib/rascut/file_observer.rb', line 31

def logger
  options[:logger]
end

#observe(files) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rascut/file_observer.rb', line 62

def observe(files)
  Array(files).each do |file|
    file = Pathname.new(file)
    if file.directory?
      dir_observe file
    else
      next if @options[:ignore_files].include?(file.realpath)

      file_observe file
    end
  end
end

#remove_update_handler(handler) ⇒ Object



54
55
56
# File 'lib/rascut/file_observer.rb', line 54

def remove_update_handler(handler)
  @update_handlers.delete_if {|h| h == handler}
end

#runObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rascut/file_observer.rb', line 35

def run
  if @th
    @th.run
  else
    @th = Thread.start do
      loop do
        update_check
        sleep @options[:interval]
      end
    end
  end
end

#stopObject



58
59
60
# File 'lib/rascut/file_observer.rb', line 58

def stop
  @th.kill
end