Skip to content

Instantly share code, notes, and snippets.

@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':
#
### 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 / 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]
@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 / 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 / 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 / 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 / model_coercer.rb
Created December 3, 2021 08:49
Dry::Initializer::ModelCoercer (instead of dry-initializer-rails)
# frozen_string_literal: true
module Dry
module Initializer
# @example
# class CreateOrder
# extend Dry::Initializer
#
# # Params and options
# param :customer, model: 'Customer' # use either a name
@estum
estum / map.rb
Created December 7, 2021 07:22
Map
# frozen_string_literal: true
require 'concurrent/map'
# {include:file:concurrent/map.rb}
class Map < Concurrent::Map
HashBuilder = proc do |hsh, default = nil, default_proc = nil|
hsh = ::Hash[hsh]
if !default.nil?
hsh.default = default
@estum
estum / expand_constant.rb
Created March 31, 2022 08:22
Module::ExpandConstant
# frozen_string_literal: true
require 'concurrent/map'
# = Expand Constant Namespace
#
# To expand namespace in classic way it have to be included or extended
# to a target module or class.
#
# This module provides +expand_constant+ method to expand external module namespace