Skip to content

Instantly share code, notes, and snippets.

@nwjsmith
nwjsmith / ancestors.sql
Created Dec 20, 2015
Recursive ancestors query for PostgreSQL
View ancestors.sql
WITH RECURSIVE ancestors(id, name, created_at, updated_at) AS (
SELECT categories.id, categories.name, categories.created_at, categories.updated_at
FROM categories, categorizations
WHERE categorizations.subcategory_id = '667C9DF5-48C0-496B-922A-1B7D743B1A39'
AND categories.id = categorizations.category_id
UNION ALL
SELECT categories.id, categories.name, categories.created_at, categories.updated_at
FROM categories, categorizations, ancestors
WHERE categorizations.subcategory_id = ancestors.id
AND categories.id = categorizations.category_id
View categories_core.clj
(ns categories.core
(:require [datomic.api :as d]))
(let [uri (str "datomic:mem://" (gensym))
conn (and (d/create-database uri) (d/connect uri))]
@(d/transact conn [{:db/id #db/id[:db.part/db]
:db/ident :category/name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
View outlay.rb
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
View reducers.clj
(ns dropwarlock.reducers)
(defprotocol Reducee
(reduce-with-instructions [this inst f]))
(extend-protocol Reducee
clojure.lang.PersistentVector
(reduce-with-instructions [this [action acc] f]
(case action
:cont (if (empty? this)
@nwjsmith
nwjsmith / bugsnag_deploy.py
Created Sep 15, 2015
An Ansible Library for Bugsnag deploys
View bugsnag_deploy.py
#!/usr/bin/env python
DOCUMENTATION = '''
---
module: bugsnag_deployment
short_description: Notifies Bugsnag of deploys.
options:
api_key:
description:
- The API Key associated with the project. Informs Bugsnag which project
@nwjsmith
nwjsmith / public_send_test.rb
Created May 14, 2015
Delegates can use `#public_send` even though they inherit from `BasicObject`
View public_send_test.rb
Run options: --seed 53381
# Running:
.
Finished in 0.000873s, 1145.2393 runs/s, 1145.2393 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
View optional.rb
class Optional
private_class_method :new
def self.maybe(possible)
possible.nil? ? absent : of(possible)
end
def self.absent
None.new
end
View enum_strings.rb
require 'active_record'
require 'logger'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Base.connection.instance_eval do
create_table(:starting_goalies) do |t|
t.string :status
end
end
View rotate.sh
#!/bin/bash
set -e
cp previous.key intermediate.key
cp current.key previous.key
mv intermediate.key current.key
service nginx reload
View fourth.rb
def evolve_cell(cell, neighbours)
if cell == 1
if neighbours.inject(&:+) == 2 || neighbours.inject(&:+) == 3
1
else
0
end
else
if neighbours.inject(&:+) == 3
1