Class: FSEvent

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

Constant Summary collapse

VERSION =
'0.11.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of FSEvent.



21
22
23
# File 'lib/rb-fsevent/fsevent.rb', line 21

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

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



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

def callback
  @callback
end

#pathsObject (readonly)

Returns the value of attribute paths.



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

def paths
  @paths
end

Instance Method Details

#open_pipeObject



107
108
109
# File 'lib/rb-fsevent/fsevent.rb', line 107

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

#process_running?(pid) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
# File 'lib/rb-fsevent/fsevent.rb', line 97

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

#runObject



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rb-fsevent/fsevent.rb', line 38

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)
    # managing the IO ourselves allows us to be careful and never pass an
    # incomplete message to OTNetstring.parse()
    message = String.new
    length = String.new
    byte = nil

    reading_length = true
    found_length = false

    while reading_length
      byte = @pipe.read_nonblock(1)
      if "#{byte}" =~ /\d/
        length << byte
        found_length = true
      elsif found_length == false
        next
      else
        reading_length = false
      end
    end
    length = Integer(length, 10)
    type = byte

    message << "#{length}#{type}"
    message << @pipe.read(length)

    decoded = OTNetstring.parse(message)
    modified_paths = decoded["events"].map {|event| event["path"]}
    # passing the full info as a second block param feels icky, but such is
    # the trap of backward compatibility.
    case callback.arity
      when 1
        callback.call(modified_paths)
      when 2
        callback.call(modified_paths, decoded)
    end
  end
rescue Interrupt, IOError, Errno::EBADF
ensure
  stop
end

#stopObject



87
88
89
90
91
92
93
94
95
# File 'lib/rb-fsevent/fsevent.rb', line 87

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

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



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

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