Skip to content

Instantly share code, notes, and snippets.

@avinasha

avinasha/Gemfile

Created Jul 18, 2012
Embed
What would you like to do?
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