Skip to content

Instantly share code, notes, and snippets.

@betamatt
betamatt / action.rb
Created June 27, 2015 19:16
Fully async snell controller action
def update
user = User.find(1)
if user.update_attributes(params[:user])
respond_with(@user)
else
respond_with(@user.errors, :status => :unprocessable_entity)
end
end
@betamatt
betamatt / split_lines.clj
Created December 21, 2014 19:18
Line sequencing
(defn- split-lines
"Takes a sequence of content chunks and returns a lazy sequence of individual lines.
(\"abc\" \"de\\nabc\") becomes (\"abcde\" \"abc\")"
[stream]
(let [
chunk (first stream)
remainder (rest stream)
[line leftover] (string/split chunk #"\n+" 2)]
(if leftover
(lazy-seq (cons line (split-lines (cons leftover remainder))))
@betamatt
betamatt / deferred_touch.rb
Created September 16, 2014 19:18
Deferred ActiveRecord touches
# When using this, be careful about your testing transactions.
# ActiveRecord after_commit hooks don't play nice with transactional fixtures
# consider adding the test_after_commit gem to your test group
ActiveRecord::Base.class_eval do
DEFERRED_KEY = "deferred"
def touch_with_defer(name = nil)
raise ActiveRecordError, "cannot touch on a new record object" unless persisted?
defer_until_commit([self.class.name, id, name, "touch"].map(&:to_s).join(":")) { touch_without_defer(name) }
end
@betamatt
betamatt / either.swift
Created June 24, 2014 03:16
Hacking around Swift's generics bugs
class Cell<T> {
let cell: T[]
var value: T {
get { return cell[0] }
}
init (_ value:T) {
self.cell = [value]
}
@betamatt
betamatt / mountain-lion-brew-setup.markdown
Created August 15, 2012 14:27 — forked from myobie/mountain-lion-brew-setup.markdown
Get Mountain Lion and Homebrew to Be Happy

Get Mountain Lion and Homebrew to Be Happy

1) Install XCode 4.4 into /Applications

Get it from the App Store.

2) Install Command Line Tools

In XCode's Preferences > Downloads you can install command line tools.

@betamatt
betamatt / hide_images.user.js
Created June 22, 2012 14:51 — forked from johnmaxwell/hide-inline-images.js
A userscript for Flowdock that adds a button next to inline elements like Tweets and Images that allows you to hide them. Really useful to hide annoying animated gifs and hubot pug bombs
// ==UserScript==
// @name Hide Images
// @namespace http://fluidapp.com
// @description Adds a button to hide images
// @include *
// @author Someone
// ==/UserScript==
// originally from: https://gist.github.com/1649063
@betamatt
betamatt / blackhole.rb
Created March 26, 2012 17:46
Blackhole server that accepts connections but never responds.
require 'eventmachine'
module Blackhole
def post_init
puts "CONNECT"
end
def receive_data data
puts "RECEIVE: #{data}"
end
@betamatt
betamatt / Procfile
Created January 26, 2012 23:29
Running delayed_job with foreman
queue: ./queue.sh
@betamatt
betamatt / delayed_mailer.rb
Created July 11, 2011 14:25
Delay Rails 2.x mailers
# Handle mail delivery in the background. Only the minimum amount of data should go in the job not
# the fully rendered email content.
# I haven't tested this but some variant should work.
ActionMailer::Base.class_eval do
def self.create_and_deliver!(message, *parameters)
new(message, *parameters).deliver!
end
def method_missing_with_delay(method_symbol, *parameters)#:nodoc:
case method_symbol.id2name
@betamatt
betamatt / faye.rb
Created April 10, 2011 20:23
Faye monit wrapper
#!/usr/env ruby
base_dir = File.expand_path("../..", __FILE__)
NAME="faye"
PID="#{base_dir}/tmp/pids/#{NAME}.pid"
COMMAND="bundle exec rackup -s thin -E production -p 3001 faye.ru"
case ARGV[0]
when "start"