Skip to content

Instantly share code, notes, and snippets.

@mschuerig
mschuerig / rake.sh
Created June 7, 2012 22:30
Better Bash Completion for Rake
case "$COMP_WORDBREAKS" in
*:*) : great ;;
*) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
esac
__rakefile_path() {
(
while [ "$PWD" != / ]; do
if [ -f "Rakefile" ]; then
@mschuerig
mschuerig / show_view_structure.rb
Created June 8, 2012 06:41
Rails initializer that adds comments to generated markup identifying which template it came from
unless Rails.env.production?
# Inspired by http://github.com/fcheung/tattler/blob/master/lib/tattler.rb
#
class ActionView::Template
def render_with_comment(*args, &block)
render_result = render_without_comment(*args, &block)
if mime_type.nil? || mime_type.in?([Mime::HTML, Mime::XML])
[
"<!-- STARTTEMPLATE: #{identifier} -->",
render_result,
@mschuerig
mschuerig / view_structure.rb
Created June 8, 2012 06:44
Display a hierarchical listing of templates and which other templates they include
#! /usr/bin/ruby1.9.1
require 'find'
require 'set'
module Tools
class ViewStructure
include Enumerable
EXTENSIONS = '.erb'
@mschuerig
mschuerig / console_tools.rb
Created June 8, 2012 06:46
Tools for the Rails console
if Rails.env.development? || Rails.env.test?
module ConsoleTools
EDITOR = ENV['CONSOLE_TOOLS_EDITOR'] || 'kwrite'
BROWSER = ENV['CONSOLE_TOOLS_BROWSER'] || 'firefox -new-window'
def edit_object(obj = @response)
# TODO add support for XML nodes
case obj
#when ActiveRecord::Base
@mschuerig
mschuerig / forget_task.rb
Created June 8, 2012 06:50
Forget a Rake task
class Rake::Task
# Forget an already defined task.
#
# Rake tasks with the same name are cumulative,
# a new definition adds to an already existing one,
# it does not override it.
# In order to completely redefine a task, this
# method makes rake forget the previous definition(s).
#
def forget
@mschuerig
mschuerig / hash_at.rb
Created June 8, 2012 06:50
Safely access an element in a nested hash by its path.
class Hash
# Safely access an element in a nested hash by its path.
# If any of the intervening hashes is not present, nil is returned.
def at(*path)
val = self
path.each do |k|
val = val[k]
return nil unless val
end
val
@mschuerig
mschuerig / sprockets_asset_uri.rb
Created June 8, 2012 06:57
CSS helper that inserts small files as data URIs
# Monkey-patch another helper into Sprockets that expands small files
# into data URIs.
module Sprockets
class Context
def asset_uri(path)
fullpath = environment.find_asset(path)
if fullpath && File.file?(fullpath) && File.size(fullpath) < 2048
asset_data_uri(path)
else
path
@mschuerig
mschuerig / after_use_switch_gemfiles_copy
Created June 10, 2012 15:28
Switch Gemfile.lock when rvm use'ing another ruby
#!/usr/bin/env bash
# Place this file at ~/.rvm/hooks/after_use_switch_gemfiles
# and make it executable.
#
# This version copies Ruby-specific Gemfile.lock.x to Gemfile.lock.
if [ -e "Gemfile" ]; then
. "${rvm_path}/scripts/functions/hooks/jruby"
unset current_platform gemfile_platform
@mschuerig
mschuerig / cheat-completion.sh
Created June 14, 2012 08:36
Bash completion for cheat
_cheat()
{
local sheets
list="$HOME/.cheat/.sheets"
if [ ! -f "$list" ]; then
cheat sheets | sed -n 's/^ \(.*\)$/\1/p' > "$list"
fi
sheets=$(cat "$list")
@mschuerig
mschuerig / cucumber-completion.sh
Created June 16, 2012 15:21
Bash completion for cucumber
__cucumbercomp() {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $( compgen -W "$1" -- "$cur" ) )
}
__cucumbertags() {
sed -r -n 's/^\s*(((@[[:alnum:]]+)\s?)+).*$/\1/p' features/**/*.feature \
| sed -r 's/\s+/\n/'
}