Skip to content

Instantly share code, notes, and snippets.

@Ball
Created October 27, 2009 20:17
Show Gist options
  • Save Ball/219912 to your computer and use it in GitHub Desktop.
Save Ball/219912 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'spec'
require 'spec/autorun'
@watches = []
def watch(matcher, &block)
@watches << Matcher.new(matcher, &block)
end
def do_a_loop(sender, arg)
puts "found a #{arg.change_type} with #{arg.name}"
@watches.each {|w| w.do_if_matched arg.name }
end
def start_watching
watcher = System::IO::FileSystemWatcher.new
watcher.path = "."
watcher.include_subdirectories = true
watcher.notify_filter = System::IO::NotifyFilters.last_write | System::IO::NotifyFilters.file_name | System::IO::NotifyFilters.directory_name
# watcher.changed do |s,a|
# do_a_loop s, a
# end
watcher.created do |s,a|
do_a_loop s, a
end
# watcher.deleted do |s,a|
# do_a_loop s, a
# end
# watcher.renamed do |s,a|
# do_a_loop s, a
# end
watcher.enable_raising_events = true
puts "pres 'q' to quit."
while "q" != System::Console.read
end
end
class Matcher
def initialize(matcher, &block)
@matcher = Regexp.compile(matcher)
@block = block
end
def do_if_matched(arg)
matches = @matcher.match arg
if not matches.nil?
if matches.size == 1
@block.call arg
else
@block.call matches[1]
end
end
end
end
watch( 'lib\\\\(.*)\\.rb' ) do |md|
puts "Running specs because of #{md}"
puts "spec spec\\#{md}_spec.rb"
system "spec spec\\#{md}_spec.rb"
end
watch( 'spec\\\\(.*)_spec.rb') do |md|
puts "Running specs because of specs"
system("spec spec\\#{md}_spec.rb")
end
start_watching()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment