Skip to content

Instantly share code, notes, and snippets.

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

David Krentzlin certainty

🏠
Working from home
View GitHub Profile
((UP "CREATE TABLE IF NOT EXISTS edge (in_vertex INTEGER,out_vertex INTEGER)")
(DOWN "DROP TABLE IF EXISTS edge"))
((UP ,(lambda (db) (do-something-nifty-with db)))
(DOWN ,(lambda (db) (undo-something-nifty-with db))))
@certainty
certainty / nomads-example3.scm
Created December 26, 2010 15:12
irreversible migrations
((UP "CREATE TABLE vertices (id INTEGER, name STRING)")
(DOWN (#f . "You must not remove vertices") ))
@certainty
certainty / migrate.scm
Created December 26, 2010 15:21
nomads
(use nomads nomads-sql-de-lite fmt fmt-color filepath)
(migration-directory "./migrations")
(database-credentials "test.db")
(define (get-version)
(let ((version (get-environment-variable "VERSION")))
(if version
(or (string->number version) (string->symbol version))
'latest)))
@certainty
certainty / test.rb
Created June 2, 2012 08:52
A test with AR
class Base < ActiveRecord::Base
self.abstract_class = true
end
# the table foobars has the column abort:string
class Foobar < Base
self.table_name = "foobars"
end
@certainty
certainty / gist:3135048
Created July 18, 2012 08:30
interface matcher
class MissmatchRecorder
attr_reader :record
def initialize
@record = ""
end
def <<(msg)
@record << msg
end
@certainty
certainty / gist:3135595
Created July 18, 2012 11:06
there you go
your_map = @users.inject({}) do |m,u|
duplicates = @users.select{ |u2| u.id != u2.id && u.email == u2.email }
(m[u.email] ||= []).concat(duplicates) unless duplicates.empty?
m
end
class Company < ActiveRecord::Base
attr_accessible :name
has_many :parent_associations, :class_name => 'Association', :foreign_key => 'child_id'
has_many :child_associations, :class_name => 'Association', :foreign_key => 'parent_id'
has_many :parents, :through => :parent_associations, :source => :parent
has_many :children, :through => :child_associations, :source => :child
end
@certainty
certainty / gist:3143730
Created July 19, 2012 13:01
Map method
Object.send(:alias_method,:_m,:method)
class Foo
def reverse(thing)
thing.reverse
end
def map_reverse(things)
things.map(&_m(:reverse))
end
class User < ActiveRecord::Base
validates :phone_number, presence: true, length: { is: 11 }, if: :gsm_number_empty?, unless: :password_or_email_changed?
validates :gsm_number, presence: true, length: { is: 11 }, if: :phone_number_empty?, unless: :password_or_email_changed?
private
def password_or_email_changed?
encrypted_password_changed? || email_changed?
end
def phone_number_empty?
!phone_number.present?