Skip to content

Instantly share code, notes, and snippets.

View flash-gordon's full-sized avatar
🔪
Working on sharp tools

Nikita Shilnikov flash-gordon

🔪
Working on sharp tools
View GitHub Profile
@flash-gordon
flash-gordon / run_state.rb
Created February 9, 2019 09:31
This shows that implementation of state effects doesn't require mutation. This example uses recursion that can be optimized after enabling TCO
require 'fiber'
class RunState
def call(fiber, state = {}, *xs)
result = fiber.resume(*xs)
if fiber.alive?
op, *args = result
case op
when :read
require 'rack'
require 'puma'
require 'thread'
class Middleware
def initialize(app)
@app = app
@queue = Queue.new
@thread = Thread.new { run_loop }
end
module MyValidation
include Dry::Monads::Validated::Mixin
def Invalid(message)
super(message.to_s.split('_').map(&:capitalize).join(' '))
end
end
class CreateUser
include MyValidation
class Matcher
def self.[](pattern)
Class.new do
define_singleton_method(:===) { |exc| pattern === exc.message }
end
end
end
begin
1 / 0
class Foo
def self.===(obj)
true
end
end
begin
1 / 0
rescue Foo
puts 'Rescued!'
def search_tags(query, tags)
tag_queries = tags.map { |t| pattern(t) }
query.where { tag_queries.reduce(`false`) { |acc, q| acc | array_to_string(self.tags, ',').ilike(q) } }
end
class Templates < ROM::Relation[:sql]
schema do
attribute :possible_resolution_ids, Types::PG::Array('bigint')
associations do
has_many :resolutions,
as: :possible_resolutions,
relation: :resolutions,
view: :for_possible_resolutions_in_templates,
foreign_key: :template_id,
class Maybe
class Some < Maybe
end
class None
end
end
class Result
class Success < Result
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'dry-auto_inject'
gem 'dry-transaction'
gem 'dry-container', git: 'https://github.com/dry-rb/dry-container'
end
class Container
class User < Dry::Struct
MissingAttribute = Class.new(NameError)
def name
[first_name, last_name].join(' ')
end
attribute :name, Dry::Types['strict.string']
attribute :first_name, Dry::Types['strict.string']