Class: FChange::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rb-fchange/watcher.rb

Overview

Watchers monitor a single path for changes, specified by event flags. A watcher is usually created via Notifier#watch.

One Notifier may have many Watchers. The Notifier actually takes care of the checking for events, via #run or #process. The main purpose of having Watcher objects is to be able to disable them using #close.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notifier, path, recursive, *flags, &callback) ⇒ Watcher

Creates a new FChange::Watcher.

Raises:

  • (SystemCallError)

See Also:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rb-fchange/watcher.rb', line 79

def initialize(notifier, path, recursive, *flags, &callback)
  @notifier = notifier
  @callback = callback || proc {}
  @path = path
  @flags = flags
  @recursive = recursive ? 1 : 0

  @id = Native.FindFirstChangeNotificationA(path, @recursive,
    Native::Flags.to_mask(flags));
#      @id = Native.FindFirstChangeNotificationW(normalize_path(path), @recursive,
#        Native::Flags.to_mask(flags));
 
  unless @id < 0
    @notifier.add_watcher(self)
    return
  end

  raise SystemCallError.new("Failed to watch #{path.inspect}", @id)
end

Instance Attribute Details

#flagsArray<Symbol> (readonly)

The flags specifying the events that this Watcher is watching for, and potentially some options as well.

Returns:

  • (Array<Symbol>)


29
30
31
# File 'lib/rb-fchange/watcher.rb', line 29

def flags
  @flags
end

#idFixnum (readonly)

The id for this Watcher. Used to retrieve this Watcher from Notifier#watchers.

Returns:

  • (Fixnum)


36
37
38
# File 'lib/rb-fchange/watcher.rb', line 36

def id
  @id
end

#notifierNotifier (readonly)

The Notifier that this Watcher belongs to.

Returns:



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

def notifier
  @notifier
end

#pathString (readonly)

The path that this Watcher is watching.

Returns:

  • (String)


22
23
24
# File 'lib/rb-fchange/watcher.rb', line 22

def path
  @path
end

#recursiveBoolean (readonly)

Returns:

  • (Boolean)


41
42
43
# File 'lib/rb-fchange/watcher.rb', line 41

def recursive
  @recursive
end

Instance Method Details

#callback!(event) ⇒ Object

Calls this Watcher’s callback with the given Event.

Parameters:



47
48
49
# File 'lib/rb-fchange/watcher.rb', line 47

def callback!(event)
  @callback[event]
end

#closeObject

Disables this Watcher, so that it doesn’t fire any more events.

Raises:

  • (SystemCallError)

    if the watch fails to be disabled for some reason



54
55
56
57
58
59
# File 'lib/rb-fchange/watcher.rb', line 54

def close
  r = Native.FindCloseChangeNotification(@id)
  #@notifier.remove_watcher(self)
  return if r == 0
  raise SystemCallError.new("Failed to stop watching #{@path.inspect}", r)
end

#normalize_path(path) ⇒ Object



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

def normalize_path(path)
  if(path.size > 256)
    path = "\\\\?\\" + Pathname.new(path).realpath.to_s
  end
#      require 'rchardet'
#      require 'iconv'
#      cd = CharDet.detect(path)
#      encoding = cd['encoding']
#      converter = Iconv.new("UTF-16LE", encoding)
#      converter.iconv(path)
  # path.encode!("UTF-16LE")
end