Class: FSEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/rb-fsevent/fsevent.rb,
lib/rb-fsevent/version.rb

Constant Summary collapse

VERSION =
'0.9.8'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil, &block) ⇒ FSEvent

Returns a new instance of FSEvent.



19
20
21
# File 'lib/rb-fsevent/fsevent.rb', line 19

def initialize args = nil, &block
  watch(args, &block) unless args.nil?
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



17
18
19
# File 'lib/rb-fsevent/fsevent.rb', line 17

def callback
  @callback
end

#pathsObject (readonly)

Returns the value of attribute paths.



17
18
19
# File 'lib/rb-fsevent/fsevent.rb', line 17

def paths
  @paths
end

Instance Method Details

#open_pipeObject



73
74
75
# File 'lib/rb-fsevent/fsevent.rb', line 73

def open_pipe
  IO.popen("'#{self.class.watcher_path}' #{options_string} #{shellescaped_paths}")
end

#process_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/rb-fsevent/fsevent.rb', line 63

def process_running?(pid)
  begin
    Process.kill(0, pid)
    true
  rescue Errno::ESRCH
    false
  end
end

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rb-fsevent/fsevent.rb', line 36

def run
  @pipe    = open_pipe
  @running = true

  # please note the use of IO::select() here, as it is used specifically to
  # preserve correct signal handling behavior in ruby 1.8.
  while @running && IO::select([@pipe], nil, nil, nil)
    if line = @pipe.readline
      modified_dir_paths = line.split(':').select { |dir| dir != "\n" }
      callback.call(modified_dir_paths)
    end
  end
rescue Interrupt, IOError, Errno::EBADF
ensure
  stop
end

#stopObject



53
54
55
56
57
58
59
60
61
# File 'lib/rb-fsevent/fsevent.rb', line 53

def stop
  unless @pipe.nil?
    Process.kill('KILL', @pipe.pid) if process_running?(@pipe.pid)
    @pipe.close
  end
rescue IOError
ensure
  @running = false
end

#watch(watch_paths, options = nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rb-fsevent/fsevent.rb', line 23

def watch(watch_paths, options=nil, &block)
  @paths      = watch_paths.kind_of?(Array) ? watch_paths : [watch_paths]
  @callback   = block

  if options.kind_of?(Hash)
    @options  = parse_options(options)
  elsif options.kind_of?(Array)
    @options  = options
  else
    @options  = []
  end
end