Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@avinasha
Created July 18, 2012 10:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avinasha/3135389 to your computer and use it in GitHub Desktop.
Save avinasha/3135389 to your computer and use it in GitHub Desktop.
Platform dependent gems
group :production, :test, :cucumber, :staging do
gem 'rb-inotify', :require => false
end
group :test, :development, :cucumber do
gem 'rb-fsevent', :require => false
end
def is_mac?
RUBY_PLATFORM.downcase.include?("darwin")
end
def is_windows?
RUBY_PLATFORM.downcase.include?("mswin")
end
def is_linux?
RUBY_PLATFORM.downcase.include?("linux")
end
require 'rb-inotify' if is_linux?
require 'rb-fsevent' if is_mac?
class InotifyProxy
attr_reader :notifier
#
# Accepts the ablosute_path which the notifier
# will monitor
#
def initialize(absolute_path, options={})
@path = absolute_path
@notifier = INotify::Notifier.new if is_linux?
@notifier = FSEvent.new if is_mac?
end
#
# Accepts a Proc object containing the code
# to be executed when a notifier event occurs
#
def watch(proc=nil)
proc = lambda { |event| puts "Detected change inside: #{event.class}" } if proc.nil? and not(proc.kind_of?(Proc))
if is_mac?
@notifier.watch([@path]) do |event|
proc.call(event)
end
elsif is_linux?
@notifier.watch(@path, :moved_to, :create) do |event|
proc.call(event)
end
end
puts "Watching #{@path}"
end
def run
@notifier.run
end
def stop
@notifier.stop
end
end
# Watch the root
notifier = InotifyProxy.new('/')
notifier.watch
# Run in an infinite loop
notifier.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment