Skip to content

Instantly share code, notes, and snippets.

View jbrains's full-sized avatar

J. B. Rainsberger jbrains

View GitHub Profile
@jbrains
jbrains / gist:9451941
Last active August 29, 2015 13:57 — forked from CoryFoy/gist:9441665
# SMELL I don't like the name gemfile_lock_file, but I don't know
# what else to call it. I want to distinguish it from Gemfile.
def extract_gems_from_gemfile_lock_file(contents)
gems = []
# Extract all the non-empty lines between and excluding 'specs:' and 'PLATFORMS'
contents.each_line do |line|
line = line.strip
# It takes a few seconds to understand this algorithm,
# but I can't yet justify replacing it with a functional
# approach. Again, it depends what the reader understands
-- Thanks to @kimwallmark for teaching me `maybe` and showing me how a single lookup.
-- I had the brilliant idea of using `snd`, even though I dislike the name. :)
fizzbuzz :: Integer -> String
fizzbuzz n = maybe (show n) snd $ classify n
where
classify n = find (\(m, _) -> n `mod` m == 0) [(15, "Fizzbuzz"), (5, "Buzz"), (3, "Fizz")]
@jbrains
jbrains / learn_sequel_model_spec.rb
Last active August 29, 2015 14:01
I think I'm figuring out how use Sequel::Model....
require "sequel"
# Use the module to avoid naming collisions with other specs.
module LearnSequelModelSpec
describe "Person Model" do
let(:db) { Sequel.sqlite }
before(:each) do
@jbrains
jbrains / _posting.scss
Created May 31, 2014 16:42
How do I remove the duplication in these SCSS rules?
a.comment-link {
/* Superimposed image */
$icon-comment-height: 10px; /* Must match the size of the graphic */
$icon-comment-width: 10px; /* Must match the size of the graphic */
background: $salmon url(../images/comment.gif) no-repeat $icon-comment-height $icon-comment-width;
color: $salmon;
&:hover {
background: $dark-salmon url(../images/comment.gif) no-repeat $icon-comment-height $icon-comment-width;
@jbrains
jbrains / post-receive
Created September 14, 2014 12:34
A safe approach to publish-on-push with git and Wordpress
#!/bin/sh
# Improved safety by not checking out directly into document root
pushd /home/jbrains
SCRATCH="$(date +%N)"
TARGET="/home/jbrains/domains/jbrains.ca/web/wordpress-jbrains.ca"
# Backup
tar cf "wordpress-jbrains.ca.$SCRATCH.tar" $TARGET
# Clone, then copy
mkdir $SCRATCH
pushd $SCRATCH
@jbrains
jbrains / InstanceVariablesInRSpecMatcher.rb
Created November 9, 2014 16:20
Is there any harm in using instance variables inside an RSpec Matcher like this? Will I keep garbage values from use to use?
RSpec::Matchers.define :eventually_redirect_to do | target_url |
def collect_redirect_urls_from(url)
redirect_urls = []
response = RestClient.get(url) do | response, request, result, &block |
redirect_urls.push(response.headers[:location]) if response.code == 301
response.return!(request, result, &block)
end
redirect_urls
end
@jbrains
jbrains / package.json
Created February 22, 2015 16:16
How well do I understand how Atom activates/deactivates a package with/in spite of having an activation command configured?
{
"name": "status-stats",
"main": "./lib/status-stats",
"activationCommands": ["status-stats:toggle"],
"version": "0.1.0",
"private": false,
"description": "A derivative of @segphault's status-wordcount. Displays wordcount, grade level, and Flesh Kincaid scores, to help you write for your audience.",
"repository": "https://github.com/jbrains/status-stats",
"license": "MIT",
"engines": {
class FindAllUsersRequest extends GetRequest {
public FindAllUsersRequest(parameters) {
super(parameters);
}
public URL getRequestUrl() {
return URL.create("http://www.jbrains.ca/users/all");
}
}
class GetRequestFactory {
static GetRequest findAllUsers(parameters) {
return new GetRequest(requestUrl: URL.create("http://www.jbrains.ca/users/all"), parameters);
}
}
testLanderDecelerates() {
accelerometer = mock(Accelerometer)
lander = Lander.new(accelerometer)
accelerometer.expects().report_acceleration(-50.ms2)
lander.decelerate()
}