Skip to content

Instantly share code, notes, and snippets.

@bmarini
bmarini / haml.rake
Created February 9, 2011 16:36
Drop this in lib/tasks/haml.rake and run rake haml:assimilate
namespace :haml do
desc "Convert all .erb views to .haml"
task :assimilate => :environment do
git = File.exist?( Rails.root.join('.git') )
chdir Rails.root do
Dir["app/views/**/*.erb"].map do |file|
[ file, file.gsub(/\.erb$/, '.haml') ]
end.each do |(erb, haml)|
sh "bundle exec html2haml #{erb} #{haml}"
@bmarini
bmarini / groovy-delegation.rb
Created January 13, 2011 16:26
Attempting to port groovy's delegate feature as an exercise
# http://dontmindthelanguage.wordpress.com/2009/12/09/groovy-delegate-annotation/
# class Cleaner {
# def doWork() { "clean" }
# }
#
# class Employee {
# def doWork() { "work!" }
# }
#
# class Manager {
@bmarini
bmarini / chef-server-virtualbox-bootstrap.sh
Created January 3, 2011 19:56
Quick start to setup a chef server on virtualbox
#!/bin/bash
#
# Install virtualbox:
# - http://www.virtualbox.org/wiki/Downloads
# - Download iso: http://releases.ubuntu.com/10.10/
# - Create a new virtualbox using the iso as the install media
# - Change network adapter to bridged
# - Use ifconfig to get ip address
#
# Once you have a clean install of ubuntu...
@bmarini
bmarini / modular-classes.rb
Created December 29, 2010 23:21
Rails 3 inspired modularity
# Making a large class modular
# * Put core functionality into Base module inside your class
# * Split other functionality into modules and include them at the bottom of the
# your class
# * Now it is easy to reuse or recreate custom versions of the class by creating
# new classes, including only some modules, swapping modules out for others, etc.
class MainClass
module Base
attr_accessor :name
def initialize(name)
@bmarini
bmarini / bundler-style-dsl.rb
Created December 28, 2010 23:15
Example of how to build a DSL
# Goal: Demonstrate the building blocks for creating a DSL in ruby, based on
# bundler's Gemfile DSL implementation
module Remote
# Responsible for evaluating the specification. This will transform the
# specification into meaningful data structures that can be acted on.
class Dsl
# Given a string of dsl code, create a new instance of dsl and evaluate
# the code within the context of the instance.
@bmarini
bmarini / verbose_log_subscriber.rb
Created December 26, 2010 22:45
a more verbose log subscriber for active resource 3.0
# Create our own log subscriber that logs a little more
class VerboseLogSubscriber < ActiveSupport::LogSubscriber
def request(event)
result = event.payload[:result]
info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}"
info "--> %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration]
info result.body
end
end
@bmarini
bmarini / 960.sh
Created December 26, 2010 19:17
960.css files
curl -sOL https://github.com/nathansmith/960-Grid-System/raw/master/code/css/uncompressed/960.css
curl -sOL https://github.com/nathansmith/960-grid-system/raw/master/code/css/uncompressed/reset.css
curl -sOL https://github.com/nathansmith/960-grid-system/raw/master/code/css/uncompressed/text.css
@bmarini
bmarini / active-record-demo.rb
Created November 30, 2010 23:20
Simple usage of AR outside of rails
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
ActiveRecord::Schema.define do
create_table :authors do |t|
t.string :name, :null => false
end
add_index :authors, :name, :unique
# This module follows best practices for daemonizing processes in a UNIX
# environment with as little fluff as possible. Forks once, calls `setsid`,
# and forks again. STDIN, STDOUT, STDERR are all redirected to /dev/null by
# default. We'll even manage the pidfile
#
# `chdir` and `umask` are optional precautions:
#
# * `chdir` is a safeguard against problems that would occur is you tried to
# launch a daemon process on a mounted filesystem
# * `umask` gives your daemon process more control over file creation
def self.save
super
rescue ActiveRecord::StatementInvalid => e
if e.message =~ /Mysql::Error: Duplicate entry/
errors.add(...)
else
raise
end
end