Skip to content

Instantly share code, notes, and snippets.

@estum
estum / sums_prefix.rb
Created April 20, 2022 16:26
Mixin with namespaced macro to inherit view prefixes from several modules.
# Mixin with namespaced macro to inherit view prefixes from several modules.
#
# @example Imagine legacy controllers…
# module V1
# class BaseController < ActionController::Metal; end
# # => _prefixes: ['api/v1/base']
#
# class PostsController < BaseController; end
# # => _prefixes: ['api/v1/base', 'api/v1/posts']
# end
@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))
@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 / 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 / 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 / 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