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
}
MSWIN32 =
!!RUBY_PLATFORM.include?('mswin32')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, options) ⇒ FileObserver

Returns a new instance of FileObserver.



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

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

#dirsObject (readonly)

Returns the value of attribute dirs.



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

def dirs
  @dirs
end

#filesObject (readonly)

Returns the value of attribute files.



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

def files
  @files
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#add_update_handler(handler) ⇒ Object



51
52
53
54
55
# File 'lib/rascut/file_observer.rb', line 51

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

#loggerObject



34
35
36
# File 'lib/rascut/file_observer.rb', line 34

def logger
  options[:logger]
end

#observe(files) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rascut/file_observer.rb', line 65

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



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

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

#runObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rascut/file_observer.rb', line 38

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

#stopObject



61
62
63
# File 'lib/rascut/file_observer.rb', line 61

def stop
  @th.kill
end