This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Not (yet) functional. | |
module AssignMatcher | |
class Assign | |
def initialize(assignment) | |
@assignment = assignment | |
end | |
def matches?(target) | |
@target = target | |
@value = assigns[@assignment] | |
!@value.nil? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
OlderNewer