Skip to content

Instantly share code, notes, and snippets.

View david-pm's full-sized avatar
💭
I may be slow to respond.

David McDonald david-pm

💭
I may be slow to respond.
View GitHub Profile
@david-pm
david-pm / layout_bg_helper.css
Created September 10, 2019 01:40
Highlights your background to help when working with layouts
// stolen from: https://dev.to/gajus/my-favorite-css-hack-32g3
* { background-color: rgba(255,0,0,.2); }
* * { background-color: rgba(0,255,0,.2); }
* * * { background-color: rgba(0,0,255,.2); }
* * * * { background-color: rgba(255,0,255,.2); }
* * * * * { background-color: rgba(0,255,255,.2); }
* * * * * * { background-color: rgba(255,255,0,.2); }
* * * * * * * { background-color: rgba(255,0,0,.2); }
* * * * * * * * { background-color: rgba(0,255,0,.2); }
@david-pm
david-pm / redis.rb
Created August 9, 2019 02:47
disable flushdb and flushall from ruby's redis client
# inspired by this article: https://www.honeybadger.io/leveling-up/safeguarding-redis/
# and adapted from this gist: https://gist.github.com/joshuap/c1ff2657c150df6fb1257398b1d2716b
#
# * needed a way to disable flushall and flushdb in Production only
# * wanted to add a way to add some basic tests for sanity and explicit documentation
#
############### config/initializers/redis.rb #########################
require 'redis'
require 'redis_dangerous_commands'
@david-pm
david-pm / reset_push
Last active January 25, 2019 02:41
bash script that resets and force pushes
#!/bin/bash
# resets HEAD back one commit, recommits with provided message and force pushes to origin
commitmsg=$1
# stole function from: .oh-my-zsh/lib/git.zsh
function current_git_branch() {
local ref
ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null)
local ret=$?
@david-pm
david-pm / protection_proxy.rb
Last active December 29, 2018 03:51
Protection Proxy Pattern example that respects Interface Segregation
class BankAccount
attr_reader :balance
def initialize(starting_balance)
@balance = starting_balance
end
# if this were #irl we'd need to worry about race conditions here
def deposit(amount)
@balance += amount
end
@david-pm
david-pm / convert_to_snakecase
Created December 21, 2018 23:39
a sed script to convert camelcase and or pascalcase to snakecase
# sed -i -f convert_to_snakecase __yourfile__
#
# to run this within in vim:
# %s/[A-Z]/_\L&/g | %s/\<_//g
s/[A-Z]/_\L&/g; s/\b_//g
@david-pm
david-pm / pre-push
Created December 10, 2018 05:16
pre-push git hook that runs rubocop on latest commit
#!/usr/bin/env ruby
changed_files = `git diff-tree --no-commit-id --name-only --diff-filter=AM -r HEAD`.lines.map(&:chomp)
return if changed_files.empty?
results = `bundle exec rubocop --color #{changed_files.join(" ")}`
no_offenses_detected = results.include?("\e[32mno offenses\e[0m detected")
return if no_offenses_detected
@david-pm
david-pm / clear_logs.rb
Created December 9, 2018 22:10
clear dev logs on app boot
return unless Rails.env.development?
MAX_LOG_SIZE = 2.megabytes
logs = File.join(Rails.root, 'log', '*.log')
if Dir[logs].any? { |log| File.size?(log).to_i > MAX_LOG_SIZE }
$stdout.puts "---------- \033[32m clearing dev logs \033[0m --------------"
`rails log:clear`
end
@david-pm
david-pm / snippets.txt
Created December 7, 2018 20:59
chat app demo code snippets
> rails g controller home index
> rails g channel chat
-------
config/routes.rb:
-------
root 'home#index'
@david-pm
david-pm / wat2wasm.txt
Created June 5, 2018 02:32
A wat2wasm tutorial
INSTALL:
$ git clone --recursive https://github.com/WebAssembly/wabt
$ cd wabt
$ make clang-release
$ cd out/clang/Release
$ pwd | pbcopy (or just pwd and copy the output manually)
Add the pwd path to your shell config $PATH for ease of use.
@david-pm
david-pm / blockchain.rb
Created May 29, 2018 01:27
blockchain poc in ruby
require 'digest'
class Block
attr_reader :index, :parent, :created_at, :data, :hash
def initialize(index:, data:, parent: '')
@index = index.to_i
@data = data
@parent = parent.empty? ? '0' : parent
@created_at = Time.now.to_s