Skip to content

Instantly share code, notes, and snippets.

@estum
estum / yieldable.rb
Created November 2, 2020 12:06
Yieldable v2
# frozen_string_literal: true
# = Yieldable
#
# This is meta-mixin, which makes your module, class or it's instance to respond
# to method +to_proc+, for example, to easily build instances during iteration:
#
# ["1", "2", "3"].map(&SomeObject)
#
# It is 2 different ways to use this module: +extend+ or +prepend+ it to your class.
@estum
estum / json_wildcard.rb
Created October 23, 2020 10:41
JSONWildcard: A tiny singleton to generate JSON templates.
# frozen_string_literal: true
# A tiny singleton to generate JSON templates.
#
# === Example:
#
# template = JSONWildcard["example" => Array.new(5, "%s")]
# # => {"example":[%1$s,%2$s,%3$s,%4$s,%5$s]}
#
# JSONWildcard.format(template, 1, 2, 3, 4, 5)
@estum
estum / insert_statement_generator.rb
Created October 23, 2020 10:23
ActiveRecord's Insert Statement Generator
# frozen_string_literal: true
require "dry-initializer"
# Simply generates insert statement with optional overridings.
# === Usage:
#
# like = Like.last
# # => #<Like:0x00007f8a42b07858> {
# # :id => "f93c47dc-9e09-4bf4-bbcb-2ec2000a873f",
@estum
estum / predicate_builder_array_fix.rb
Created October 10, 2020 13:09
Fix the array handling on ActiveRecord's prepared statements
# frozen_string_literal: true
# Before (creates prepared statement for each varying size of given array):
#
# $ Post.where(id: [uuid1, uuid2])
# => Post Load (0.6ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" IN ($1, $2) [["id", "a830f21d-a27b-4bde-8c05-6e5dd088712e"], ["id","531ee026-d60d-4a59-a9a5-d44c44578a98}"]]
#
# After (the only one statement for varying size of given array):
#
# $ Post.where(id: [uuid1, uuid2])
@estum
estum / multikey_map.rb
Last active December 3, 2021 08:45
Multikey Map
# frozen_string_literal: true
class MultikeyMap
include Enumerable
# Creates multikey map from a dict { value => alias | [aliases...] },
# i.e. both key and values will be mapped to key.
# @param dict [Hash]
# @raise [TypeError] if input can't be coerced as hash
# @return [MultikeyMap]
### Keybase proof
I hereby claim:
* I am estum on github.
* I am estum (https://keybase.io/estum) on keybase.
* I have a public key ASCBUSe8TPNeG3_gFmmy3hMi6Azmxilwu-gM4ftWje5ruAo
To claim this, I am signing this object:
@estum
estum / lesmenu.rb
Last active November 11, 2020 04:32
Menu utility for LES (Live Enhancement Suite)
#!/usr/bin/env ruby
# frozen_string_literal: true
# = Lesmenu: Menu utility for Live Enhancement Suite (https://enhancementsuite.me)
#
# The utility creates a YAML-formatted copy of LES's `menuconfig.ini`,
# which is (IMHO) a better format for a human-readable and -editable structure.
#
# First, you should generate '~/.hammerspoon/menuconfig.yml':
#
@estum
estum / version_storage.rb
Created June 5, 2020 20:08
Shrine.rb plugin to separate storages per versions
# frozen_string_literal: true
class Shrine
protected :_store, :_delete
module Plugins
# Separate storages per versions
#
# Usage:
#
@estum
estum / recursive_shift_rows_with_modifier.sql
Last active June 5, 2020 20:13
PostgreSQL: recursively shift rows in order using modifier
-- +--------+-------+-------+
-- | init | mod | exp |
-- |--------+-------+-------|
-- | 6 | -1 | 5 |
-- | 5 | 0 | 4 |
-- | 4 | 1 | 6 |
-- | 3 | 0 | 3 |
-- | 2 | -1 | 1 |
-- | 1 | 0 | 2 |
-- +--------+-------+-------+
@estum
estum / joint.rb
Last active December 3, 2021 08:44
ActiveRecord's JOIN clause string wrapper class
# frozen_string_literal: true
class Joint < String
_patterns = [[/LATERAL$/, 'ON true'], [/^(?:NATURAL|CROSS)/, '']].deep_freeze!
_exception = "expected one non-blank argument of `on' or `using' unless type matches one of: #{_patterns.map(&:first).inspect}"
FALLBACK_BOOL_EXP = -> (type) do
case type
when _patterns[0][0]; _patterns[0][1]
when _patterns[1][0]; _patterns[1][1]
else raise ArgumentError, _exception