Skip to content

Instantly share code, notes, and snippets.

View dkubb's full-sized avatar
🏠
Working from home

Dan Kubb dkubb

🏠
Working from home
  • Betterment
  • Mission, BC, Canada
  • X @dkubb
View GitHub Profile
@dkubb
dkubb / eager_repository.rb
Last active May 13, 2019 21:10
Eager Loading DataMapper Associations
require 'memoist'
# Usage:
#
# customers = Customer.preload(Customer.orders.line_items.item)
#
# customers.each do |customer|
# customer.orders.each do |order|
# order.line_items.each do |line_item|
# line_item.item # yay, no more N+1, only 4 queries executed !
@dkubb
dkubb / gist:3055616
Last active October 6, 2015 21:28
Check alignment of curly braces in specs
#!/usr/bin/ruby
# encoding: utf-8
require 'pathname'
paths = ARGV.map { |arg| Pathname.glob(arg) }.flatten
paths.each do |path_in|
start_path = path_in.directory? ? path_in + '**/*' : path_in
@dkubb
dkubb / .gitignore
Created December 5, 2011 18:26
Test Feedzirra w/VCR using an HTTP proxy
cassettes
#!/opt/local/bin/ruby
# Copyright (c) 2007, 2011 Dan Kubb <[email protected]>
require 'rubygems'
require 'csspool'
require 'hpricot'
require 'net/https'
require 'open-uri'
@dkubb
dkubb / explanation.md
Created October 24, 2011 02:32
Veritas Explanation

https://github.com/dkubb/veritas

High level design

  • In Veritas a relation is an Enumerable object that will yield a set of tuples.
  • Internally a relation is represented as a tree.
  • The leaf nodes are base relations, which are the data sources.
  • There are 3 types of inner nodes:
    1. A node may be a relational algebra operation like join, rename, project or others. When iterated it will evaluate it's children, then perform the operation in-memory and yield each tuple
@dkubb
dkubb / gist:1294429
Created October 18, 2011 01:57
Reflect on a PostgreSQL Database
# gem install backports veritas veritas-optimizer veritas-do-adapter do_postgres
require 'pp'
require 'rational'
require 'rubygems'
require 'backports'
require 'backports/basic_object'
require 'veritas'
require 'veritas-optimizer'
@dkubb
dkubb / veritas_migration.rb
Created October 11, 2011 06:48
Example data migration using Veritas
class MyMigration < Veritas::Migration
def call
# Transformations
# ---------------
# 1) remove all columns except id, name and email
# 2) rename user_id to id
# 3) add a boolean active column, setting the default to false
# 4) add a constraint on name where it must be between 1 and 100 characters
@dkubb
dkubb / random_routing_number.rb
Created August 17, 2011 05:11
Generate random ABA routing number
# Random is available in 1.9 (or 1.8 w/backports)
def random_routing_number
digits = Random.new.rand(10_000_000..99_999_999).to_s.split('')
sum = digits.each_slice(3).inject(0) do |sum, (d1, d2, d3)|
sum + 7 * d1.to_i + 3 * d2.to_i + 9 * d3.to_i
end
digits.push(sum % 10).join
@dkubb
dkubb / slim_stream.rb
Created August 14, 2011 07:23
Stream output using Slim
#!/usr/bin/env ruby -Ku
# encoding: utf-8
require 'stringio'
require 'rubygems'
require 'slim'
class StreamBuffer < Temple::Generator
@dkubb
dkubb / .rvmrc
Created August 10, 2011 22:44
Web framework Spike
rvm use @$(basename `pwd`) --create