Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env ruby -wKU
# Don't allow user to commit code containing the symbol "NOCOMMIT".
if system "git diff --cached | grep -q '^\+.*NOCOMMIT'"
puts "Can't commit: NOCOMMITs found in code."
system "git grep --cached -n NOCOMMIT"
exit 1
else
exit 0
module ValidationMatcher
class BeValid
def matches?(target)
@target = target
@target.valid?
end
def failure_message
"expected #{@target} to be valid, but:\n" +
@target.errors.map { |a, m| "- #{a.humanize} #{m} (#{a}: #{@target[a].inspect})" }.join("\n")
end
module HaveAMatcher
class HaveA
def method_missing(attribute)
@attribute ||= attribute
self
end
def matches?(target)
@target = target
raise ArgumentError, "No attribute specified for have(:a)" unless @attribute
@value = @target.__send__(@attribute)
@Peeja
Peeja / spec_helper.rb
Created October 17, 2008 20:07
Add to your spec_helper.rb to make your view spec render calls DRYer.
# Lets you just say 'render' in a view spec. The path is
# discovered from the describe block.
# This is here so you can config.include(RenderHelper) to use this feature.
module RenderHelper
def self.included(mod)
if mod.ancestors.include? Spec::Rails::Example::RailsExampleGroup
Spec::Rails::Example::ViewExampleGroup.instance_eval do
def inherited(subclass)
super
# config.include(HaveTagExtensions) to do things like:
#
# response.should have_tag("form input#email") do
# with_value("dewey@duckberg.gov")
# end
#
# response.should have_tag("strong") do
# with_text("Important!")
# end
module HaveTagExtensions
# Not (yet) functional.
module AssignMatcher
class Assign
def initialize(assignment)
@assignment = assignment
end
def matches?(target)
@target = target
@value = assigns[@assignment]
!@value.nil?
# klass.first_responder(:method) gives you the Module in klass's inheritance chain which will respond to :method.
# klass.all_responders(:method) gives you all of the Modules in klass's inheritance chain which can respond to :method, in order from klass to Kernel.
require 'metaid'
class Object
def first_responder(method)
method = method.to_s
metaclass.ancestors.find do |mod|
mod.instance_methods(false).include? method
# Git aliases for bash
alias gst='git status'
alias gf='git fetch'
alias gl='git pull'
alias gp='git push'
alias gc='git commit -v'
alias gca='git commit -v -a'
alias gb='git branch'
alias gba='git branch -a'
@Peeja
Peeja / cereal_pagination.rb
Created November 20, 2008 16:02
CerealPagination: Demeter's Revenge for will_paginate.
# CerealPagination: Demeter's Revenge for will_paginate.
#
# For every has_many :foos association, creates a paginate_foos
# method which calls foos.paginate.
#
# TODO: Support has_and_belongs_to_many.
module CerealPagination
def self.included(base)
base.instance_eval do
@Peeja
Peeja / .autotest
Created November 21, 2008 18:33
If you've got pending migrations, tells you why all your tests/specs failed.
# If you've got pending migrations, tells you why all your tests/specs failed.
module Autotest::Migrations
Autotest.add_hook :ran_command do |autotest|
if autotest.class.name =~ /Rails/
system "rake db:abort_if_pending_migrations"
end
end
end