Skip to content

Instantly share code, notes, and snippets.

View norman's full-sized avatar
🪕
Scruggs not drugs

Norman Clarke norman

🪕
Scruggs not drugs
View GitHub Profile
@norman
norman / object.lua
Created April 11, 2011 13:51
ridiculously simple object oriented Lua
local class = {}
local methods = {}
function class.new()
local instance = {}
return setmetatable(instance, {__index = methods})
end
return class
@norman
norman / etag.rb
Last active May 31, 2016 11:39
One way to make Rails consider the layout in an etag.
# By default, changes to the layout temnplate won't cause a Rails etag
# to change. I suppose this is to facilitate Turbolinks. Sometimes you
# might want to change an etag in response to changes in the layout -
# this is particularly true when working in development mode. This is
# one way to do it, there are probably other (read: likely better) ways
# to do this too.
class FooController < ApplicationController
# There's probably some API method to get the name of the layout
# rather than hard-coding it like I did here, but I'm too lazy to
# look it up right now.
@norman
norman / gist:8300884
Created January 7, 2014 15:22
Benchmark comparing Haml 4.0.5 to Haml 4.1.0.beta.1

Haml 4.0.5

ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.4.0]
------------------------------------------------------------------------

1000 Iterations
Rehearsal --------------------------------------------------------
compiled haml pretty   0.550000   0.000000   0.550000 (  0.551526)
compiled haml ugly     0.270000   0.000000   0.270000 (  0.270457)

FriendlyId uses Rails's extending method to avoid overwriting your model's find method. Internally this is implemented similar to this:

def self.friendly
  all.extending(friendly_id_config.finder_methods)
end

This however comes with a performance impact, because extending invokes Ruby's extend, which blows away MRI's method cache. To work around this, FriendlyId lets you include a :finders module which overrides your model's find to increase performance.

@norman
norman / factories.rb
Last active December 22, 2015 07:39
A very minimal alternative to FactoryGirl?
module FlexMinder
module Factories
def factory(klass, attributes)
name = klass.to_s
underscored = name.underscore
class_eval(<<-END, __FILE__, __LINE__ + 1)
def build_#{underscored}(attributes = {})
attributes = valid_#{underscored}_attributes.deep_merge(attributes)
#{name}.new(attributes)
@norman
norman / benchmarks.txt
Last active December 20, 2015 21:58
FriendlyId 5.0 versus 4.0
## FriendlyId 5.0
------------------------------------------------------------------------
Using ruby 2.0.0 AR 4.0.0 with sqlite3 (in-memory)
Rehearsal -------------------------------------------------------------------------
find (without FriendlyId) 0.410000 0.000000 0.410000 ( 0.412285)
find (in-table slug) 0.890000 0.000000 0.890000 ( 0.911959)
find (in-table slug; finders addon) 0.670000 0.010000 0.680000 ( 0.686639)
find (external slug) 1.660000 0.040000 1.700000 ( 1.721548)
insert (without FriendlyId) 0.960000 0.010000 0.970000 ( 0.977678)
" File Name: removeTrailingSpace.vim
"
" Author: Sudipta Ghorui <sudipta05@gmail.com>
"
" Credits: Bitan Kundu <bitan@alumnux.com>
" who always suggested me to do this manualy
"
" Last Modified: 05 May 2006
"
" Description: removes the trailing space from the file when user will save
@norman
norman / date.rb
Created April 10, 2013 14:07
My code before and after reading the docs for Ruby's Date class from stdlib.
# before
class Date
def first_business_day_of_month
date = beginning_of_month
loop do
return date if date.workday?
date = date.next
end
end
end
require "ffaker"
require 'thread'
class ConcurrentHash
def initialize
@reader, @writer = {}, {}
@lock = Mutex.new
end
def [](key)
require "ffaker"
require 'thread'
# Taken from Josh Peek's code at: http://stackoverflow.com/questions/1080993/pure-ruby-concurrent-hash
class ConcurrentHash
def initialize
@reader, @writer = {}, {}
@lock = Mutex.new
end