Skip to content

Instantly share code, notes, and snippets.

View tylerhunt's full-sized avatar

Tyler Hunt tylerhunt

View GitHub Profile
@tylerhunt
tylerhunt / decorator.rb
Created February 11, 2013 21:05
Simple Decorator
class Decorator < SimpleDelegator
class Collection < SimpleDelegator
include Enumerable
def initialize(collection, decorator)
super(collection)
@decorator = decorator
end
def each
@tylerhunt
tylerhunt / support.md
Last active December 17, 2015 19:58
The Noise in the Night

In which your humble narrator sends a technical support request to LaCie.

In the wee hours of the morning, I awoke to the sound of a distant, melodic clicking. I ventured downstairs to investigate the source of this noise in the night, only to find the 5big's flashing light of death casting the room in the red hue of alarm. I dutifully checked the drive LEDs on the back panel, finding one of them completely void of any illumination. Panicked at the potential for data loss and acting out of self-preservation in the interest of saving my ears from that wicked racket emanating from aluminum enclosure, I pulled up the dashboard to see that drive four was offline. I made the tough call to power it down. My data may be offline now, but I remain hopeful that, with the right part, it will come alive again with my memories intact.

@tylerhunt
tylerhunt / application_controller.rb
Last active December 18, 2015 12:39
Modify Rails view binding to allow values to be explicitly exposed to the views and telling the instance variables to GTFO.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def render(*args)
options = args.extract_options!
options[:locals] ||= @_exposed
super *args, options
end
def view_assigns
@tylerhunt
tylerhunt / users_controller.rb
Created June 19, 2013 19:02
Example usage of the strong_parameters gem.
class UsersController < ApplicationController
def update
current_user.update_attributes(user_params)
end
private
def user_params
params.require(:user).permit(:email, :password)
end
@tylerhunt
tylerhunt / struct_to_hash.gemspec
Created December 6, 2013 03:10
Monkey patches Struct with a #to_hash method.
Gem::Specification.new do |spec|
spec.name = 'struct_to_hash'
spec.version = '0.0.1'
spec.platform = Gem::Platform::RUBY
spec.author = 'Tyler Hunt'
spec.summary = 'Struct#to_hash'
spec.description = 'Monkey patches Struct with a #to_hash method.'
spec.files = ['struct_to_hash.rb']
spec.test_file = 'struct_to_hash_spec.rb'
@tylerhunt
tylerhunt / Gemfile
Last active October 11, 2023 14:44
ActiveAdmin association autocomplete without complicated plugins.
gem 'active_model_serializers'
gem 'activeadmin'
gem 'jquery-ui-rails'
@tylerhunt
tylerhunt / Gemfile
Created January 27, 2014 17:19
Fetch the most recent power meter reading from EPB.
source 'https://rubygems.org/'
gem 'capybara'
gem 'capybara-webkit'
@tylerhunt
tylerhunt / Gemfile
Created February 7, 2014 00:54
Monkey-patch for Bundler's release Rake task to push to Gemfury instead of RubyGems.org.
source 'https://rubygems.org/'
gem 'gemfury', '~> 0.4.20'
@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)