Skip to content

Instantly share code, notes, and snippets.

@paneq
paneq / observer.rb
Created November 26, 2010 18:25
Observer simplification
#BEFORE
class Observer < ActiveModel::Observer
class_attribute :observed_methods
self.observed_methods = [].freeze
def initialize
super
observed_descendants.each { |klass| add_observer!(klass) }
end
@paneq
paneq / unit.rb
Created December 11, 2010 16:11
Capturing IO in Ruby
##
# Captures $stdout and $stderr into strings:
#
# out, err = capture_io do
# warn "You did a bad thing"
# end
#
# assert_match %r%bad%, err
def capture_io
require 'minitest/unit'
MiniTest::Unit.autorun
class TestMeme < MiniTest::Unit::TestCase
def test_flunk
pid = fork do
exec("sleep 100") # firefox in reality
end
require 'minitest/unit'
MiniTest::Unit.class_eval do
def self.autorun
at_exit {
next if $! # don't run if there was an exception
# the order here is important. The at_exit handler must be
# installed before anyone else gets a chance to install their
# own, that way we can be assured that our exit will be last
@paneq
paneq / assert_differences.rb
Created December 29, 2010 11:32
Useful assertions for unit testing.
module Test::Unit::Extensions
module AssertDifferences
# assert_stable_count(Person) <=> assert_no_difference("Person.count")
def assert_stable_count(expression, &block)
assert_same_count(expressify_class(expression), &block)
end
# assert_removed(Person) <=> assert_difference("Person.count", -1)
@paneq
paneq / capc.rb
Created January 27, 2011 18:50
Creating an object with Capybara api and jumping into it.
#!/usr/bin/env ruby
require 'bundler'
Bundler.setup(:default, :test) if defined?(Bundler)
require "selenium-webdriver"
require 'capybara/dsl'
Capybara.default_driver = :selenium
Capybara.default_selector = :css
Capybara.default_wait_time = 5
@paneq
paneq / touch.watchr
Created April 15, 2011 10:56
watchr script to observe changes in Rails.root/app/ directory and store the info about last change time in hidden Rails.root/.watchr file. # This file: touch.watchr is in Rails.root/scripts/
require 'fileutils'
rails_root = File.expand_path('../..', __FILE__)
watchr_file = File.join(rails_root, '.watchr')
watch( 'app/(.*)\.rb' ) {|md| FileUtils.touch(watchr_file) }
@paneq
paneq / bootstrap.rb
Created April 15, 2011 11:03
Changed piece of railties-3.0.7.rc1/lib/rails/application/bootstrap.rb Clear dependencies before request only when some changes were made to any file observed by watchr.
initializer :set_clear_dependencies_hook do
unless config.cache_classes
changed_at = Proc.new{ File.new(File.join(Rails.root, '.watchr')).mtime }
last_change = changed_at.call
ActionDispatch::Callbacks.before do
change = changed_at.call
if change > last_change
Rails.logger.info("DETECTED CHANGES")
last_change = change
@paneq
paneq / nested.rb
Created April 28, 2011 12:59
Creating nested procs with inject
inside = Proc.new{ puts 'inside' }
nested = [1,2,3].reverse.inject(inside) do |sum, obj|
Proc.new do |&inner|
puts obj
sum.call(&inner)
puts obj
end
end
@paneq
paneq / development.rb
Created May 1, 2011 07:44
Start watchr if we are in `rails s`
if defined?(Rails::Server) # rails s only (not in console)
fork do
exec("watchr", "script/touch.watchr")
end
end