Skip to content

Instantly share code, notes, and snippets.

View ColinDKelley's full-sized avatar

Colin Kelley ColinDKelley

  • Invoca, Inc.
  • Santa Barbara, CA
View GitHub Profile
@ColinDKelley
ColinDKelley / lwq-example.rb
Created March 13, 2014 00:07
limited_work_queue-example.rb
queue = LimitedWorkQueue(EventMachine, 2)
messages.each do |message|
queue.add do
message.send # no more than 2 of these will run at the same time
end
end
@ColinDKelley
ColinDKelley / parallel_sync-example.rb
Created March 13, 2014 00:26
parallel_sync-example.rb
responses =
ExceptionalSynchrony::ParallelSync.parallel(EventMachine) do |parallel|
messages.each do |message|
parallel.add { message.send }
end
end
responses.each { ... }
@ColinDKelley
ColinDKelley / modified_singleton-example.rb
Created March 13, 2014 00:50
modified_singleton-example.rb
class Secrets
def initialize(secrets_path)
@hash = YAML.load_file(secrets_path)
end
def [](key)
@hash[key]
end
def set_instance
require 'mysql2'
module Invoca
module Mysql2AdapterKillOnTimeout
def kill(thread_id)
@connection.query("KILL #{thread_id}")
end
def execute(sql, name = nil)
@ColinDKelley
ColinDKelley / parse_fig_leaf.rb
Created August 15, 2014 23:30
Fig leaf to intercept parse with American date formats like 03/04/05
module ParseFigLeaf
def parse(*args)
arg0 = args.first
arg0.is_a?(String) && arg0 =~ /\A\d\d[-\/]\d\d[-\/]\d\d/ and raise "Ambiguous parse format in #{arg0.inspect}"
super
end
end
class << Date
prepend ParseFigLeaf
@ColinDKelley
ColinDKelley / example_usage.rb
Last active August 29, 2015 14:06
retry_on_exception
result =
retry_on_exception(Timeout::Error, total_tries: 2) do
http_fe.post(...)
end
tiles = {}
STDIN.readlines.map.with_index do |line, y|
x = 0
line.chomp.split(".").each do |segment|
unless segment.empty?
tile = [x, segment.size, segment[0]]
if value = tiles[tile]
tiles[tile] = [value.first, value.last + 1]
else
@ColinDKelley
ColinDKelley / user.rb
Created April 9, 2010 20:36
our user.rb with Hobo Fields
class User < ApplicationModel
fields :null=>false do
first_name :string
last_name :string
time_zone :string
encrypted_password :binary, :limit => 255
last_login_at :datetime, :null=>true
tos_agreed_date :datetime, :null=>true
registered_IP_address :string, :null=>true
contact_country_code :string, :limit => 3, :default => ""
class User < ActiveRecord::Base
attr_default :first_name, 'First'
attr_default :last_name, 'Last'
attr_default :name_addr, lambda { "#{full_name} <#{email}>" }
attr_default :email, lambda { organization.default_email }
def full_name
[first_name, middle_name, last_name].select { |name| !name.blank? } * ' '
end
@ColinDKelley
ColinDKelley / gist:2927004
Created June 13, 2012 22:48
Hobo example
class User < ActiveRecord::Base
fields do
first_name :string, :ruby_default => 'First'
last_name :string :ruby_default => 'Last', :null => false, :default => ''
name_addr :string, :ruby_default => lambda { "#{full_name} <#{email}>" }
email :string :ruby_default => lambda { organization.default_email }
level :enum, :limit => [:Basic, :Admin, :Super], :default => :Basic, :null => false
end
def full_name