Skip to content

Instantly share code, notes, and snippets.

View tylerhunt's full-sized avatar

Tyler Hunt tylerhunt

View GitHub Profile
@tylerhunt
tylerhunt / README.md
Last active August 29, 2015 13:56
Demonstrate an issue with reaching the maxclients limit in Redis, even when using a connection pool.

Start a Redis server with a limited number of clients:

$> /usr/local/opt/redis/bin/redis-server --port 6380 --maxclients 5

Then, run the script:

$> ruby redis_connection_pool.rb

The script should fail with the following error during the second iteration:

@tylerhunt
tylerhunt / simulator.rb
Last active August 29, 2015 13:56
An example showing how to use factory_girl without polluting the global scope.
require 'factory_girl'
class Simulator
def initialize(model, options={}, &block)
self.model = model
self.strategy = options.fetch(:strategy) { FactoryGirl::Strategy::Build }
self.factory = define(&block)
end
def build(overrides={}, &block)
@tylerhunt
tylerhunt / keybase.md
Created April 18, 2014 14:30
Keybase Proof

Keybase proof

I hereby claim:

  • I am tylerhunt on github.
  • I am tylerhunt (https://keybase.io/tylerhunt) on keybase.
  • I have a public key whose fingerprint is 9E14 9931 48C7 405F 4372 B2A5 ACAA 8C6F 2C0A A4C5

To claim this, I am signing this object:

@tylerhunt
tylerhunt / validations.rb
Last active August 29, 2015 14:00
Using ActiveModel::Validations standalone.
# What I should have to do:
require 'active_model/validations'
# What I actually have to do:
require 'active_support/callbacks'
require 'active_support/concern'
require 'active_support/core_ext/module/delegation'
require 'active_model/callbacks'
require 'active_model/errors'
require 'active_model/naming'
@tylerhunt
tylerhunt / void.rb
Created August 15, 2014 18:47
Void macro for Ruby
module Void
def void(method_name)
method = instance_method(method_name)
remove_method method_name
define_method(method_name) do |*args, &block|
method.bind(self).call(*args, &block).tap do |return_value|
raise TypeError, 'unexpected return value' unless return_value.nil?
end
end
@tylerhunt
tylerhunt / my_rack_app.rb
Created September 29, 2014 12:25
Rack app object with middleware
class MyRackApp
def initialize(app)
stack = Rack::Buidler
# stack.use …
stack.run app
@app = stack.to_app
end
def call(env)
@tylerhunt
tylerhunt / dependencies.rb
Created February 28, 2015 15:31
A macro to allow object dependencies to be specified at the class level with a block defining a default value. Based on attr_defaultable.
module Dependencies
# Ensure dependencies from the superclass are inherited by the subclass.
def inherited(subclass)
if superclass.respond_to?(:dependencies)
subclass.dependencies.concat dependencies
end
super
end
@tylerhunt
tylerhunt / gist:206213
Created October 9, 2009 18:03
Rack middleware that redirects requests to a canonical host.
class CanonicalHost
def initialize(app, host=nil, &block)
@app = app
@host = (block_given? && block.call) || host
end
def call(env)
if url = url(env)
[301, { 'Location' => url }, ['Redirecting...']]
else
" Vim filetype detection file
" Language: Ruby
" Maintainer: Tyler Hunt <tyler@tylerhunt.com>
" Version: 1
" Last Change: 2010 May 26
"
augroup ruby
" Ruby metafiles
au! BufRead,BufNewFile Gemfile setfiletype ruby
au! BufRead,BufNewFile config.ru setfiletype ruby
@tylerhunt
tylerhunt / coerce_blanks.rb
Created January 5, 2011 13:54
A Rails plugin for stripping strings on models and replacing empty strings with nil values.
module CoerceBlanks
def write_attribute(attr_name, value)
if value.is_a?(String)
value.strip!
value = nil if value.empty?
end
super
end
end