Skip to content

Instantly share code, notes, and snippets.

@jacaetevha
jacaetevha / splat_vs_index.rb
Created September 27, 2012 14:29
Simple currying in Ruby: splat vs. indexed

ruby-1.8.7-p358

Rehearsal -----------------------------------------
splat   0.390000   0.000000   0.390000 (  0.386936)
index   0.770000   0.000000   0.770000 (  0.775964)
-------------------------------- total: 1.160000sec

            user     system      total        real
splat   0.410000   0.000000   0.410000 (  0.407737)
index   0.750000   0.000000   0.750000 (  0.751499)
@jacaetevha
jacaetevha / gist:3352560
Created August 14, 2012 20:23
Runs any number of tests as an ad-hoc suite
#!/bin/bash
# This method of running tests in a Rails environment
# does not prepare the database first. As a benefit, the
# entire test run is much faster; the obvious drawback
# is that the test database could be out-of-sync.
#
# In order to get your test database in sync, you can pass
# the -m flag.
#
@jacaetevha
jacaetevha / valid_email_valid_url.rb
Created August 10, 2012 13:33
valid email and url
# gratuitously lifted from Vito Botta at http://vitobotta.com/sinatra-contact-form-jekyll/
require 'resolv'
def valid_email?(email)
if email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
domain = email.match(/\@(.+)/)[1]
Resolv::DNS.open do |dns|
@mx = dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
end
add_person = ->(person) {
return [nil,"Name is required"] if String(person.(:name)) == ''
return [nil,"Birthdate is required"] if String(person.(:birthdate)) == ''
return [nil,"Gender is required"] if String(person.(:gender)) == ''
return [nil,"Gender must be 'male' or 'female'"] if person.(:gender) != 'male' &&
person.(:gender) != 'female'
id = insert_person.(person.(:name),person.(:birthdate),person.(:gender),person.(:title))
# [new_person.(:with_id,id),nil] # <====
[person.(:with_id,id),nil] # <==== I think it should be this. Right?
@jacaetevha
jacaetevha / rails_log_sql_extractor.sh
Created June 20, 2012 15:28
a quick BASH script to parse times and SQL statements out of a Rails log
#!/bin/bash
#################################################################
# #
# I don't typically use this on an entire Rails log file, #
# I usually copy out portions of the log that I am interested #
# in and then run it through this script. While this script #
# will work on the bare Rails log I don't recommend it, since #
# the Rails log includes color formatting information and can #
# be quite large. #
@jacaetevha
jacaetevha / login-hook.sh
Created June 3, 2012 03:37
Mac OS/X login/logout hooks to save login/logout information to a Mongo DB
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
MONGO="${MONGO:-/usr/local/bin/mongo}"
MONGO_USER='<mongo username>'
MONGO_PWD='<mongo password>'
MONGO_URL="<database server>:<port>/<database name>"
@jacaetevha
jacaetevha / gist:2836460
Created May 30, 2012 13:53
hooks for DatabaseCleaner in Rails
class ActiveSupport::TestCase
# ...
class << self
def startup
DatabaseCleaner.clean
end
def shutdown
DatabaseCleaner.clean
@jacaetevha
jacaetevha / create_rpm.sh
Created April 17, 2012 18:51
A Bash script that wraps FPM, allowing access to certain environment variables in an RPM post install script
#!/bin/bash
# get the actual directory for this script, resolving links and relative paths
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
ARCH="${ARCH:-x86_64}"
RELEASE="${RELEASE:-`date +%Y_%m_%d_%H%M`}"
@jacaetevha
jacaetevha / terminator-config
Created February 1, 2012 22:22
save this file to ~/.config/terminator/config
[global_config]
enabled_plugins = CustomCommandsMenu, InactivityWatch, TestPlugin, ActivityWatch, TerminalShot, LaunchpadCodeURLHandler, APTURLHandler, MavenPluginURLHandler, LaunchpadBugURLHandler, LayoutManager
title_transmit_bg_color = "#832527"
[keybindings]
[profiles]
[[default]]
scrollback_lines = 4000
[layouts]
[[default]]
[[[child1]]]
module Annotations
def override
@check_method = true
end
def singleton_method_added method_name
if @check_method
clazz = self.ancestors.reject{|e| e == self}.first
unless clazz.methods.include? method_name
raise "None of #{self}'s ancestors defines ##{method_name}"