Skip to content

Instantly share code, notes, and snippets.

View pda's full-sized avatar
💾
Formatting…

Paul Annesley pda

💾
Formatting…
View GitHub Profile
@pda
pda / README.md
Created November 7, 2011 12:43
Ruby Time usec float bug?

Ruby Time usec float bug?

Most of the time-modification methods in ActiveSupport use [Time#change][1] which boils down to instantiating a new Time object.

If [Time#change][1] is passed a null-change hash, e.g. {} or { :month => 0 }, then the resulting Time object should compare equally to the original.

This appears to be the case in Mac OS X (Ruby 1.9.3-p0 and 1.9.2-p290) but does not

@pda
pda / .gitconfig
Created November 17, 2011 05:12
Git config
[user]
name = Paul Annesley
email = ...
[color]
ui = auto
[alias]
st = status
co = checkout
l = log --graph
r = !git --no-pager l -20
@pda
pda / categorizable.rb
Created November 30, 2011 10:58
Example of class @instance_var that used to be a @@class_var
module Category::Categorizable
extend ActiveSupport::Concern
included do
def self.categorizable_through(table)
has_many table, dependent: :destroy
@categorizable_join_model = Kernel.const_get(table.to_s.singularize.classify)
end
@pda
pda / example.rb
Created January 9, 2012 06:46
MethodHuntingDelegator
class Book < SomeModelThing
attr_reader :title, :blurb
end
class Course < SomeModelThing
attr_reader :name, :description
end
class SearchResult < MethodHuntingDelegator
@pda
pda / spec_helper.rb
Created March 14, 2012 06:23
Fast RSpec with a tiered environment: SPEC=(unit|model|full) rspec …
mode = ENV["SPEC"] || "full"
require_relative "spec_helper_#{mode}"
@pda
pda / client.rb
Created March 28, 2012 08:52
Simple Dependency Injection and MiniTest::Mock
require "addressable/uri"
require "net/http"
module Ralexa
class Client
# Hacked to simplify example. Full version at:
# https://github.com/flippa/ralexa/blob/master/lib/ralexa/client.rb
def initialize(*credentials)
@pda
pda / http_patch_initializer.rb
Created August 1, 2012 03:00
Bits of HTTP PATCH support for Rails 3.2
# Support for HTTP PATCH.
# Should not be necessary as of Rails 4.
# See: actionpack/lib/action_dispatch/routing/mapper.rb
module ActionDispatch
module Routing
class Mapper
module HttpHelpers
# Define a route that only recognizes HTTP PATCH.
@pda
pda / www.rb
Created August 9, 2012 02:23
www: Serve the current directory via HTTP.
#!/usr/bin/env ruby
# Serve the current directory via HTTP.
# Like Python's SimpleHTTPServer, but with no-cache headers.
# Default port 8000, specify alternate port as first parameter:
# www 3000
# sudo www 80 # (probably a bad idea)
# Inspired by http://chrismdp.github.com/2011/12/cache-busting-ruby-http-server/
@pda
pda / query_spy.rb
Created September 3, 2012 04:05
QuerySpy for counting ActiveRecord queries, asserting against n+1 regressions.
# An SQL query spy derived from the perhaps deprecated SQLCounter
# in ActiveRecord::TestCase and/or activerecord/test/cases/helper.rb
class QuerySpy
# Returns the number of queries executed by yielding the given block.
# Examples:
#
# QuerySpy.count { get "/things" }.should == 1
#
# create_a_thing
@pda
pda / base_form.rb
Created September 12, 2012 07:15
A form model base class for Rails.
require "active_model"
# Base class for Rails forms that aren't backed by a single database model.
# Implement a #save method and treat it like any other model.
#
# class ExampleForm < BaseForm
#
# # Declare attributes.
# required_attribute :email
# attribute :name