This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class IbanConverter | |
{ | |
function convert_dk($input) { | |
// DKkk BBBB CCCC CCCC CC | |
$iban = "DK00" . | |
str_pad($input['dk_bank_reg_number'], 4, "0", STR_PAD_LEFT) . | |
str_pad($input['dk_bank_account_number'], 10, "0", STR_PAD_LEFT); | |
$this->iban_replace_checksum($iban); |