Created
July 18, 2012 10:08
-
-
Save avinasha/3135389 to your computer and use it in GitHub Desktop.
Platform dependent gems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
group :production, :test, :cucumber, :staging do | |
gem 'rb-inotify', :require => false | |
end | |
group :test, :development, :cucumber do | |
gem 'rb-fsevent', :require => false | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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