Skip to content

Instantly share code, notes, and snippets.

View cjheath's full-sized avatar

Clifford Heath cjheath

View GitHub Profile
#
# A Custom Matcher for RSpec that shows the difference between two multi-line strings.
# Author: Clifford Heath.
# Usage:
# actual_text.should_not differ_from(expected_text)
#
require 'diff/lcs'
module FileMatcher
aget "/notifications/:n" do |n|
channel = $channels[n] and
subscription = channel.subscribe { |msg|
body(msg)
}
on_close { channel.unsubscribe(subscription) }
end
grammar PivotalStories
rule stories
story*
end
rule story
story_label:one_line "\n" feature*
end
rule feature
#! /usr/bin/env ruby
require 'rubygems'
require 'hpricot'
require 'open-uri'
site = "http://www.tomwaits.com"
download_to = "/Users/gus/tmp/"
index = open(site + "/songs/") { |f| Hpricot(f) }
@cjheath
cjheath / array_matcher.rb
Created October 21, 2010 03:37
RSpec differ_from matcher (calls other matchers)
module RSpec
module Matchers
class ArrayMatcher < Matcher
def initialize expected
super(:be_different_array_from, expected) do |*_expected|
match_for_should do |actual|
perform_match(actual, _expected[0])
@extra + @missing != []
end
namespace :video do
desc "Create a bunch of sent messages to test reporting & analytics, with random contacts"
task :transcode do
Dir["*.mov"].each do |mov|
# REVISIT: Get width and height from the video:
width = 568
height = 386
ogv = mov.sub(%r{.mov}i, '.ogv')
@cjheath
cjheath / scope_variables.rb
Created December 8, 2010 05:29
ScopeVariables - set and get local variables in a method that passed you a block(!)
#
# ScopeVariables is a collection of utility methods to access variables
# relative to a specific Proc or Binding. It allows getting and assigning
# local variables in a method which passed us a block, for example:
#
# def monkey &block
# block.var_set("v1", "goodbye")
# end
#
# class Test
#! /usr/bin/env ruby
#
# This code works (or fails!) differently on MRI, Rubinius and JRuby,
# also depending on whether the Fixnum monkey-patches are included.
# Note that the patches exist to apply a to_i to the parameter of Fixnum@eql?
#
# If Ruby's Hash claims to use only "hash" and "eql?" to store and find
# items, why can't the Ruby interpreters agree on *how*?
#
# In particular:
@cjheath
cjheath / gist:909129
Created April 8, 2011 01:26
An alternative to the builder pattern, e.g. for configuration APIs.
class Capybara
class << self
attr_accessor :default_wait_time
def magic(a,&b)
puts "doing magic for #{a}"
yield
puts "mischief managed"
end
object.flash3times = function() {
object.attr({fill: "#FF0"});
var times = 3;
var flashMore = function() {
// Make it go dim:
object.attr({fill: "#CC0"});
if (--times > 0)
setTimeout(function() {
object.attr({fill: "#FF0"});