Skip to content

Instantly share code, notes, and snippets.

View katafrakt's full-sized avatar
🐟

Paweł Świątkowski katafrakt

🐟
View GitHub Profile
defmodule IexHelpers do
def search_fn(name) do
:code.all_loaded()
|> Enum.filter(fn {mod, _} -> "#{mod}" =~ ~r{^[A-Z]} end)
|> Enum.map(fn {mod, _} -> mod end)
|> Enum.each(fn mod ->
mod.__info__(:functions)
|> Enum.each(fn {fun_name, arity} ->
if fun_name == name do
IO.puts "#{mod}.#{fun_name}/#{arity}"
require 'hanami/validations'
class ReservationValidation
include Hanami::Validations
predicate :before? do |other, current|
current < other
end
validations do
@katafrakt
katafrakt / dynamic.rb
Created November 26, 2019 09:22
dynamic dry-v contract
require 'dry/validation'
# fake dynamic fields
dynamic_fields = ->() { %i[email name] }
contract = Class.new(Dry::Validation::Contract) do
params do
dynamic_fields.().each do |f|
required(f).filled(:string)
end
@katafrakt
katafrakt / _test.rb
Created October 18, 2019 21:41
Shrine 3 entity problem
require 'shrine'
require 'shrine/storage/file_system'
require 'image_processing/mini_magick'
Dir.mktmpdir do |tmp|
Shrine.storages = {
cache: Shrine::Storage::FileSystem.new(tmp),
store: Shrine::Storage::FileSystem.new(tmp, prefix: 'uploads')
}
@katafrakt
katafrakt / flash_test.rb
Created January 25, 2019 08:42
Problem with flash in Hanami
require 'bundler/inline'
gemfile do
gem 'hanami', '~> 1.3'
end
require 'hanami/action/session'
module Home
class Index
@katafrakt
katafrakt / 01_kraftwerk-example.rb
Last active January 5, 2019 18:08
This is a showcase for a new API framework under development. Code is not yet publicly available.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'kraftwerk', path: 'kraftwerk'
end
DATABASE = []
module Measures
@katafrakt
katafrakt / benchmark.rb
Created July 5, 2018 16:16
Object creation benchmark
require 'benchmark/ips'
class Service
def call(a, b)
a + b
end
end
Benchmark.ips do |x|
x.report("creation") do
digraph finite_state_machine {
rankdir=LR;
size="8,5"
labelloc = "t";
label = "/a/";
"" [shape = none];
node [shape = doublecircle]; q1;
node [shape = circle];
"" -> q0;
q0 -> q1 [ label = "a" ];
046573a8640a2a13d7c1857678d5421ed619de1eb61ca8c48ee7ad379a0dfdc314882a0ab50207cf7643c7eaad6dba5292cbab90c1e4854b9eff60bbc56c4b7f62
@katafrakt
katafrakt / day_18.rb
Created December 18, 2017 21:12
Advent of Code 2017, day 18, referential Ruby implementation
class Program < Hash
Recv = Class.new(StandardError)
attr_reader :last_sound
def set(reg, val)
self[reg] = value(val)
end
def add(reg, val)
self[reg] += value(val)