Skip to content

Instantly share code, notes, and snippets.

View zporter's full-sized avatar
👨‍💻

Zach Porter zporter

👨‍💻
View GitHub Profile
@zporter
zporter / full_url_validator.rb
Created June 18, 2014 20:26
Rails: Full URL validator
class FullUrlValidator < ActiveModel::EachValidator
VALID_SCHEMES = %w(http https)
def validate_each(record, attribute, value)
unless valid_full_url?(value)
record.errors[attribute] << (options[:message] || 'is not a valid URL')
end
end
@zporter
zporter / .gitignore-global
Created June 13, 2014 14:04
Global Gitignore
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
# Packages #
@zporter
zporter / tmux.conf
Created May 8, 2014 14:14
tmux config
# Set prefix to Ctrl-a
set -g prefix C-a
bind C-a send-prefix
# Unbind previous Ctrl-b prefix
unbind C-b
# Change default delay between keystrokes
set -s escape-time 1
@zporter
zporter / chop.exs
Last active August 29, 2015 13:56
Learning Elixir: Guess a number between a given range
defmodule Chop do
def guess(actual, range = low..high) when actual >= low and actual <= high do
_guess(actual, ask(div(low + high, 2)), range)
end
defp _guess(actual, actual, _) do
IO.puts "\nYES! It is #{actual}\n"
end
@zporter
zporter / traffic_light.rb
Last active December 27, 2015 11:59
Confident Ruby: Traffic Light Refactoring
class TrafficLight
class Lamp
%w(name color).each do |attr|
define_singleton_method("#{attr}=") do |value|
define_method(attr) { value }
end
end
def to_s
name
@zporter
zporter / routes.rb
Created September 27, 2012 00:55
OfficeGames Root Path Constraint
class AuthConstraint
def matches?(request)
request.session['user_id'].present?
end
end
Officegames::Application.routes.draw do
constraints(AuthConstraint.new) do
root :to => 'games#index'
end
@zporter
zporter / rack_deflater.rb
Created June 15, 2012 14:48
Fixes deadlock issue on Rack::Deflater
require 'rack/deflater'
module Rack
class Deflater
class GzipStream
def each(&block)
@writer = block
gzip =::Zlib::GzipWriter.new(self)
gzip.mtime = @mtime
@body.each { |part|
@zporter
zporter / field_error_proc.rb
Created November 9, 2011 15:42
customize the Rails error messages that wrap around form fields
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
if html_tag =~ /<label[^>]+/
"<div class='field_with_errors'>#{html_tag}".html_safe
else
"#{html_tag}</div>".html_safe
end
end