Skip to content

Instantly share code, notes, and snippets.

🐼
fuzzy logic

Josh Goodall inopinatus

🐼
fuzzy logic
Block or report user

Report or block inopinatus

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@inopinatus
inopinatus / bundlemark.sh
Last active Jul 17, 2019
crude little benchmark for parallelizing bundle-installed gems
View bundlemark.sh
#!/bin/bash
TMPDIR="$(mktemp -d)"
CORES=$(sysctl -n hw.ncpu 2>/dev/null || nproc)
echo "==> testing with $CORES cores in $TMPDIR"
cd $TMPDIR
RUBY_VERSION=$(rbenv version-name)
TIME=$(which time)
@inopinatus
inopinatus / taskable.rb
Last active Jul 13, 2019
Delegated relational composition for OR'ing polymorphic ActiveRecord associations
View taskable.rb
module Taskable
extend ActiveSupport::Concern
included do
has_many :tasks, as: :taskable
end
def tasks
task_relations.inject(super) do |this, rel|
this.or(rel.extending(AllTasks).all_tasks)
end
@inopinatus
inopinatus / remap_reviewer_ids.rb
Created Jul 1, 2019
example use of CTEs in object-rewriting migration
View remap_reviewer_ids.rb
def remap_reviewer_ids(table)
connection.execute(<<~EOQ)
WITH reviewer_map AS (
SELECT reviewers.base_id, to_jsonb(array_agg(DISTINCT profiles.account_id))
FROM profiles
JOIN (SELECT jsonb_array_elements(options->'reviewer_ids')::text::int as reviewer_id, id AS base_id FROM #{table}) AS reviewers
ON reviewers.reviewer_id = profiles.id
GROUP BY base_id
)
UPDATE #{table}
@inopinatus
inopinatus / AllowDescribeNetworkInterfacesAndAssignIpv6Address
Last active Jun 28, 2019
OpWorks Stacks managed instance self-update to have IPv6 address.
View AllowDescribeNetworkInterfacesAndAssignIpv6Address
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:DescribeNetworkInterfaces",
"ec2:AssignIpv6Addresses"
],
@inopinatus
inopinatus / digest_queries.rb
Created Apr 25, 2019
SHA digest queries for ActiveRecord + postgresql
View digest_queries.rb
# frozen_string_literal: true
module DigestQueries
def predicate_builder
@predicate_builder ||= super.tap do |pb|
pb.register_handler(Digest::Instance, DigestHandler.new(pb))
end
end
class DigestHandler
@inopinatus
inopinatus / table_to_csv.rb
Last active Jun 28, 2019 — forked from sandys/table_to_csv.rb
convert a html table to CSV using ruby
View table_to_csv.rb
require 'rubygems'
require 'nokogiri'
require 'csv'
f = File.open("table.html")
doc = Nokogiri::HTML(f)
CSV.open("output.csv", 'w') do |csv|
doc.xpath('//table/tbody/tr').each do |row|
tarray = []
View verify_and_decrypt_session_cookie52.rb
require 'cgi'
require 'active_support'
def verify_and_decrypt_session_cookie(cookie, secret_key_base = Rails.application.secret_key_base)
cookie = CGI::unescape(cookie)
salt = 'authenticated encrypted cookie'
encrypted_cookie_cipher = 'aes-256-gcm'
serializer = ActiveSupport::MessageEncryptor::NullSerializer
key_generator = ActiveSupport::KeyGenerator.new(secret_key_base, iterations: 1000)
@inopinatus
inopinatus / simpsonian.rb
Last active Jun 28, 2019
ruby's little-known array parameter destructuring.
View simpsonian.rb
def list((head, *tail))
case tail.length
when 0
head&.fetch(:name).to_s
when 1
"#{list [head]} & #{list tail}"
else
"#{list [head]}, #{list tail}"
end
end
@inopinatus
inopinatus / invoice.rb
Last active Jun 25, 2018
json models embedded in your activerecord models
View invoice.rb
class Invoice < ApplicationRecord
include JsonModel::Attribute
json_attribute_model :payment, Payment
validates_associated :payment
end
@inopinatus
inopinatus / hash_proc_lookup.rb
Created Jun 21, 2018
Cheap lookups with Hash#to_proc
View hash_proc_lookup.rb
[:foo, :bar].map(&{ bar: "BAZ", foo: "QUUX" })
You can’t perform that action at this time.