Skip to content

Instantly share code, notes, and snippets.


Justin Searls searls

View GitHub Profile
View runs_sales_report_test.rb
require "date"
require "mocktail"
require "minitest/autorun"
class FetchesOrdersAndItems
Result =, :items, keyword_init: true)
def fetch(start_date, end_date)
searls / executes_command.rb
Created Apr 3, 2022
Silly little class for when you want the combined stdout/stderr output of a command and its success status
View executes_command.rb
require "open3"
class ExecutesCommand
Result =, :output, keyword_init: true)
def call(command)
stdin, stdout_and_stderr, wait_thr = Open3.popen2e(command)
result =
success: wait_thr.value == 0,
searls / whoops.rb
Created Jan 11, 2022
I mistakenly thought that re-assigning all entries on an association would cascade a save! on the owner of the relationship to the depended models if they were dirty. Turns out, nope!
View whoops.rb
class CountsInventory
def count_inventory(store)
Inventory.find_or_initialize_by(store: store).tap do |inventory|
inventory.items = count_items(inventory) # Updates which items are associated, does not cascade save to them!
def count_items(inventory)

I was having issues with getting my Web Audio effects in KameSame loud enough to be audible when an iOS device was simultaneously playing music in the background. Because I like to listen to music while I study, here's how I increased the volume of the sound effect relative to the music in iOS.

Here's the original function in my app for playing audio in iOS (all other platforms work fine with new window.Audio(url).play()):

function playIos (url) {
  const audioContext = new AudioContext()
  const source = audioContext.createBufferSource()
  const request = new window.XMLHttpRequest()'GET', url, true)
searls / upsert_all_or_else_delete.rb
Created Dec 24, 2021
This is an attempt to efficiently upsert/overwrite a large chunk of records' (items) associated rows (terms).
View upsert_all_or_else_delete.rb
Item.where("updated_at > ?", after).includes(:english_terms).find_in_batches(batch_size: 5000).with_index do |items, i|
good_term_attrs = []
bad_term_ids = []
items.each do |item|
# This just gathers all the potential valid english terms associated with a dictionary entry:
term_texts = { |s|
searls / hmm.rb
Last active Oct 4, 2021
method_missing will catch private methods. Makes sense, but TIL
View hmm.rb
class Hmm
def method_missing(name)
"missing #{send(name)}"
def respond_to_missing?(name, include_all = false)
def beef?
View wtf.rb
class Wtf
def method_missing(name, *args)
def respond_to?(name)
def respond_to_missing?(name)
View rspec_suite_example.rb
require "bundler/inline"
gemfile do
source ''
gem "rspec", "~> 3.10"
require "rspec/autorun"
RSpec.configure do |config|
searls / whereable.rb
Created Sep 4, 2021
The initial implementation of a Whereable query filter for KameSame.
View whereable.rb
class Whereable
def initialize(where:, model: Item, ranking_conditions: [], valid: true, data_source: nil)
@model = model
@where = where
@data_source = data_source
@ranking_conditions = ranking_conditions
@valid = valid
def valid?
searls / searches_stuff.rb
Last active Sep 1, 2021
Whereable Query Pattern. A design pattern for combining numerous types of queries only if the query is relevant into a single query using ARel
View searches_stuff.rb
# I heard Whereables were having a moment.
# More on the real version of this feature here:
# Below is simplified but basic usage:
#"arigatou") # finds ありがとう
#"にほんご") # finds 日本語
#"探して") # finds 探す