Skip to content

Instantly share code, notes, and snippets.

View codatory's full-sized avatar

Alex Conner codatory

View GitHub Profile
@codatory
codatory / phrase.rb
Created June 21, 2012 03:32
Fuzzy string matching convenience class
require 'fuzzystringmatch'
class Phrase
attr_reader :matches
def initialize(str, match_score=0.8)
@str = str
@matches = []
@match_score = match_score
@scorer = FuzzyStringMatch::JaroWinkler.create
@codatory
codatory / cleaned_list.rb
Created June 21, 2012 00:51
Ruby Class for creating a list of normalized strings
require 'set'
class CleanedList
def initialize
@data = SortedSet.new
end
def to_a
@data.to_a
end
# Date formats
Time::DATE_FORMATS.merge!(
:friendly_date => lambda {|d| "#{d.month}/#{d.mday}" }, # 4/25 (no leading zeroes)
:friendly_time => lambda {|t|
date = (t.at_midnight == Time.now.at_midnight) ? '' : "#{t.month}/#{t.mday}@" # 4/25@4PM on other days
"#{date}#{t.strftime('%I').to_i}#{t.strftime('%p')}" # 4PM on same day
},
:year => "%Y", # 2008
:month_year => "%b %Y", # Apr 2008
:ampm_time => "%I:%M %p", # 04:15 PM
module Sanity
module ActsAsSaneNestedAttributes
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def acts_as_sane_nested_attributes(options = [])
puts 'Setting up child callbacks'
ActiveRecord::Callbacks::CALLBACKS.each do |callback|
#!/usr/bin/env ruby
QUEUE = Beanstalk::Pool.new(['127.0.0.1:11300'])
MAILR = Mailer.new
MAILR.deliver_digests
# Main program loop
counter = 0
loop do
@codatory
codatory / .tmux.conf
Created April 6, 2012 19:15
TMUX Conf
# Switch me back to ^A, thanks
set-option -g prefix C-a
unbind-key C-b
bind-key a send-prefix
# I miss ^A^A and ^ASpace
bind-key C-a last-window
bind-key Space next-window
bind-key C-Space previous-window
@codatory
codatory / updates.sh
Created March 1, 2012 16:02
Automatically run the current latest check_update script
#!/bin/bash
YUM=`which yum 2> /dev/null`
APT=`which apt-get 2> /dev/null`
YUM_GIST=1926743
APT_GIST=1926909
if [ $YUM ]; then
bash < <(curl --location --silent https://raw.github.com/gist/$YUM_GIST)
@codatory
codatory / check_update.sh
Created February 27, 2012 20:44
Debian / Ubuntu Check update script
#!/bin/bash
apt-get update -qq
UPDATES=`apt-get dist-upgrade --simulate | grep Inst | awk '{print $2}'`
COUNT=`printf "%s\n" "$UPDATES" | grep -v "^$" | wc -l`
echo '--------------------------------------------------------------------------------'
echo " Weekly Update Notification for `hostname`"
echo " -> $COUNT update(s) available"
echo '--------------------------------------------------------------------------------'
printf "%s\n" "$UPDATES"
@codatory
codatory / check_update.sh
Created February 27, 2012 20:16
CentOS / Fedora Weekly update checking script
#!/bin/bash
UPDATES=`yum check-update -q | awk '{print $1}'`
COUNT=`printf "%s\n" "$UPDATES" | grep -v "^$" | wc -l`
YUM=`printf "%s\n" "$UPDATES" | grep -v "^$" | grep yum`
echo '--------------------------------------------------------------------------------'
echo " Weekly Update Notification for `hostname`"
echo " -> $COUNT update(s) available"
if [ -n "$YUM" ]; then
echo ' -> IMPORTANT: A Yum update is available. Perform this separately and first.'
fi
# c = CSVBuilder.new ['column 1 name', 'column 2 name', 'column 3 name']
# rowdata.each do |r|
# special_column = r.boolean ? 'YES' : 'NO'
# c.add_row [special_column, r.name, r.date]
# end
# c.export('optional_filename.csv')
class CSVBuilder
def initialize(head)
if head.is_a?(Array)