Skip to content

Instantly share code, notes, and snippets.

View asaaki's full-sized avatar
🦀
I may be slow to respond. Ping me on Twitter instead: https://twitter.com/asaaki

Christoph Grabo asaaki

🦀
I may be slow to respond. Ping me on Twitter instead: https://twitter.com/asaaki
View GitHub Profile
@asaaki
asaaki / absolute.sh
Created August 28, 2015 14:58
Absolute final directory path of origin script
#!/bin/sh
# Shamelessly stolen from:
# https://github.com/michaeldfallen/git-radar/blob/master/git-radar#L7-L13
if [[ "$OSTYPE" == *darwin* ]]; then
READLINK_CMD='greadlink'
else
READLINK_CMD='readlink'
fi
@asaaki
asaaki / Rakefile
Last active August 29, 2015 13:55
Include .rabl templates files for `rake notes` task
module SourceAnnotationExtractorExtension
extend ActiveSupport::Concern
included do
def find_in_with_rabl dir
results = {}
Dir.glob("#{dir}/*") do |item|
next if File.basename(item)[0] == ?.
# abuse of `inject` and `tap`
#
# it's not really short
# but shows some nice Ruby stuff
%w[bigdecimal /util].inject(""){|a,e|(a<<e).tap{|r|require r}}
@asaaki
asaaki / env_loader.rb
Created April 11, 2014 09:11
ENV loader
require "yaml"
begin
application_yml = YAML.load_file(File.join(File.dirname(__FILE__), "../config/application.yml"))
global_envs = application_yml.select { |key, _| key =~ /([A-Z_])+/ }
environment = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
env_envs = application_yml.fetch(environment, {})
current_envs = global_envs.merge(env_envs)
current_envs.each_pair do |key, value|
ENV[key.to_s.upcase] ||= value.to_s
=begin
Message Pack vs similar utilities
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
Packing
user system total real
pack: bert 60.850000 0.270000 61.120000 ( 62.003839)
pack: bson 2.750000 0.010000 2.760000 ( 2.799844)
pack: bson (moped) 12.260000 0.030000 12.290000 ( 12.468642)
@asaaki
asaaki / pre-commit
Created June 12, 2014 08:53
.git/hooks/pre-commit - Abort on conflict markers
#!/bin/sh
# Conflict markers
git diff --cached --diff-filter=ACMR | awk '/\+(<<<[<]<<<|>>>[>]>>>|===[=]===$)/ { exit 1 }'
CODE=$?
if [ "$CODE" != "0" ]; then
echo "COMMIT ABORTED: Conflict markers found." >&2
exit $CODE
fi
@asaaki
asaaki / zone_conversion.rb
Last active August 29, 2015 14:02
Timezones: float offset to numerical string presentation of zone
module ZoneConversion
module_function
def float2zone(offset)
("%+06.2f" % offset).tr(".", ":")
end
end
### Example
@asaaki
asaaki / token.rb
Last active August 29, 2015 14:03
[Ruby] retry a block until a condition is met
module Token
module_function
MAX_RETRIES = 3
TokenTakenError = Class.new(StandardError)
def token_generator
# SecureRandom.hex(32)
("a".."z").to_a.sample
@asaaki
asaaki / proc_string.rb
Created July 7, 2014 17:05
ProcString
require "forwardable"
class ProcString
extend Forwardable
def_delegators :stringified_proc, :to_s, *("".methods - BasicObject.methods)
attr_reader :proc
def initialize(&string_proc)
@proc = string_proc
@asaaki
asaaki / stuff.rb
Created July 10, 2014 09:39
Stuff
sunday = (Date.today .. (Date.today + 7)).detect { |date| date.cwday == 7 }