Skip to content

Instantly share code, notes, and snippets.

View jqr's full-sized avatar

Elijah Miller jqr

View GitHub Profile
@jqr
jqr / cte_badged_count.rb
Created March 4, 2024 15:13
CTE Limited Badged Counters
GoodJob::Job.retried.badged_count
module CteLimited
extend ActiveSupport::Concern
included do
scope :cte_limited, -> (limit = 10_000) { with(table_name => limit(limit)) }
scope :badged_count, -> (limit = 999) { (limit ? cte_limited(limit + 1) : all).count.badgify(limit:) }
end
end

Cursoring Constraints

Exploring cursoring constraints with multiple field ordering. While Ruby's Array#<=> operator does this internally, this was about seeing the internal constraints to port them to various database systems.

Makes all combinations of a=1..3 b=1..3 c=1..3 in a pre-sorted order. Array slicing operations are then used to validate the instance methods results against this ground truth.

@jqr
jqr / iron_lung.rb
Last active December 13, 2023 02:31
Ruby code to help navigate the game of Iron Lung
# Ruby code to help navigate the game of Iron Lung.
class Numeric
def sign
value = (self / self.abs)
if value.nan?
1
else
value
end
class Current < ActiveSupport::CurrentAttributes
attribute :interactive_timeout_remaining
end
class ApplicationController < ActionController::Base
before_action :set_interactive_timeout_remaining
def set_interactive_timeout_remaining
Current.interactive_timeout_remaining = 5.seconds
end
@jqr
jqr / cleanup.rb
Created October 24, 2022 16:45
Clean incidental code directories
#!/usr/bin/env ruby
# Usage: cleanup <dir> [dir] ...
# Summary: Removes incidental files in code dirs
# Help: Recursively removes log files and does git garbage collection
# for the given directory.
dirs = ARGV
if !dirs.first
puts "ERROR: must specify directory"
system "help cleanup"
class SomeModel < ApplicationRecord
serialize :client_secret, EncryptedStringSerializer.new
serialize :credentials, EncryptedJsonSerializer.new
end
@jqr
jqr / linear_contention.rb
Created February 21, 2018 17:11
Easy to reason about linear overlapped execution of multiple threads to force contention for tests.
require "thread"
# Easy to reason about linear overlapped execution of multiple threads to
# force contention for tests.
#
# -------- Time ------->
# Thread.current 1 ---------------------------------- 4
# t2 2 ------- 3
m = Mutex.new
@jqr
jqr / namespaces_and_nesting.rb
Created February 20, 2018 15:46
Namespaces and Nesting
# Show an issue with reopened namespaces having lookups done using nesting
# instead of namespaces. Then show a simple work-around.
# Define a constant in a module with namespaced classes.
module Something
CONSTANT = true
end
# Reopen Something add add a class using nesting.
module Something
@jqr
jqr / test.md
Created September 1, 2016 17:45
test
summary deets!