Skip to content

Instantly share code, notes, and snippets.

@bguthrie
bguthrie / argument_examples.rb
Created February 27, 2011 04:00
Several method signatures used in a DSL like ActiveRecord, somewhat simplified.
# def initialize(attributes = {})
Pirate.new :name => "Greenbeard", :ship => "The Queen Anne's Renegotiation"
# def has_many(of_what, options = {})
has_many :crew_members, :class_name => "Pirate", :foreign_key => "captain_id"
# def validates_presence_of(*arrrrrgs)
validates_presence_of :parrot, :peg_leg, :blank => false
# Gem idea, extracted from ResourceFull. Thoughts on the API?
class User < ActiveRecord::Base
named_scope :born_after, lambda {|date|
{ :conditions => ["birthdate >= ?", Date.parse(date)] }
}
query_set :filter do
filter_with_scope :active
queryable_with :email
# Found on the ZshWiki
# http://zshwiki.org/home/config/prompt
#
# As lifted from http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/
function collapse_pwd {
echo $(pwd | sed -e "s,^$HOME,~,")
}
function prompt_char {
package proovy;
import groovy.xml.dom.DOMCategory
import org.lobobrowser.html.parser.*
import org.lobobrowser.html.test.*
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPathFactory
import javax.xml.xpath.XPathConstants
import css2xpath.*
#!/usr/bin/env ruby
if ARGV.empty?
puts "Usage: gemmate <name-of-gem>"
else
src = `gem which #{ARGV[0]}`.split.last
lib_dir = src.slice 0, src.rindex("lib")
`mate -a '#{lib_dir}'`
end
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
UPS_URL = "http://wwwapps.ups.com/WebTracking/processRequest?AgreeToTermsAndConditions=yes&loc=en_US&tracknum="
if ARGV.empty?
puts "Usage: ups_tracker tracking_number" and exit
end
class BlankHelper
include ActionView::Helpers::UrlHelper
include ActionView::Helpers::TagHelper
include ActionView::Helpers::TextHelper
include ActionView::Helpers::FormTagHelper
include ActionView::Helpers::CaptureHelper
include ActionView::Helpers::AssetTagHelper
attr_accessor_with_default :output_buffer, ""
# Give the stack a name, and describe its members.
stack = Awsymandias::EC2::ApplicationStack.new("test") do |s|
s.role "db", :instance_type => "m1.large", ...
s.role "app", :instance_type => "c1.xlarge", ...
end
# Check if we're running by pulling stack description from SDB; if not, launch asynchronously.
stack.launch unless stack.running?
until stack.running?
sleep(5)
user should receive(username, name, firstName) andReturn("guybrush")
; is equivalent to
user should receive(username: "guybrush", name: "guybrush", firstName: "guybrush")
user should receive(reload, save) anyNumberOfTimes
; is equivalent to
user should receive(reload anyNumberOfTimes, save anyNumberOfTimes)
; is equivalent to
user should receive reload anyNumberOfTimes
user should receive save anyNumberOfTimes
user = mock! name andReturn("Guybrush", "Elaine", "LeChuck")
user name should == "Guybrush"
user name should == "Elaine"
user name should == "LeChuck"