Skip to content

Instantly share code, notes, and snippets.

@fj
fj / gist:583670
Created September 17, 2010 04:02
Given /^an? (.+) exists with an? (.+) of "([^"]*)"$/ do |model, field, value|
factory_name = model.gsub(' ', '_')
Factory factory_name, field => value
end
## Rails 3 doesn't seem to handle PUT methods correctly.
## routing
FruitApplication::Application.routes.draw do
resources :fruits do
member do
put :peel # A HTTP PUT method that will map to fruits/:id/peel
end
end
@fj
fj / gist:595528
Created September 24, 2010 15:12
56 def fully_refundable?
57 self.room_stays.map(&:room_rate).all? { |rate|
58 rate.cancellation_rules.reject { |rule|
59 rule.deadline && (Time.now.utc < rule.deadline.utc)
60 }.empty? # Rcov says there's no coverage on the commented lines,
61 } # but the others have coverage.
62 end # How is that possible?
@fj
fj / gist:600987
Created September 28, 2010 13:29

Here's an interesting design question I ran into recently. Some details have been changed for client confidentiality reasons.

  • You operate a car rental aggregation service called ParkingGarage. Individual rental agencies use your API to tell you about excess inventory, which they then give to you to sell.

  • ParkingGarage doesn't actually sell the inventory, however. Instead, it acts as a sort of rental-car exchange and makes the inventory available to third-party services who would like to book reservations. ParkingGarage charges a 5% fee on each reservation as a commission and gives the balance to the rental agency that posted the inventory.

  • Word of your service spreads and it gets very popular. Three affiliates AppleAudis, BananaBeetles, and CoconutCars sign up to be your first sellers. Each affiliate is a distinct, separate service that derives its inventory from a variety of sources, of which ParkingGarage is only one. They love your API, and they're selling cars like hotcakes.

  • As things grow, th

@fj
fj / gist:627089
Created October 14, 2010 21:25
Discussion with #cucumber folks about the best way to persist state across scenarios
(2010-10-14 16:07:24) jsquared (irc.freenode.net): What's the best way to persist data between invocations of a Cucumber test?
16:15
(2010-10-14 16:17:29) jherdman: jqsuared: do you mean between individual steps, or entire scenarios?
(2010-10-14 16:18:21) jsquared (irc.freenode.net): jherdman: Entire scenarios. I have a test which makes a request to an external system which then returns a confirmation number. Then that confirmation number is used by other scenarios.
(2010-10-14 16:18:41) jherdman: i'd stub that shit. you don't want external services being involved with your stories
(2010-10-14 16:19:47) jherdman: that way you also don't need to do anything weird to save a value between scenarios ;)
16:20
(2010-10-14 16:21:33) jsquared (irc.freenode.net): jherdman: Well, this is a bit of an unusual use for Cucumber. We're getting certified against the external system by sending a series of messages to it and testing that the responses match what is expected.
(2010-10-14 16:21:52) jsquared (irc.freenode.net): j
ruby-1.8.7-p299 > class Vehicle
ruby-1.8.7-p299 ?> include Mongoid::Document
ruby-1.8.7-p299 ?> field :horsepower
ruby-1.8.7-p299 ?> field :top_speed_mph
ruby-1.8.7-p299 ?> end
=> nil
ruby-1.8.7-p299 > Vehicle.new(:horsepower => 300, :top_speed_mph => 180).save
=> true
@fj
fj / gist:660318
Created November 2, 2010 21:19
Pop quiz: What's wrong with this Ruby code?
# This won't print what we expect. Why not?
# Can you determine the answer without running the code?
zip_codes = {
"California" => 90210,
"Maine" => 07347,
"New York" => 11234,
"Virginia" => 22207,
"North Carolina" => 10489
}
@fj
fj / cucumber-failures.txt
Created November 8, 2010 09:31
Test failures in the Cucumber repo.
(in /home/_library/_proj/experimental/cucumber)
bundle exec /home/johnf/.rvm/rubies/ruby-1.9.2-p0/bin/ruby -I "/home/_library/_proj/experimental/cucumber/lib:lib" "/home/_library/_proj/experimental/cucumber/bin/cucumber" --profile ruby_1_9
Using the ruby_1_9 profile...
...........................................................................................................................................................................................................................................................................................................................................................................................................................................................F..F...................................................................................................................................................................................................................Using Cucumber
StepMother has been deprecated and will be gently put to sleep at the next major
@fj
fj / gist:705568
Created November 18, 2010 20:37
Thinking about invoking methods by including a module
module Pinger
extend ActiveSupport::Concern
module ClassMethods
def ping_when(method)
puts "ping! #{method} was called"
# what goes here?
end
end
end
# We're using Mongoid, but we don't want Stateflow to actually persist
# when the state changes.
Stateflow.persistence = :mongoid
class Insect
include Mongoid::Document
include Stateflow
stateflow do
state_column :state