Skip to content

Instantly share code, notes, and snippets.

View hypomodern's full-sized avatar

Matt Wilson hypomodern

View GitHub Profile
(function($) {
$.fn.selectText = function() {
var doc = window.document;
return this.each(function() {
var selection, range;
if (window.getSelection && doc.createRange) {
// modern FF, Safari, and Opera (I think)
selection = window.getSelection();
range = doc.createRange();
range.selectNodeContents(this);
@hypomodern
hypomodern / rspec_tips.markdown
Created January 10, 2011 19:42
quick rspec tips

With rspec 1.x:

spec path/to/file_spec.rb:LINE_NUMBER

With rspec 2.x:

rspec path/to/file_spec.rb:LINE_NUMBER
rspec --tag TAG_NAME

BTW, tagging is accomplished like so:

@hypomodern
hypomodern / sublime_text_2_user_keybindings.json
Last active September 25, 2015 03:27
My preferred SublimeText2 User Prefs
[
{ "keys": ["super+backquote"], "command": "auto_complete" }
]
@hypomodern
hypomodern / synchrony_multi_example.rb
Created May 13, 2011 15:17
How do I use EM::Synchrony::Multi inside a controller action?
# So, I have an controller action that makes multiple calls to third-party APIs over http.
# I'd like to parallelize these calls via EventMachine::Synchrony::Multi, but can't figure out how to wire the controller
#
# This works fine from, say, a script on the command line, but not in a controller running on a thin server.
#
# What I was missing: a) rack/fiber_pool, b) we don't need the EM.synchrony block
class DemoController < ApplicationController # or a sinatra app, same result
# ...
def evented
multi = nil
@hypomodern
hypomodern / gist:1296572
Created October 18, 2011 20:13
google.coffee
request = require('request')
querystring = require('querystring')
module.exports =
help: [
{
command: '@agora image QUERY',
description: 'Fetches the first Google Image for QUERY'
}
]
@hypomodern
hypomodern / config.ru
Created February 20, 2012 03:51
HOWTO: Bundler.setup in a sinatra app
$LOAD_PATH << '.'
$LOAD_PATH << 'lib'
require 'rubygems'
require 'bundler'
Bundler.setup :default, (ENV['RACK_ENV'] || 'development')
# your requires here...
@hypomodern
hypomodern / gist:2279482
Created April 1, 2012 23:39
What I'm currently doing to test behavior when an external service fails
client.stub!(:connection).and_return(
Faraday.new do |builder|
builder.adapter :test do |stub|
stub.get('laikal1.card') {[ 500, {}, 'Oh Noes! I blew it up!' ]}
end
end
)
VCR.turned_off do
response = client.fetch
response.status.should == 500
@hypomodern
hypomodern / gist:2316750
Created April 6, 2012 04:06 — forked from cadwallion/gist:2316695
Array() shenanigans...
>> Array([])
=> []
>> Array(nil)
=> []
>> Array([{foo: 'bar'}])
=> [{:foo=>"bar"}]
>> Array([{foo: 'bar'}, {bar: 'baz'}])
=> [{:foo=>"bar"}, {:bar=>"baz"}]
>> Array({foo: 'bar'})
=> [[:foo, "bar"]]
@hypomodern
hypomodern / hash_ext.rb
Created May 22, 2012 15:51
Ruby Hash#fetch_path
class Hash
def fetch_path(path, default = nil)
pieces = path.split('.', 2)
value = self[pieces[0]]
return value if value && !pieces[1]
return default if !value.respond_to?(:fetch_path) || !value
value.fetch_path(pieces[1], default)
end
end
@hypomodern
hypomodern / blocks_are_procs.rb
Created May 24, 2012 20:11
Blocks become of class Proc in a useful context ;)
class Proc
def foobar
puts "...method on Proc!"
end
end
def block_class &block
block.call
block.foobar
puts block.class.inspect