Skip to content

Instantly share code, notes, and snippets.

View kevinrutherford's full-sized avatar

Kevin Rutherford kevinrutherford

View GitHub Profile
@kevinrutherford
kevinrutherford / spec_helper.rb
Created August 7, 2011 10:59
Mocking warden in controller specs
module RandomHelpers
def random_number() SecureRandom.random_number(10000); end
def random_id() random_number; end
def random_name() SecureRandom.hex(20); end
end
module ControllerHelpers
def user_double(attrs = {})
user_attrs = {
:first_name => random_name,
@kevinrutherford
kevinrutherford / AdjustQualityBy.java
Created December 15, 2014 08:52
Gilded rose solution
public class AdjustQualityBy implements Adjuster {
private int adjustment;
public AdjustQualityBy(int adjustment) {
this.adjustment = adjustment;
}
public void adjust(Item item) {
item.setQuality(item.getQuality() + adjustment);
if (item.getSellIn() < 0)
import deepFreeze from 'deep-freeze'
import configureStore from '../app/store/'
export const reduce = (actions) => {
const store = configureStore()
actions.forEach(action => store.dispatch(action))
return store.getState()
}
export const reductio = (reducer, actions) => {
@kevinrutherford
kevinrutherford / bundle-bisect.sh
Created March 6, 2015 06:51
Sometimes I need to figure out which gem update broke my build.
#! /bin/sh
#
GEMFILE=Gemfile.lock
[ $# -lt 1 ] && { echo "Usage: $0 cmd" >&2; return 1; }
[ -r $GEMFILE ] || { echo "Cannot find $GEMFILE" >&2; return 1; }
cmd="$@"
Pair = Struct.new(:head, :tail)
Either = Struct.new(:val)
class Left < Either
def value(pair, hylo)
pair.head
end
end
import static org.junit.Assert.*;
import org.junit.Test;
public class OcpPotterTester {
private double price(int... books) {
Basket basket = new BasketFactory().create();
return basket.bestPrice(books);
}
import static org.junit.Assert.*;
import org.junit.Test;
public class PotterTester {
private double price(int... books) {
return 0.0;
}
@Test
@kevinrutherford
kevinrutherford / event_bus_spec.rb
Last active December 17, 2015 16:28
How can I remove the duplicated Then clauses in these specs?
context 'EventBus methods cascade' do
context 'clear' do
When(:result) { EventBus.clear }
Then { result.should == EventBus }
end
context 'publish' do
When(:result) { EventBus.publish('aa123bb', {}) }
Then { result.should == EventBus }
@kevinrutherford
kevinrutherford / application_controller.rb
Created May 14, 2013 18:50
Query actions in Rails contollers
class ApplicationController < ActionController
def wiki
@wiki ||= if (current_user.admin?)
AdminWiki.new
else
ReadonlyWiki.new
end
end
@kevinrutherford
kevinrutherford / cronjob.rake
Created July 11, 2012 15:31
Code moved out of the adapter
namespace :cron
task :validate_models => [:environment] do
UseCases::ValidatesPostsAndComments.new.run
end
end