Class: Rucola::FSEvents
Defined Under Namespace
Classes: FSEvent, StreamError
Instance Attribute Summary collapse
-
#allocator ⇒ Object
Returns the value of attribute allocator.
-
#context ⇒ Object
Returns the value of attribute context.
-
#flags ⇒ Object
Returns the value of attribute flags.
-
#latency ⇒ Object
Returns the value of attribute latency.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
-
#since ⇒ Object
Returns the value of attribute since.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Class Method Summary collapse
-
.start_watching(*paths, &block) ⇒ Object
Initializes a new FSEvents ‘watchdog` object, creates, starts and then returns the stream.
Instance Method Summary collapse
-
#create_stream ⇒ Object
Create the stream.
-
#initialize(paths, options = {}, &block) ⇒ FSEvents
constructor
Creates a new FSEvents ‘watchdog` object.
-
#start ⇒ Object
Start the stream.
-
#stop ⇒ Object
Stop the stream.
Constructor Details
#initialize(paths, options = {}, &block) ⇒ FSEvents
Creates a new FSEvents ‘watchdog` object. Pass it an array of paths and a block with your callback. Options allow you to specify in more detail how the events watcher should behave.
*:since: The service will report events that have happened after the supplied event ID. Never use 0 because that
will cause every fsevent since the "beginning of time" to be reported. Use OSX::KFSEventStreamEventIdSinceNow
if you want to receive events that have happened after this call. (Default: OSX::KFSEventStreamEventIdSinceNow).
*:latency: Number of seconds to wait until an FSEvent is reported, this allows the service to bundle events. (Default: 0.0)
For the rest of the options read the Cocoa documentation.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rucola/fsevents.rb', line 67 def initialize(paths, ={}, &block) raise ArgumentError, 'No callback block was specified.' unless block_given? paths.each { |path| raise ArgumentError, "The specified path (#{path}) does not exist." unless File.exist?(path) } @allocator = [:allocator] || OSX::KCFAllocatorDefault @context = [:context] || nil @since = [:since] || OSX::KFSEventStreamEventIdSinceNow @latency = [:latency] || 0.0 @flags = [:flags] || 0 @stream = [:stream] || nil @paths = paths @user_callback = block @callback = Proc.new do |stream, client_callback_info, number_of_events, paths_pointer, event_flags, event_ids| paths_pointer.regard_as('*') events = [] number_of_events.times {|i| events << Rucola::FSEvents::FSEvent.new(self, event_ids[i], paths_pointer[i]) } @user_callback.call(events) end end |
Instance Attribute Details
#allocator ⇒ Object
Returns the value of attribute allocator.
30 31 32 |
# File 'lib/rucola/fsevents.rb', line 30 def allocator @allocator end |
#context ⇒ Object
Returns the value of attribute context.
31 32 33 |
# File 'lib/rucola/fsevents.rb', line 31 def context @context end |
#flags ⇒ Object
Returns the value of attribute flags.
34 35 36 |
# File 'lib/rucola/fsevents.rb', line 34 def flags @flags end |
#latency ⇒ Object
Returns the value of attribute latency.
33 34 35 |
# File 'lib/rucola/fsevents.rb', line 33 def latency @latency end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
27 28 29 |
# File 'lib/rucola/fsevents.rb', line 27 def paths @paths end |
#since ⇒ Object
Returns the value of attribute since.
32 33 34 |
# File 'lib/rucola/fsevents.rb', line 32 def since @since end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
28 29 30 |
# File 'lib/rucola/fsevents.rb', line 28 def stream @stream end |
Class Method Details
.start_watching(*paths, &block) ⇒ Object
Initializes a new FSEvents ‘watchdog` object, creates, starts and then returns the stream.
Pass it a block which will be used as a callback. The block will receive an array of FSEvent objects.
fsevents = Rucola::FSEvents.start_watching('/some/path') do |events|
events.each do |event|
p event
end
end
p fsevents
49 50 51 52 53 54 |
# File 'lib/rucola/fsevents.rb', line 49 def start_watching(*paths, &block) fsevents = new(paths.flatten, &block) fsevents.create_stream fsevents.start fsevents end |
Instance Method Details
#create_stream ⇒ Object
Create the stream. Raises a Rucola::FSEvents::StreamError if the stream could not be created.
90 91 92 93 94 |
# File 'lib/rucola/fsevents.rb', line 90 def create_stream @stream = OSX.FSEventStreamCreate(@allocator, @callback, @context, @paths, @since, @latency, @flags) raise(StreamError, 'Unable to create FSEvents stream.') unless @stream OSX.FSEventStreamScheduleWithRunLoop(@stream, OSX.CFRunLoopGetCurrent, OSX::KCFRunLoopDefaultMode) end |
#start ⇒ Object
Start the stream. Raises a Rucola::FSEvents::StreamError if the stream could not be started.
98 99 100 |
# File 'lib/rucola/fsevents.rb', line 98 def start raise(StreamError, 'Unable to start FSEvents stream.') unless OSX.FSEventStreamStart(@stream) end |