Skip to content

Instantly share code, notes, and snippets.

@softwaregravy
softwaregravy / rspec-syntax-cheat-sheet.rb
Created September 9, 2011 21:25 — forked from dnagir/rspec-syntax-cheat-sheet.rb
RSpec 2 syntax cheat sheet by example
# RSpec 2.0 syntax Cheet Sheet by http://ApproachE.com
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
# Probably the proper way to check a hash for options
def my_method(options = {})
if options.has_key?(:key) && !options[:key].nil?
# do something using the option here
end
end
@softwaregravy
softwaregravy / gist:1210199
Created September 11, 2011 22:11
Options of Nil and Fasle
# The way I basically always check
def my_method(options = {})
if options[:key]
# do something using the option here
end
end
# What I really wanted my logic to be
def my_method(options = {})
if options.has_key?(:key) && !options[:key].nil?
if options[:key]
# do something using the option here
else
# do something else
end
end
end
module DayMap
day_array = []
Date::DAYNAMES.each_with_index{|d, i| day_array << [d, i]}
reverse_day_array = []
Date::DAYNAMES.each_with_index{|d, i| reverse_day_array << [i, d]}
DAY_MAP = Hash[day_array]
REVERSE_MAP = Hash[reverse_day_array]
class << self
@softwaregravy
softwaregravy / time_period_spec.rb
Created October 13, 2011 16:38
Time Period Refactor
# before
describe "<=>" do
it "first compares days" do
l = TimePeriod.create(:start_day => 1, :start_time => 10.hours.to_i, :duration => 1.hours.to_i)
r = TimePeriod.create(:start_day => 2, :start_time => 10.hours.to_i, :duration => 1.hours.to_i)
(l <=> r).should == -1
(r <=> l).should == 1
end
end
@softwaregravy
softwaregravy / business_spec.rb
Created October 13, 2011 16:52
Business spec refactoring
# before
let (:user) { Factory(:user) }
# after
before :all do
@user = Factory(:user)
end
after :all do
@user.destroy
@softwaregravy
softwaregravy / factories.rb
Created October 13, 2011 16:58
User Factory
Factory.define :user do |user|
user.sequence(:email) {|n| "1test#{n}@sample.com"}
user.password 'secret'
user.password_confirmation 'secret'
end
<html>
<body>
<script type="text/javascript">
var lat = 40.733722;
var lon = -73.990683;
var id = "1234567890";
var api_endpoint = "api-sandbox.thinknear.com";
var link_endpoint = "m2-s.thinknear.com";
var api_key = "YOUR API KEY";
var app_id = "YOUR APP ID";
@softwaregravy
softwaregravy / irb.rb
Created March 11, 2012 23:29
Override IRB Prompt
require 'irb'
module IRB
class << self
alias :orig_init_config :init_config
def init_config(ap_path)
begin
puts "loading init config: #{Rails.env}"
# Set up the prompt to be slightly more informative