Skip to content

Instantly share code, notes, and snippets.

@coreyhaines
coreyhaines / some_spec.rb
Created March 20, 2012 23:44 — forked from stevenharman/some_spec.rb
Using Rspec's "let" inside an "it" block. Crazy? Yes. Useful? Occasionally.
describe "#mentions_story?" do
subject { described_class.new(file) }
let(:file) { "COMMIT_EDITMSG" }
before do
File.stub(:read).with(file) { example_commit_message(@relevant_part) }
end
context "commit message contains the special Pivotal Tracker story syntax" do
it "matches just the number" do
@relevant_part = "[#8675309]"
@coreyhaines
coreyhaines / old_school_use.rb
Created September 11, 2012 14:52
Environment-based initializers in Rails - USE THEM!
# what? why does your production code care what environment it is in? SILLY!
unless Rails.development? || Rails.test?
Slottd::CreatesReservationTimer.for(slot.id)
end
@coreyhaines
coreyhaines / Foldit.exs
Last active April 4, 2018 20:53
Convert a list of paths to a tree
defmodule FolditTest do
use ExUnit.Case
doctest Foldit
def to_keyword([], keyword), do: keyword
def to_keyword([val], keyword) do
Keyword.update(keyword, nil, [val], fn existing -> [val | existing] end)
end
@coreyhaines
coreyhaines / timer.rb
Created February 4, 2012 09:14
Timer for coderetreat session
#!/usr/bin/env ruby
# Usage ruby 2-minute-timer.rb "message to say when time is up"
# or just chmod +x timer.rb and ./timer.rb
# for more info, see http://coderetreat.org/profiles/blogs/new-session-idea-baby-steps
message = ARGV[0] || "times up"
minutes = 2
seconds = minutes*60
while(true)
@coreyhaines
coreyhaines / Id.elm
Last active July 8, 2017 18:49
The Id type I like
type Id
= Id Int
idIs : Id -> { a | id : Id } -> Bool
idIs thisId =
idFieldIs .id thisId
idFieldIs : (a -> Id) -> Id -> a -> Bool
@coreyhaines
coreyhaines / Animation.elm
Last active June 19, 2017 14:48
Animation support library
module Animation exposing (..)
import Task
import Process
import Time exposing (Time, millisecond)
type Animation state
= Setup state
| Animate state
@coreyhaines
coreyhaines / UserAlert.elm
Created January 27, 2017 22:18
User Alerts
module Shared.UserAlert exposing (UserAlert, Msg, show, startShowAlertAnimation, startHideAlertAnimation, default, update, view)
import Helpers exposing (classes)
import Shared.Animation as Animation
import Html exposing (Html, div, span, text)
import Html.Attributes exposing (class)
import Task
type UserAlert
@coreyhaines
coreyhaines / Api.Error.elm
Last active December 9, 2016 21:25
Elm Form Builder for Hearken
-- support stuff for remote stuff
type alias ErrorMessages =
Dict.Dict String (List String)
type alias ApiError =
{ message : String
, errors : ErrorMessages
, explanation : Maybe String
}
@coreyhaines
coreyhaines / Elm.rake
Created December 9, 2016 18:24
Rake task to compile/copy elm
namespace :elm do
Apps = [ "WorkspaceMain", "ManageSubscribersMain", "NotebookMain", "ResponsesMain" ]
JsFileName = "irn_elm.js"
JsOutputDir = "app/assets/javascripts"
desc "Updates packages, compiles the Elm code and copies it to #{JsOutputDir}"
task :compile_and_copy => [:package_install, :make, :copy] do
puts "Updated Packages, Compiled and copied Elm code to #{JsOutputDir}"
end
function directory_to_titlebar {
local pwd_length=42 # The maximum length we want (seems to fit nicely
# in a default length Terminal title bar).
# Get the current working directory. We'll format it in $dir.
local dir="$PWD"
# Substitute a leading path that's in $HOME for "~"
if [[ "$HOME" == ${dir:0:${#HOME}} ]] ; then
dir="~${dir:${#HOME}}"
fi