Skip to content

Instantly share code, notes, and snippets.

Avatar
💚

Justin Searls searls

💚
View GitHub Profile
View runs_sales_report_test.rb
require "date"
require "mocktail"
require "minitest/autorun"
class FetchesOrdersAndItems
Result = Struct.new(:orders, :items, keyword_init: true)
def fetch(start_date, end_date)
end
end
@searls
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 = 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 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.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)
View web_audio_mix_ios.md

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 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 = @expands_parentheticals.call(item.meaning_texts).map { |s|
@massages_english.call(s)
}.uniq
@searls
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)}"
end
def respond_to_missing?(name, include_all = false)
true
end
def beef?
View wtf.rb
class Wtf
def method_missing(name, *args)
:lol
end
def respond_to?(name)
:nope
end
def respond_to_missing?(name)
View rspec_suite_example.rb
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 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
end
def valid?
@searls
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:
# 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 探す