Skip to content

Instantly share code, notes, and snippets.

unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
GEMFILE
system 'bundle'
end
module AliasMethodChainResilient
# Give us a module which will intercept attempts to alias methods named in
# the keys of method_aliases, and instead alias the methods named in the
# values of method_aliases.
def self.alias_intercepting_module(method_aliases)
Module.new do
define_method :alias_method do |new_name, old_name|
if method_aliases.key?(old_name.to_sym)
super new_name, method_aliases[old_name.to_sym]
else
time = Time.mktime(2014, 10, 14, 12, 1)
allowed_ranges = [
[11, 59]..[12, 1],
]
formatted_time = [time.hour, time.min]
p allowed_ranges.any? { |range| range.cover?(formatted_time) }
#!/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 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)
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
@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