Skip to content

Instantly share code, notes, and snippets.

@estum
estum / app-env
Last active February 13, 2024 14:33
Simple dotenv wrapper on Bash (replace dotenv gem).
#!/usr/bin/env bash
#
#/ Usage: app-env [[-e <DOTENV_FILE>]... ] [[-s <SOURCE_FILE>]... ] [--] <command> [<args>...]
#/ app-env [-h|--help]
#/
#/ Executes a given command with environment variables loaded from dotenv files.
#/ Dotenv files are executed in current shell context, wrapped with `set -e'.
#/ Source files, which are set with the `-s' flag will be included after `set +e'
#/ It is possible to provide several files, just by using flags as many tymes as you need.
#/
@estum
estum / README.rdoc
Last active August 23, 2023 13:47
iTerm: Coprocess-script to auto-paste sudo password from keychain

iTerm Coprocess-script to auto-paste sudo password from keychain

Usage

  1. Open Keychain Access.app and create new object using the host as a name (“example.com”), username with sudo rights (“user”) and it’s password.

  2. Add a trigger for iTerm’s profile (‘Advanced‘ tab):

    • Regular Expression: \$ sudo

    • Action: Run Coprocess…

    • Parameters: /path/to/iterm_reply_with_keychain.rb user@example.com

@estum
estum / switch_on.rb
Last active August 8, 2023 05:52
Ruby alternative switch-case syntax.
# = Kernel#switch
# Provides alternative switch-case syntax.
#
# # support methods:
# value = []
# switch value do
# on empty?: -> { "none" }
# on one?: -> { "one" }
# on many?: -> { "many" }
# end
@estum
estum / atomic.rb
Last active May 30, 2023 05:52
Dry::Types::Atomic
# frozen_string_literal: true
require 'concurrent/atomic/atomic_reference'
module Dry
module Types
# Atomic types denote references in advance to target ones due to help achieve a cross-reference.
#
# @example
# module T
@estum
estum / rw.rb
Last active May 3, 2023 02:15
RW.rb - inverted-control accessor tool
# frozen_string_literal: true
require 'dry/core/cache'
require 'dry/core/constants'
require 'dry/container'
require 'dry/types'
require 'dry/types/tuple' # → https://github.com/estum/dry-types-tuple
# External instance-level accessor wrapper in inverted control:
# once defined with an accessor name it could be used with different objects
@estum
estum / dry-types-tuple.rb
Last active January 26, 2023 09:40
Dry::Types::Tuple #dry-rb #dry-types
# frozen_string_literal: true
module Dry
module Types
# @example
# Types::ServiceArgs = Types.Tuple(
# Types::Params::Symbol, # --- positional types
# [Types::Params::Integer | Types::Coercible::String] # --- [type for the rest items]
# )
# Types::ServiceArgs[['thumb', '300', '300', 'sample']]
@estum
estum / mat_module_eval.rb
Last active December 15, 2022 12:12
Materialize macro-defined method sources for documentation (ruby)
# The refinement module helps to inspect a composed source of meta-generated methods,
# which were defined via Module#module_eval or Module#class_eval methods
# in strings with interpolations.
#
# Unless this refinement is using, looking up for an actual source of such
# kind of method will result with a raw string literal with no interpolation applied.
#
# It's monkey patch the Module#module_eval and Module#class_eval methods to
# write a source with applied interpolation into a temporary file per method owner and
# substitutes an evaluted located path & lineno for the string.
@estum
estum / migrate_hstore_to_json.rb
Last active October 11, 2022 12:29
Ruby on Rails & Postgres: Reversible migrate hstore column to jsonb with contents
class MigrateHstoreToJson < ActiveRecord::Migration
def up
rename_column :posts, :data, :data_hstore
add_column :posts, :data, :jsonb, default: {}, null: false, index: { using: 'gin' }
execute 'UPDATE "posts" SET "data" = json_object(hstore_to_matrix("data_hstore"))::jsonb'
remove_column :posts, :data_hstore
end
def down
rename_column :posts, :data, :data_jsonb
@estum
estum / osascript.rb
Created September 22, 2022 22:52
Ruby Osascript runner
# @example Simple
# Osascript.new(<<~SCPT.freeze).()
# activate application "Finder"
# SCPT
#
# @example JSC with args
# # The script takes 2 arguments: directory path & image path
# # to set a folder icon to the given directory.
# script = Osascript.new(<<-JS.freeze, lang: 'JavaScript')
# ObjC.import("Cocoa");
@estum
estum / comparator.rb
Created September 1, 2022 19:44
Dry::Core::Comparator
# frozen_string_literal: true
require 'dry/core/equalizer'
module Dry
# @api public
def self.Comparator(*keys, **options)
if keys.size == 0 && options.size > 0
keys << options.dup
options.replace(keys[0].extract!(:immutable, :inspect, :compare))