Skip to content

Instantly share code, notes, and snippets.

View tobiashm's full-sized avatar

Tobias H. Michaelsen tobiashm

View GitHub Profile
@tobiashm
tobiashm / pre-commit
Created September 30, 2016 07:24
Git pre-commit hook
#!/bin/bash -l
# [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
[[ -f ".ruby-version" ]] && rvm use . > /dev/null
if [ -f ".rubocop.yml" ]; then
changed_ruby_files=$(git diff --cached --name-only --diff-filter=ACM | grep -e '\.rb$' | grep -v 'db/schema' | grep -v 'db/migrate')
if [ "$changed_ruby_files" != "" ]; then
bundle exec rubocop -n $changed_ruby_files || exit 1
fi
@tobiashm
tobiashm / transformer.rb
Created September 2, 2016 14:31
Partition dates exercise. Implement `Transformer#call`
# encoding: UTF-8
#
# Given dates `dx = [dX...] and dates `dy = [dYY...]`
# and d1 <= d11
#
# d1 d2 d3
# --------------------------------------------
# d11 d12 d13 d21 d22 d23 d31 d32 d33
#
# becomes
@tobiashm
tobiashm / docker-prune
Created August 25, 2016 13:09
Docker prune unused containers, images, and volumes
#!/bin/bash
docker rm -v $(docker ps -q --filter status=dead --filter status=exited)
docker rmi $(docker images -qf "dangling=true")
docker volume rm $(docker volume ls -qf "dangling=true")

Keybase proof

I hereby claim:

  • I am tobiashm on github.
  • I am tobiashm (https://keybase.io/tobiashm) on keybase.
  • I have a public key whose fingerprint is 5271 5CDF C372 D90C 2DA6 04CB 2F9D 28B8 93AF 52E7

To claim this, I am signing this object:

@tobiashm
tobiashm / tag_name.sh
Created July 13, 2016 14:30
Create a tag name from current directory
#!/bin/bash
tag_name="$(echo -e "${PWD##*/}" | tr -cd '[:alnum:]')"
echo $tag_name
@tobiashm
tobiashm / options_response.rb
Created March 1, 2016 13:08
Simple HTTP OPTIONS response middleware for Rails
module Rack
class OptionsResponse
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.options?
generate_response(request)
@tobiashm
tobiashm / dabblet.css
Created February 8, 2016 15:38
Untitled
div {
display: inline-block;
background-color: white;
transform: rotate(60deg);
transform-origin: left bottom;
}
body {
background-color: red;
margin: 0;
@tobiashm
tobiashm / render_error.rb
Created January 11, 2016 11:53
Render error for JSON API
def render_error(status, error = nil)
status = Rack::Utils.status_code(status)
body = {
error: status,
reason: error.is_a?(Exception) ? error.to_s : Rack::Utils::HTTP_STATUS_CODES[status]
}
body[:detail] = error.backtrace.join("\n") if error.is_a?(Exception)
render status: status, json: body
end
@tobiashm
tobiashm / i18n-date.rb
Last active December 7, 2015 09:53
I18n wrapper for Date.strptime
require "date"
require "i18n"
module I18n
module Date
module_function
def strptime(string, format = nil, options = nil)
format ||= :default
format = I18n.t(format, scope: "date.formats") if format.is_a?(Symbol)
@tobiashm
tobiashm / connection_handler.rb
Created December 2, 2015 08:58
Handle connection for ActiveRecord subclasses
require "active_record/base"
module ConnectionHandler
class << self
def establish_connections
connection_classes.each(&:establish_connection)
end
def disconnect_all!
connection_classes.map(&:connection).each(&:disconnect!)