Skip to content

Instantly share code, notes, and snippets.

module ShouldaContextExtensions
def self.included(base)
base.class_eval do
alias_method_chain :build, :fast_context
alias_method_chain :am_subcontext?, :fast_context
end
end
def fast_context(name, &blk)
@fast_subcontexts ||= []
@chrisk
chrisk / duration_helper.rb
Created November 5, 2009 07:00
DurationHelper#format_duration
module DurationHelper
# Takes a number of seconds as input and returns a formatted string. See
# tests for examples.
def format_duration(seconds, options = {})
return "0 seconds" if seconds.floor == 0
options.reverse_merge!(:parts_to_show => 4, :word_style => :short, :specific_end => false)
description = (options[:word_style] == :short) ?
[ ['sec', 60], ['min', 60], ['hr', 24], ['day'] ] :
@chrisk
chrisk / filtered-top.sh
Created December 10, 2009 22:23
Monitor the processes from a set of pid files in top
# Monitor the processes from a set of pid files in top
# This doesn't work on OS X, because its top doesn't have -p.
top -p $(cat /var/run/*.pid | tr '\n', ',' | sed s/,$//)
@chrisk
chrisk / post_tweets_to_campfire.rb
Created December 10, 2009 22:29
Post tweets to Campfire
#!/usr/bin/ruby
require 'rubygems'
require 'twitter'
require 'httparty'
require 'json'
require 'action_view'
include ActionView::Helpers
@chrisk
chrisk / campfire.rb
Created December 10, 2009 22:49
Stateful version of the Integrity notifier for Campfire
# A stateful version of the Integrity notifier for Campfire. It only pings the room
# when the build is broken or when it's just been fixed. This is nice when you already
# have a post-receive hook posting every commit to the same room.
require 'rubygems'
require 'integrity'
require 'tinder'
require 'action_view'
module Integrity
@chrisk
chrisk / remove_empty_vendor_directories.rb
Created January 4, 2010 16:46
Rails initializers that clean up git-submodule droppings
module EmptyDirectoryHelper
def self.dir_contains_files?(dir)
entries = Dir.entries(dir).reject { |entry| entry == '.' || entry == '..' }
entries.any? { |entry| !File.directory?(File.join(dir, entry)) || dir_contains_files?(File.join(dir, entry)) }
end
def self.empty_vendor_dirs
Dir.glob(Rails.root.join("vendor/{gems,plugins}/*")).reject { |dir| dir_contains_files?(dir) }
end
end
@chrisk
chrisk / compact_progress_bar_formatter.rb
Last active September 4, 2015 06:04
CompactProgressBarFormatter for RSpec 1
# Originally by Nicholas A. Evans. See LICENSE.txt
#
# Note: includes modifications by Chris Kampmeier to use '=' for the progress
# bar mark and keep the cursor from hopping all around during the animation
#
# This version is compatible with RSpec 1.2.9 and ProgressBar 0.9.0
require 'spec/runner/formatter/base_text_formatter'
require 'progressbar'
@chrisk
chrisk / copy_pivotal_members.rb
Created February 1, 2010 09:40
Copy Pivotal Tracker members to all projects
#!/usr/bin/env ruby
# This script uses Pivotal Tracker's API to generate a list of unique members
# from all of your projects; it then iterates over each project, adding any of
# those members who are missing. That way, everyone has access to everything.
#
# It only acts on projects belonging to accounts called ACCOUNT_NAME below.
#
# By default, the script doesn't proceed with the modifications, so you can
# double-check the pending changes. Run it again with --no-dry-run to proceed.
@chrisk
chrisk / gist:356034
Created April 5, 2010 04:49
Check an email address domain's validity using MX records
# Check an email address domain's validity using MX records
require 'resolv'
def known_email_domain?(email)
domain = email.match(/\@(.+)/)[1]
Resolv::DNS.open { |dns| dns.getresources(domain, Resolv::DNS::Resource::IN::MX) }.any?
end
@chrisk
chrisk / cucumber_fakeweb_hook.rb
Created May 7, 2010 02:57
Using FakeWeb to stub out HTTP requests made by a server process from Cucumber's remote runners (Selenium, Mechanize, etc.)
# config/initializers/cucumber_fakeweb_hook.rb
# If this looks like a test server process, make its FakeWeb available to other
# processes via DRb. That way the test runner (which runs in a separate process)
# can dynamically register stubs for this server to use.
if Rails.env.cucumber? && defined?(PhusionPassenger)
require 'drb'
require 'fakeweb'
DRb.start_service("druby://localhost:30010", FakeWeb)