Class: FChange::Watcher
- Inherits:
-
Object
- Object
- FChange::Watcher
- 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
-
#flags ⇒ Array<Symbol>
readonly
The flags specifying the events that this Watcher is watching for, and potentially some options as well.
-
#id ⇒ Fixnum
readonly
The id for this Watcher.
-
#notifier ⇒ Notifier
readonly
The Notifier that this Watcher belongs to.
-
#path ⇒ String
readonly
The path that this Watcher is watching.
- #recursive ⇒ Boolean readonly
Instance Method Summary collapse
-
#callback!(event) ⇒ Object
Calls this Watcher’s callback with the given Event.
-
#close ⇒ Object
Disables this Watcher, so that it doesn’t fire any more events.
-
#initialize(notifier, path, recursive, *flags, &callback) ⇒ Watcher
constructor
Creates a new Watcher.
- #normalize_path(path) ⇒ Object
Constructor Details
#initialize(notifier, path, recursive, *flags, &callback) ⇒ Watcher
Creates a new FChange::Watcher.
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
#flags ⇒ Array<Symbol> (readonly)
The flags specifying the events that this Watcher is watching for, and potentially some options as well.
29 30 31 |
# File 'lib/rb-fchange/watcher.rb', line 29 def flags @flags end |
#id ⇒ Fixnum (readonly)
The id for this Watcher. Used to retrieve this Watcher from Notifier#watchers.
36 37 38 |
# File 'lib/rb-fchange/watcher.rb', line 36 def id @id end |
#notifier ⇒ Notifier (readonly)
The Notifier that this Watcher belongs to.
17 18 19 |
# File 'lib/rb-fchange/watcher.rb', line 17 def notifier @notifier end |
#path ⇒ String (readonly)
The path that this Watcher is watching.
22 23 24 |
# File 'lib/rb-fchange/watcher.rb', line 22 def path @path end |
#recursive ⇒ Boolean (readonly)
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.
47 48 49 |
# File 'lib/rb-fchange/watcher.rb', line 47 def callback!(event) @callback[event] end |
#close ⇒ Object
Disables this Watcher, so that it doesn’t fire any more events.
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 |