Skip to content

Instantly share code, notes, and snippets.

View tobiashm's full-sized avatar

Tobias H. Michaelsen tobiashm

View GitHub Profile
@tobiashm
tobiashm / enumerable-try_find.rb
Last active December 16, 2015 09:19
Enumerable#try_find - inspired by PrototypeJS Try.these
module Enumerable
def try_find(rescue_type = StandardError)
each do |element|
begin
candidate = element.respond_to?(:call) ? element.call : element
return candidate if candidate
rescue rescue_type
next
end
end
@tobiashm
tobiashm / log_tail.rb
Created January 15, 2013 21:12
Capistrano task to tail server log
namespace :log do
desc "tail log file"
task :tail, :roles => :app do
run "tail -n 200 -f #{shared_path}/log/#{stage}.log" do |channel, stream, data|
trap("INT") { puts ' Log tailing finished'; exit 0; }
puts data
break if stream == :err
end
end
end
@tobiashm
tobiashm / spree_i18n_compare.rake
Created December 12, 2012 16:32
Rake task to compare Spree locale file with 'baseline'
desc "compare spree en.yml with i18n da.yml"
task :compare do
require 'open-uri'
core = %w[api core dash promo].inject({}) do |memo, name|
memo.deep_merge YAML.load(open "https://raw.github.com/spree/spree/#{ENV['branch'] || 'master'}/#{name}/config/locales/en.yml")
end
other = YAML.load_file File.expand_path('../../../config/locales/da.yml', __FILE__)
en, da = core['en'], other['da']
puts ["=" * 60, "* checking en against da", "=" * 60].join("\n")
compare_yaml_hash en, da
@tobiashm
tobiashm / db-protect_environment.rake
Created October 15, 2012 13:18
rake db:protect_environment
namespace :db do
task :protect_evironment do
raise "Do not run db task in #{Rails.env} mode (or use env FORCE=true)" unless Rails.env.test? or ENV['FORCE']
end
task :load_config => :protect_evironment
end
@tobiashm
tobiashm / comics-title-text.js
Created September 9, 2012 09:48
Boomarklet to show title test on commics. Works for XKCD and Dinosaur Comics
javascript:(function(){var i=document.querySelectorAll('img.comic, #comic img')[0];i&&alert(i.title);})()
@tobiashm
tobiashm / sass-inline-color-image.rb
Created August 20, 2012 13:53
Generates a data-url for a PNG created from the given color.
require "chunky_png"
module Sass::Script::Functions
# Generates a data-url for a PNG created from the given color.
# Can be used to set a alpha-transparent background for IE8<
#
# @example
# background: url(inline-color-image(rgba(102, 54, 32, 0.5)));
def inline_color_image(color)
assert_type color, :Color
APP_PATH = File.expand_path('../..', __FILE__)
require File.join(APP_PATH, "lib/site")
working_directory APP_PATH
pid_path = File.join(APP_PATH, "tmp/pids/web.pid")
pid pid_path
stderr_path File.join(APP_PATH, "log/unicorn-stderr")
stdout_path File.join(APP_PATH, "log/unicorn-stdout")
@tobiashm
tobiashm / gist:2560372
Created April 30, 2012 17:41
Computer math WTF
Math.round(0.4999999999999999722444243843710865) == 1
Math.round(0.4999999999999999722444243843710864) == 0
@tobiashm
tobiashm / inline-color-image.rb
Created April 30, 2012 06:28
SASS inline-color-image
require "sass"
require "chunky_png"
module Sass::Script::Functions
# Generates a data-url for a PNG created from the given color.
# Can be used to set a alpha-transparent background for IE8<
#
# @example
# background: url(inline-color-image(rgba(102, 54, 32, 0.5)));
def inline_color_image(color)
module ModelSerializer
def self.serialize(object)
object.to_json include: nil # don't include any associations - only serialize shallow object
end
def self.deserialize(json)
hash = ActiveSupport::JSON.decode(json) # => { "section": { "id": 42, ... } }
model = hash.keys.first.camelize.constantize # => Section
attributes = hash.values.first
object = model.new