Skip to content

Instantly share code, notes, and snippets.

@joakimk
joakimk / gmail.rb
Created July 9, 2012 09:14
Send mail from a plain ruby process using using gmail
# Useful for notifying about errors from deploy scripts, etc.
require_relative "tlsmail"
require "time"
module Gmail
def self.send(subject, body, opts = {})
to = opts[:to]
from = opts[:from]
password = opts[:password]
@joakimk
joakimk / unit_testable_notes.md
Created June 29, 2012 11:40
Experiences designing a unit-testable feature

Thought I'd write down my experiences with trying to design a feature (for a Rails app) in a way so that it's as unit-testable as possible.

The feature is a page to show statistics based on date constraints.

Names below are different from the actual feature.

Call chain:

View/Form -> Controller -> Filter

-> Stats -> Query

@joakimk
joakimk / virtus_blank_string.rb
Created June 26, 2012 08:53
Virtus: empty string on Integer is empty string. By design or bug?
require 'virtus'
class Record
include Virtus
attribute :count, Integer
end
record = Record.new
record.count = ''
@joakimk
joakimk / code.rb
Created June 15, 2012 18:36
Using virtus to implement a date filter that remembers input and converts strings to dates.
# app/models/something/filter.rb
class Something
class Filter
include Virtus
attribute :from, Date, default: Date.new(2011, 1, 1)
attribute :to, Date, default: Proc.new { Date.today } # Proc because Date.today changes every day... :)
end
end
# controller
@joakimk
joakimk / convert.rb
Created May 7, 2012 21:07
Experiment: Making apple docs readable to ruby developers (for MacRuby and RubyMotion)
# The idea is to make this into a userscript that inserts MacRuby translations into Apple docs.
# Prototype written in ruby.
# Untested, unrefactored, prototype code below...
class Declaration
def initialize(class_name, declaration)
@declaration = declaration
@class_name = class_name
end
@joakimk
joakimk / example.md
Created April 28, 2012 21:29
Example of running testbot locally
MacbookAir:tmp $ rvm ree
MacbookAir:tmp $ gem install testbot
Fetching: testbot-0.6.4.gem (100%)
Successfully installed testbot-0.6.4
1 gem installed
MacbookAir:tmp $ testbot --server
testbot --runner --connect localhost
mkdir -p testbotdemo/test; cd testbotdemo
echo 'require "test/unit"' > test/demo_test.rb

echo 'class DemoTest < Test::Unit::TestCase; def test_first; end; end' >> test/demo_test.rb

@joakimk
joakimk / 0_rspec_sphinx.rb
Created April 16, 2012 12:03
Our sphinx testing config, the result of many iterations to make it fast and reliable.
# Use this with https://gist.github.com/2397889 to make it much more stable.
# Usage:
# 1) Put this in spec/support/rspec_sphinx.rb
# 2) In your spec_helper.rb: "RSpec::ThinkingSphinx.setup(config)"
# Expects a spec_helper like:
#
# require 'database_cleaner'
#
@joakimk
joakimk / rspec_retries.rb
Last active June 10, 2016 09:10 — forked from seancribbs/rspec_retries.rb
A simple way to retry examples in RSpec using filters and around
# Based on https://gist.github.com/1718985
# Usage:
# 1) Put in spec/support/rspec_retries.rb
# 2) Call from spec_helper.rb: "RSpec::Retries.setup(config)"
# 3) Change around filter to suit your needs.
# Example output:
# ..............
#
@joakimk
joakimk / separate_id_series.rb
Created April 13, 2012 10:39
RSpec: Separates id series for each table so that tests does not accidentally pass when rows in different tables have the same ID
class RSpec::SeparateIdSeries
# Separates id series for each table so that tests does not
# accidentally pass when rows in different tables have the same ID
def self.setup
# This takes almost a second when using spork for some reason, so skipping it there.
return if Spork.using_spork?
t = Time.now
tables.each_with_index do |table, i|
@joakimk
joakimk / app.yml
Created April 7, 2012 13:23
Teamocil example, opens two windows, and focuses the first with an editor that takes up 75% of the window
session:
name: "app"
windows:
- name: "app-name"
root: ~/Projects/app
splits:
- cmd: "clear"
- cmd: "clear; vim"
width: 75
- name: "console"