Skip to content

Instantly share code, notes, and snippets.

Avatar

Brandon Keepers bkeepers

View GitHub Profile
View gist:1909309
# puts (1..100).to_a.shuffle.inspect
class Array
def shuffle
size.downto(1) { |n| swap(n - 1, rand(n)) }
self
end
def swap(x, y)
self[x], self[y] = self[y], self[x]
end
@bkeepers
bkeepers / gist:1901470
Created Feb 24, 2012
Spine Collections
View gist:1901470
class Message extends Spine.Model
@configure 'Message', 'text'
class Mentions extends Spine.Collection
@configure Message
@extend Spine.Model.Ajax
@url: '/messages/mentions'
@bkeepers
bkeepers / plugin.rb
Created Sep 30, 2011
Theoretical plugin API for Qu
View plugin.rb
module AutoRetry
include Qu::Plugin
configure do
def autoretry(*errors)
self.config = {:limit => 3}.merge!(errors.extract_options!)
errors << Exception if errors.empty?
config[:errors] = errors
@bkeepers
bkeepers / gist:1197658
Created Sep 6, 2011
:in and :from associations in MongoMapper
View gist:1197658
class List
include MongoMapper::Document
key :user_ids, Array, :typecast => ObjectId
many :users, :in => :user_ids
end
class User
include MongoMapper::Document
View output
$ ruby serialization.rb
Sizes:
marshal: 44
msgpack: 35
json: 46
Rehearsal -------------------------------------------
marshal 0.840000 0.030000 0.870000 ( 0.878109)
msgpack 0.420000 0.060000 0.480000 ( 0.467086)
json 1.310000 0.130000 1.440000 ( 1.444504)
---------------------------------- total: 2.790000sec
@bkeepers
bkeepers / gist:951751
Created May 2, 2011
Creating custom validations in MongoMapper
View gist:951751
module MyCustomValidation
extend ActiveSupport::Concern
module InstanceMethods
def do_the_validation
errors.add(:foo, 'is not valid') unless some_condition_is_met?
end
def some_condition_is_met?
true
@bkeepers
bkeepers / factory_girl.js
Created Apr 15, 2011
Syntax for a JavasScript implementation of factory_girl
View factory_girl.js
Factory.define('game', Game)
.sequence('id')
.attr('is_over', false)
.attr('created_at', "2011-02-01T22:36:45-06:00")
.sequence('random_seed')
.attr('players', function() {
return [
Factory.attributes('player'),
Factory.attributes('player')
];
@bkeepers
bkeepers / benchmarks_multi.rb
Created Dec 1, 2010
delayed_job benchmarks with multiple workers
View benchmarks_multi.rb
require 'spec/spec_helper'
require 'logger'
require 'benchmark'
workers = 4
puts "#{workers} workers"
Benchmark.bm(10) do |x|
Delayed::Job.delete_all
(1000...10001).step(500).each do |n|
View party.rb
module Kernel
def the(a, b)
a.new(b)
end
def roof
Exception
end
def yo!
@bkeepers
bkeepers / gist:716299
Created Nov 26, 2010
DRB cucumber formatter
View gist:716299
require 'drb'
module Cucumber
module Formatter
class Drb
SERVER_URI = "druby://localhost:8787"
def initialize(step_mother, io, options)
DRb.start_service
@formatter = DRbObject.new_with_uri(SERVER_URI)