Skip to content

Instantly share code, notes, and snippets.

View wils3005's full-sized avatar

Jack Wilson wils3005

View GitHub Profile
@wils3005
wils3005 / env.rb
Last active January 26, 2020 17:51
# frozen_string_literal: true
env =
File.
read('.env').
split("\n").
reject { |str| str =~ /^\s*#/ }.
map { |str| str.split('=') }.
to_h
# Called by wrapping arbitrary code-to-be-logged in a block. Its return value
# will be the return value of whatever's in the block. e.g.
# require '/Users/jack/lib/trace_logger'
# def my_method
# ...
# foo = some_buggy_method
# ...
# end
Pry.config.commands.command 'trace_point', 'Enables trace point logging' do
next unless defined? Rails
$logger = Logger.new(Rails.root.join('log', 'trace_point.log'))
$trace_point =
TracePoint.trace(:call) do |it|
next unless it.path[Rails.root.to_s]
json = {
@wils3005
wils3005 / async.rb
Last active December 29, 2019 03:39
class Async
def initialize
self.read_io, self.write_io = IO.pipe
raise(LocalJumpError, 'no block given (yield)') unless block_given?
self.pid =
Process.fork do
read_io.close
Marshal.dump(yield, write_io)
write_io.close
module FamilyTree
def family_tree
{ name => (subclasses.sort_by(&:name).map(&:family_tree).reduce(&:merge) || {}) }
end
end
ActionController::Base.extend(FamilyTree)
Rails.application.eager_load!
module Analyze
def analyze
connection.execute("EXPLAIN ANALYZE #{to_sql}").values.flatten
end
end
ActiveRecord::Relation.include(Analyze)
# frozen_string_literal: true
require 'pstore'
module Transporter
BUCKET_NAME = 'REPLACE ME'
def self.transport!(object_name)
s3_object_key = File.join('tmp', "#{object_name}.pstore")
pathname = Rails.root.join(s3_object_key)
@wils3005
wils3005 / raise.rb
Last active December 29, 2019 03:57
# frozen_string_literal: true
module Raise
def raise(*args)
error_class = args.find { |it| it.is_a?(Class) }
message =
args
.find { |it| it.is_a?(String) }
.to_s
# frozen_string_literal: true
################################################################################
# class MyClass
# include Attributable
# attr(:foo) { SecureRandom.uuid }
# attr(:bar) { Time.current }
# attr(:baz) { MyClass.new }
# end
# frozen_string_literal: true
Rails.application.eager_load!
class MyFind
LIMIT = 1000.0
class << self
def call(*args)
new(*args).call