Skip to content

Instantly share code, notes, and snippets.

View fractaledmind's full-sized avatar

Stephen Margheim fractaledmind

View GitHub Profile
@fractaledmind
fractaledmind / create_fts_posts.rb
Created January 24, 2025 19:32
Core of how to implement FTS search with SQLite
class CreateFtsPosts < ActiveRecord::Migration[8.0]
def change
create_virtual_table :post_documents, :fts5,
[:title, :body, "content=''", "contentless_delete=1", "tokenize='porter unicode61 remove_diacritics 2'"]
end
end
@fractaledmind
fractaledmind / sqlite-statement-diagrams.rb
Created January 2, 2025 23:18
Skeleton of a Ruby SQLite parser that renders the syntax diagrams for all of the statements in plain text
# ┌──────{ , }◀─────┐
# ◯─┴┬─▶[ sql-stmt ]─┬┴─▶◯
# └───────▶───────┘
def sql_stmt_list
end
# ◯─┬─────────────┬▶─────────────────────┬─┬─▶[ alter-table-stmt ]──────────▶─┬▶◯
# └─{ EXPLAIN }─┴─▶{ QUERY }─▶{ PLAN }─┘ ├─▶[ analyze-stmt ]──────────────▶─┤
# ├─▶[ attach-stmt ]───────────────▶─┤
# ├─▶[ begin-stmt ]────────────────▶─┤
@fractaledmind
fractaledmind / routing_test.rb
Created October 31, 2024 11:26
Test that ensures you have no controllers with public methods that do not have a corresponding route defined.
class RoutingTest < ActionDispatch::IntegrationTest
IGNORED_CONTROLLERS = Set[
"Rails::MailersController"
]
test "no unrouted actions (public controller methods)" do
actions_by_controller.each do |controller_path, actions|
controller_name = "#{controller_path.camelize}Controller"
next if IGNORED_CONTROLLERS.include?(controller_name)
require 'benchmark/ips'
require 'oj'
require 'json'
puts "Ruby version: #{RUBY_VERSION}"
puts "Oj version: #{Oj::VERSION}"
puts "JSON version: #{JSON::VERSION}"
# Sample data to hash - using varied data types
test_data = [1, "string", { a: 1, b: 2 }, [3, 4, 5]]
@fractaledmind
fractaledmind / active_job_duplicate_tagged_logs.rb
Created October 24, 2024 19:29
This is a reproducible script to demonstrate a bug in ActiveJob related to tagged logging
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", "8.0.0.rc1"
# If you want to test against edge Rails replace the previous line with this:
# gem "rails", github: "rails/rails", branch: "main"
@fractaledmind
fractaledmind / bench.rb
Created October 9, 2024 17:05
Ruby benchmark script to test which way is faster to merge two arrays: Array + Array or Array << Entry
require 'benchmark'
def array_plus_array(n)
result = []
n.times do |i|
result = result + [i]
end
result
end
@fractaledmind
fractaledmind / bench.rb
Created October 9, 2024 17:00
Ruby benchmark script to test which way is faster to normalize a hash: stringify_keys! or symbolize_keys!
require 'benchmark'
require 'active_support/core_ext/hash/keys'
def create_mixed_hash(size)
hash = {}
size.times do |i|
if i.even?
hash[i.to_s] = "value#{i}"
else
hash[i.to_s.to_sym] = "value#{i}"
@fractaledmind
fractaledmind / bench.rb
Created July 19, 2024 17:42
Explore the impact of polling for the SQLite write lock on performance under various contention scenarios. Run via `ruby meta.rb`, where meta.rb and bench.rb are in the same directory
require "sqlite3"
require "benchmark"
DB_NAME = "test.sqlite"
parent_connection = SQLite3::Database.new(DB_NAME)
parent_pid = Process.pid
mode = ARGV[0] || "constant"
process_count = ARGV[1] == "--processes" ? ARGV[2].to_i : 1
@fractaledmind
fractaledmind / app__controllers__concerns__oauth.rb
Last active May 27, 2024 15:34
Basic OAuth implementation for a Rails app
module OAuth
extend ActiveSupport::Concern
SIGN_UP = "sign_up"
SIGN_IN = "sign_in"
DESTINATION_PARAMS_KEY = :destination
DESTINATION_SESSION_KEY = "oauth.destination"
ORIGIN_PARAMS_KEY = :origin
ORIGIN_SESSION_KEY = "oauth.origin"
@fractaledmind
fractaledmind / seeds.rb
Created May 14, 2024 16:14
Example of simple file to centralize User management
users_file = Rails.root.join('storage', 'users.yml.erb')
if File.exist? users_file
users = YAML.load(ERB.new(File.read(users_file)).result)
User.insert_all(
users.map { |user| user.except("password") },
unique_by: :username
)
end