Skip to content

Instantly share code, notes, and snippets.

Avatar

Troels Knak-Nielsen troelskn

View GitHub Profile
@troelskn
troelskn / dk-tax.js
Last active Dec 19, 2019
Danish tax calculations for Google sheets
View dk-tax.js
function DK_TAX_PARSE_OPTIONS(opts) {
var options = {
hasChildren: true, // Do you receive "børnepenge"?
isFamily: true, // Are you married (and your spouse without income)?
interests: 44256, // Deductible interests paid (typically on a mortgage)
churchTax: 0.00867, // Set your church tax rate here, if you are a member of the church. Otherwise set to 0.0
municipalityTaxRate: 0.24926 // Find you tax rate here: https://www.skm.dk/skattetal/satser/kommuneskatter
};
var applyOption = function(key, value) {
var key = key.trim();
View table_print.rb
# takes an array of hashes and prints as a table on the console
def table_print(rows)
table = [rows.first.keys.map(&:to_s)] + rows.map(&:values).map { |v| v.map(&:to_s) }
sizes = table.map { |row| row.map(&:size) }.transpose.map(&:max)
puts table.map { |row| row.each_with_index.map { |txt, idx| txt.rjust(sizes[idx]) }.join("\t") }.join("\n")
end
@troelskn
troelskn / ruby_print.rb
Created Apr 26, 2019
Ruby pretty print with rubocop standard syntax
View ruby_print.rb
class RubyPrint
def self.pp(mixed)
puts new.transform(mixed)
end
def transform(mixed, indentation = 1)
if mixed.is_a?(Hash)
transform_hash mixed, indentation
elsif mixed.is_a?(Array)
View just_and_exactly.rb
# frozen_string_literal: true
require "active_record"
module JustAndExactly
module ActiveRecordExtensions
MultipleRecordsFound = Class.new(ActiveRecord::ActiveRecordError)
# Like +first!+, but will also fail if the query would match more than
# one record.
View docker-machine-auth
PS1='\[\e[1m\]\[\e[34m\]\u@\h$([ "$DOCKER_MACHINE_NAME" = "" ] || echo " \[\e[33m\][docker-machine:$DOCKER_MACHINE_NAME]\[\e[34m\]"): \[\e[35m\]\w\[\e[33m\]$(__git_ps1) \[\e[34m\]$\[\e[0m\] '
docker-machine-auth () {
MACHINE_NAME=$1
if [ "$MACHINE_NAME" = "" ]
then
eval "$(docker-machine env -u)"
else
eval "$(docker-machine env $MACHINE_NAME)"
fi
}
@troelskn
troelskn / db.rake
Last active Jan 28, 2019
Wait for db. Useful in a dockerized setup
View db.rake
namespace :db do
desc "Wait for db. Used for initial boot up"
task wait: :environment do
puts "*** Giving DB time to boot"
catch :done do
10.times do
Timeout.timeout(1) do
begin
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute("SELECT NOW()")
@troelskn
troelskn / shell_execute.rb
Created Oct 24, 2018
Running a shell subprocess is surprisingly hard in ruby
View shell_execute.rb
require "open3"
def execute_shell(command, verbose: false)
Rails.logger.info "[SHELL] #{command}"
# see: https://nickcharlton.net/posts/ruby-subprocesses-with-stdout-stderr-streams.html
# see: http://stackoverflow.com/a/1162850/83386
output = []
Open3.popen3(command) do |stdin, stdout, stderr, thread|
# read each stream from a new thread
@troelskn
troelskn / sprockets.rb
Created Nov 1, 2017
Use sprockets-es6 to handle jsx
View sprockets.rb
# config/initializers/sprockets.rb
module Sprockets
if respond_to?(:register_transformer)
register_mime_type 'text/jsx', extensions: ['.jsx'], charset: :unicode
register_transformer 'text/jsx', 'application/javascript', ::BabelTransformer
register_preprocessor 'text/jsx', DirectiveProcessor
end
if respond_to?(:register_engine)
args = ['.jsx', ::BabelTransformer]
@troelskn
troelskn / multipart.rb
Created Oct 27, 2017
Low level implementation of multipart post, using net/http
View multipart.rb
# Low level implementation of multipart post, using net/http
class MultipartPost
def initialize(uri:)
@uri = uri
@post_body = []
end
def send
request = Net::HTTP::Post.new(@uri.request_uri)
request.body = build_body
@troelskn
troelskn / Rakefile
Created Oct 6, 2017
Drop into an IRB session from within a rake task
View Rakefile
desc "Start an interactive console"
task :console do
require "irb"
require "irb/completion"
require "pp"
ARGV.clear
IRB.setup(nil)
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
irb = IRB::Irb.new
You can’t perform that action at this time.