Skip to content

Instantly share code, notes, and snippets.

@rogerrohrbach
rogerrohrbach / capistrano_database_yml.rb
Created June 10, 2012 19:01 — forked from weppos/capistrano_database_yml.rb
Provides a couple of tasks for creating the database.yml configuration file dynamically when deploy:setup is run.
#
# = Capistrano database.yml task
#
# Provides a couple of tasks for creating the database.yml
# configuration file dynamically when deploy:setup is run.
#
# Category:: Capistrano
# Package:: Database
# Author:: Simone Carletti <weppos@weppos.net>
# Copyright:: 2007-2010 The Authors
@rogerrohrbach
rogerrohrbach / version_vector.rb
Created June 28, 2011 14:08
Version vector class
class VersionVector
# D. S. Parker, et al. Detection of Mutual Inconsistency in
# Distributed Systems. IEEE Trans. Softw. Eng. 9, 3 (May 1983), 240-247.
def initialize
@versions = {}
@versions.default = 0
end
def [](site)
@rogerrohrbach
rogerrohrbach / nanp_validator.rb
Created June 27, 2011 14:42
Active Model North American Telephone Number validator
# == Active Model North American Telephone Number Validator
# http://en.wikipedia.org/wiki/North_American_Numbering_Plan#Current_system
# [Author] Roger Rohrbach (roger@ecstatic.com)
class NanpValidator < ActiveModel::EachValidator
def self.matcher(require_area_code) # :nodoc:
%r{
(?<country_code> \+1 ){0}
(?<trunk_prefix> 1 ){0}
(?<delimiter> ([-\.]|\ +) ){0}
@rogerrohrbach
rogerrohrbach / gist:701703
Created November 16, 2010 11:08
Sound like U2's guitarist by using Euler's number to set delay effect (in ms)
require 'singleton'
module U2
class Edge
include Singleton
attr_accessor :delay
def set_delay_for_tempo(bpm)
# http://5cense.com/Edge_Delay.htm
@rogerrohrbach
rogerrohrbach / geo.rb
Created July 3, 2010 19:51
Calculate the distance between two points on Earth
module Geo
def self.distance(point_a = {}, point_b = {})
# http://en.wikipedia.org/wiki/Haversine_formula
r = 6371.009 # IUGG mean radius of the Earth
deg2rad = lambda { |d| d * Math::PI / 180 }
sin2 = lambda { |x| Math.sin(x) ** 2 }
lat1 = deg2rad.call(point_a['lat'])