Skip to content

Instantly share code, notes, and snippets.

@chrismccord
chrismccord / gist:1507045
Created December 21, 2011 18:13 — forked from lucasfais/gist:1207002
Sublime Text 2 - Useful Shortcuts

Sublime Text 2 – Useful Shortcuts (Mac OS X)

General

⌘T go to file
⌘⌃P go to project
⌘R go to methods
⌃G go to line
⌘KB toggle side bar
⌘⇧P command prompt
@chrismccord
chrismccord / gist:3801696
Created September 28, 2012 19:30
Rake Task to kill postgres test and development database connections
  desc "Kill test and development postgres database connections"
  task :pg_terminate => :environment do

    dbs = []
    dbs << ActiveRecord::Base.configurations["development"]["database"]
    dbs << ActiveRecord::Base.configurations["test"]["database"]
    db_names = "#{dbs.map{|name| "'#{name}'"}.join(", ")}"

    terminated = false
# remove any bad refs
git remote prune origin
# pipe into bash and auto-delete any branches that have been merged into master!
git log master --pretty=format:'%d' | grep '^ (origin' | tr -d ' ()' | sed 's/origin\//git push origin :/'
@chrismccord
chrismccord / gist:5519310
Last active December 17, 2015 00:11
"I have a table that shows the 10 most recent entries and then via Sync new ones get added. Is there an easy way to remove existing entries to keep the total at 10?"
# Assuming your partial is named 'list_row' and your model is 'Todo'
class Sync.TodoListRow extends Sync.View
maxRows: 10
afterInsert: ->
$rows = @$el.siblings("tr")
$rows.first().remove() if $rows.length > @maxRows
@chrismccord
chrismccord / gist:6000108
Created July 15, 2013 13:54
Wrap eredis connection
defmodule ERedis do
@name :eredis_connection
def start do
{:ok, pid} = :eredis.start_link
:global.register_name(@name, pid)
end
def connection do
@chrismccord
chrismccord / gist:6298006
Last active January 8, 2017 21:47
Rails callback/listener approach to shared service object behavior from controllers
class CommentsController < ApplicationController
def create
CommentCreator.new(current_user, params[:comment], self).create
end
private
def create_successful(comment)
@chrismccord
chrismccord / gist:6349128
Last active December 21, 2015 18:39
Access protocol only for specific record
defrecord User, email: nil
defmodule Access.User do
def access(record, attr), do: apply(elem(record, 0), attr, [record])
end
user = User.new(email: "foo@bar.com")
iex> user[:email]
"foo@bar.com"
@chrismccord
chrismccord / gist:6723644
Created September 27, 2013 03:04
zsh vi mode status
vim_ins_mode="[INS]"
vim_cmd_mode="[CMD]"
vim_mode=$vim_ins_mode
function zle-keymap-select {
vim_mode="${${KEYMAP/vicmd/${vim_cmd_mode}}/(main|viins)/${vim_ins_mode}}"
zle reset-prompt
}
zle -N zle-keymap-select
@chrismccord
chrismccord / gist:7806146
Created December 5, 2013 14:42
Elixir 0.11 record bug
# This worked on 0.10
defmodule Records do
defmacro __using__(_) do
quote do
defrecord A, name: "chris"
defrecord B, id: nil, nested: A.new
end
end
@chrismccord
chrismccord / gist:8446557
Last active January 3, 2016 09:59
Nested Macro
defmodule Example do
defmacro __using__(_options) do
quote do
Module.register_attribute __MODULE__, :attr, accumulate: true, persist: false
import unquote(__MODULE__)
end
end
defmacro append(attr) do