Skip to content

Instantly share code, notes, and snippets.

@mschuerig
mschuerig / git-pre-commit-check.rb
Created June 23, 2012 12:51
Pre-commit hook for git
#! /usr/bin/ruby -w
# Michael Schuerig, <[email protected]>, 2012.
# This file is in the public domain.
# Any trouble it may cause is yours, not mine.
#
# This is a pre-commit hook for git with a few checks I find useful
# useful in Rails projects. The checks are:
#
# * Bad tags (such as XXX, WTF, ...) as specified in
@mschuerig
mschuerig / webshot.js
Created August 26, 2012 15:58
Screenshot script for webpages
#! /usr/bin/env phantomjs
// Michael Schuerig, <[email protected]>, 2012.
// Portions are Copyright 2011 Valeriu Paloş ([email protected]). (see below)
// This script requires PhantomJS
// http://phantomjs.org/download.html
var jQueryUrl = "//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js";
@mschuerig
mschuerig / copytags.rb
Last active December 15, 2015 20:58
Copy tags from OGG or MP3 files to FLAC files. Matching files are found by patterns containing their track number. Optionally rename files.
#! /usr/bin/ruby1.9.3 -w
require 'optparse'
require 'taglib'
FIELDS = %w( ALBUM ARTIST CDDB DATE GENRE DISCNUMBER TITLE TRACKNUMBER )
$dry_run = false
$tag = true
$rename = false
@mschuerig
mschuerig / yearly.rb
Created April 20, 2013 08:40
Generate yearly and year-monthly playlists based on when albums were added to the collection. The heuristic used to find the adding date is to take the date of the oldest file in the album's directory. No CLI yet, use at your own risk.
#! /usr/bin/ruby1.9.3
require 'taglib'
require 'debugger'
# TODO
# - CLI
# - -m, --monthly
# - -u, --update
@mschuerig
mschuerig / git-amnesia.rb
Created May 11, 2013 20:25
In a directory containing multiple git repositories, run git log on each of them. git amnesia --author=me --since=2012-03-01
#! /usr/bin/ruby -w
require 'date'
require 'find'
# TODO
# - make work with -p option
def main
logopts = "--color=always --since=#{Date.today - 3} #{ARGV.join(' ')}"
@mschuerig
mschuerig / gist:7577756
Created November 21, 2013 08:13
within_any. Helper method for capybara. For selectors matching multiple elements, expect the block not to raise an error for one of them.
def within_any(*args)
candidates = page.all(*args)
if candidates.empty?
query = Capybara::Query.new(*args)
raise Capybara::ElementNotFound.new("Unable to find #{query.description}")
end
error = nil
candidates.any? do
begin
@mschuerig
mschuerig / gist:7891143
Created December 10, 2013 14:10
Mongoid: Raise an exception and log errors if the identity map contains changed but unsaved objects after a request was processed.
unless Rails.env.production?
after_filter do
objects = Mongoid::IdentityMap.values.flat_map(&:values)
changed_objects = objects.select { |o| o.changes.except('updated_at', 'locked_at', 'locked_until').present? }
unsaved_objects = changed_objects.select { |o| o.errors.empty? }
unless unsaved_objects.empty?
unsaved_objects.each do |obj|
Rails.logger.error "Unsaved object: #{obj.inspect}\n Changes: #{obj.changes.inspect}"
end
raise "Some changed objects have not been saved. See log for details."
@mschuerig
mschuerig / hash_bench.rb
Created December 11, 2013 00:35
Measure the times it takes to access (hit and miss) elements in hashes with up to 2^21 elements.
require 'benchmark'
require 'ascii_charts'
def measure
GC.disable
Benchmark.realtime do
yield
end
ensure
GC.enable
@mschuerig
mschuerig / snapper completion
Last active August 29, 2015 13:56
snapper bash completion
__snappercomp() {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $( compgen -W "$1" -- "$cur" ) )
}
# From http://github.com/jweslley/rails_completion
# @param $1 Name of variable to return result to
# @param $2 Command list
__snappercmd() {
any_command=$(echo $2 | sed -e 's/[[:space:]]/|/g')
@mschuerig
mschuerig / mpd-announce
Last active August 29, 2015 14:02
Announce (speak) the song currently playing in MPD
#! /bin/sh -e
# Requires https://github.com/profh/romanic
# gem install romanic
device="default:CARD=X20"
lock="/run/lock/mpd-announce"
dotlockfile -r 0 -p "$lock" || exit 0
trap "dotlockfile -p -u \"$lock\"" EXIT TERM INT