Skip to content

Instantly share code, notes, and snippets.

View timuruski's full-sized avatar
🏳️‍⚧️
Protect Trans Rights

Tim Uruski timuruski

🏳️‍⚧️
Protect Trans Rights
View GitHub Profile
@timuruski
timuruski / coordinated_siege.rb
Created April 16, 2012 18:08
Use siege to benchmark specific routes in your app.
#!/usr/bin/env ruby
if ARGV.empty?
warn "Need a list of URLs to benchmark"
exit 1
end
class Siege
REQUESTS = 10
CONCURRENCY = 1
@timuruski
timuruski / default_addresses.rb
Created April 23, 2012 20:06
Is this too crazy>
## Duplicated methods:
def default_billing_address=(address)
raise CustomerDoesNotOwnAddress if address.addressable != self
self.default_billing_address_id = address.id
end
def default_billing_address
billing_address = self.addresses.find(default_billing_address_id) if default_billing_address_id.present?
billing_address ||= self.addresses.order(:created_at).first
@timuruski
timuruski / gist:2572187
Created May 1, 2012 23:02 — forked from isa/gist:2571012
Convert in less than 30 lines
Question: Convert following into the latter data structure in less than 30 lines:
List:
A, B, C
A, C, E
E, F, D
D, A, J
E, D, J
List
a = [1,2,3]
a.map { |n| [n, n * n] }
# => [[1, 1], [2, 4], [3, 9]]
a.flat_map { |n| [n, n * n] }
# => [1, 1, 2, 4, 3, 9]
class Invoice < ActiveRecord::Base
has_many :line_items
# ...
delegate :<<, :to => :line_items
end
class User < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :user
end
alice = User.create
@timuruski
timuruski / reverb.rb
Created August 24, 2012 15:23
An experiment with pseudo-callable objects.
module Reverb
def self.included(base)
@@verbs ||= []
@@verbs << base
end
def self.inject(base)
base.class_exec do
Array(@@verbs).each do |verb|
verb_name = verb.name.to_sym
@timuruski
timuruski / break-point.rb
Created September 5, 2012 17:13
Quick and dirty controller breakpoints in Rails
class ApplicationController
# ...
class BreakPoint < StandardError
def initialize(messages)
@messages = messages
end
def to_s
@messages.join("\n")
# Emulate ActiveRecord's block <3
class Invoice
def initialize
yield self
end
end
# Checkout delegate to the invoice
class Checkout
def initialize(invoice)
class CheckoutForm < ActiveRecord::Base
attr_accessible :delivery_type # ...
# ...
module Defaults
def delivery_type
super || default_delivery_type
end