Skip to content

Instantly share code, notes, and snippets.

View searls's full-sized avatar
💚

Justin Searls searls

💚
View GitHub Profile
@searls
searls / executes_command.rb
Created April 3, 2022 12:57
Silly little class for when you want the combined stdout/stderr output of a command and its success status
require "open3"
class ExecutesCommand
Result = Struct.new(:success, :output, keyword_init: true)
def call(command)
stdin, stdout_and_stderr, wait_thr = Open3.popen2e(command)
result = Result.new(
success: wait_thr.value == 0,
output: stdout_and_stderr.read
)
@searls
searls / whoops.rb
Created January 11, 2022 15:24
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!
class CountsInventory
def count_inventory(store)
Inventory.find_or_initialize_by(store: store).tap do |inventory|
inventory.assign_attributes(date: Time.zone.now.to_date)
inventory.items = count_items(inventory) # Updates which items are associated, does not cascade save to them
inventory.save!
end
end
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()
 request.open('GET', url, true)
@searls
searls / upsert_all_or_else_delete.rb
Created December 24, 2021 15:05
This is an attempt to efficiently upsert/overwrite a large chunk of records' (items) associated rows (terms).
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 = @expands_parentheticals.call(item.meaning_texts).map { |s|
@massages_english.call(s)
}.uniq
@searls
searls / hmm.rb
Last active October 4, 2021 12:27
method_missing will catch private methods. Makes sense, but TIL
class Hmm
def method_missing(name)
"missing #{send(name)}"
end
def respond_to_missing?(name, include_all = false)
true
end
def beef?
class Wtf
def method_missing(name, *args)
:lol
end
def respond_to?(name)
:nope
end
def respond_to_missing?(name)
require "bundler/inline"
gemfile do
source 'https://rubygems.org'
gem "rspec", "~> 3.10"
end
require "rspec/autorun"
RSpec.configure do |config|
@searls
searls / whereable.rb
Created September 4, 2021 16:06
The initial implementation of a Whereable query filter for KameSame.
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
end
def valid?
@searls
searls / searches_stuff.rb
Last active September 1, 2021 14:24
Whereable Query Pattern. A design pattern for combining numerous types of queries only if the query is relevant into a single query using ARel
# I heard Whereables were having a moment.
#
# More on the real version of this feature here:
# https://community.wanikani.com/t/kamesame-a-fast-feature-rich-japanese-memorization-webapp/31319/1575?u=searls
#
# Below is simplified but basic usage:
#
# SearchesStuff.new.call("arigatou") # finds ありがとう
# SearchesStuff.new.call("にほんご") # finds 日本語
# SearchesStuff.new.call("探して") # finds 探す
@searls
searls / 1-without-lifecycle-hooks.txt
Created July 30, 2021 15:41
Speed of test_data with and without using the lifecycle hooks introduced in 0.2.1
$ time bin/rails test
Run options: --seed 6029
# Running:
.............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
Finished in 22.966130s, 20.7697 runs/s, 19.7682 assertions/s.
477 runs, 454 assertions, 0 failures, 0 errors, 0 skips
Coverage report generated for Minitest to /Users/justin/code/testdouble/present/coverage. 2793 / 3633 LOC (76.88%) covered.