Skip to content

Instantly share code, notes, and snippets.

require "json"
require "httpclient"
# Usage:
# reevoo_hq = LongLat.from_postcode("SE1 0RF")
# reevoo_hq.long # => -0.10276
# reevoo_hq.lat # => 51.500991
class LongLat
PostcodeLookupError = Class.new(RuntimeError)
class BaseColor
attr_reader :r,:g,:b,:a
def alpha(v)
BaseColor.new(r,g,b,v)
end
def initialize(r, g, b, a = 1.0)
@r, @g, @b, @a = normalize(r), normalize(g), normalize(b), normalize(a, 1.0)
end
# Example Rails Usage:
# config.middleware.use Rack::LessCss, "/stylesheets", File.join(RAILS_ROOT, "public", "stylesheets")
#
# public/stylesheets/foo.less is now used to render /stylesheets/foo.css
#
require 'less'
module Rack
class LessCss
File = ::File
def initialize(app, css_path, css_dir)
# No Boom, it was creating the handle first... cause app Boom
# This does however, as you say, "Boom"
Factory.define :product do |p|
p.product_handles do |p|
[Factory.build(:product_handle)]
end
end
# Only issue here is that the product has knowledge of the handle... knowledge it need not have.
# Example Usage:
#
# FactoryWithCallbacks.define :product do |p|
# p.on_create do |prod|
# Factory(:product_alias, :product => prod)
# end
# end
class FactoryWithCallbacks < Factory
## Tom's Magical "Don't Mate Your Root" Script
# Prevents "mate /" from crashing your Mac.
function mateforce(){
`which mate` $*
}
function line_count_less_than(){
ruby -e "
max_count = ARGV.first.to_i
# Example:
# # Fish has a has_been_caught_by_someone_called(...) scope that is too complex (several joins, and group bys)
# Fish.count # => 999,999,999
# my_fish = Fish.has_been_caught_by_someone_called("bubba")
#
# my_fish.count # => 999,999
# my_fish.update_all(:color => "green") # Too complex, too many joins, group by etc... mysql will not play ball.
# my_fish.all... # => OUT OF MEMORY!!!!!!
# my_fish.each_in_batches{|f| f.update_attribute(:color => "green") } # => Takes more than a decade.
# my_fish.complex_update_all(:color => "green") # => 999,999
@tomlea
tomlea / twitter_echo_bot.rb
Created April 27, 2009 00:33
A script to retweet any direct messages sent to the account.
require "rubygems"
gem "twitter4r"
require "twitter"
require "active_support"
Twitter::Client.configure do |conf|
conf.application_name = "Twitter Echo"
end
username, password = ARGV.shift, ARGV.shift
# The data setup inside the block may take upto a second to be ready to pass the test. But probably much less.
def retrying_for(secconds = 1)
limit = Time.now + secconds
begin
yield
rescue
if Time.now < limit
sleep 0.05
retry
else
@tomlea
tomlea / .bashrc
Created February 27, 2009 12:20
Prevent textmate exploding when you do a "mate /" by accident.
function mateforce(){
`which mate` $*
}
function mate(){
if find $* -type f | ~/line_count_less_than 1000
then
mateforce $*
else
echo "Too many files!"