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
module Dry
module Types
class Hash < Definition
class Safe < Hash
def call(hash, meth = :call)
member_types.each_with_object({}) do |(key, type), result|
if hash.key?(key)
result[key] = type.__send__(meth, hash[key])
elsif type.is_a?(Default)
result[key] = type.evaluate
def resolve_missing_value(result, key, type)
if type.is_a?(Default)
result[key] = type[type.evaluate]
else
result[key] = type[nil]
end
end
module Dry
module Monad
class Either
attr_reader :right, :left
def ==(other)
other.is_a?(Either) && right == other.right && left == other.left
end
class Right < Either
module Dry
module Monad
class Maybe
def self.lift(value)
if value.nil?
None.instance
else
Some.new(value)
end
end
class Users < ROM::Relation[:sql]
schema(:users) do
attribute :id, Types::Serial
attribute :name, Types::String
associate do
many :posts
many :labels, through: :posts
end
end
Class.new(ROM::Repository[relation]) do
defines :mapping
commands :create, update: :by_id, delete: :by_id, mapper: mapping
self.mapping mapping
def find(id)
collection.by_id(id).one
end
def all
module Imprint
class Container
extend Dry::Container::Mixin
class << self
attr_reader :root
def register_dir(dir, eager: false, instantiate: true)
Dir["#{root}/#{dir}/**/*.rb"].map do |path|
name = path.sub("#{root}/", '').sub('.rb', '')
module Imprint
Import = Dry::AutoInject(Container)
KwargsImport = Import.kwargs
class << self
def inject(target)
-> *values { target.send(:include, KwargsImport[*values]) }
end
def args_inject(target)
module CommandPlugins
module ActiveRecordTimestamps
class HashWithTimestamps
def initialize(input)
@input = input
end
def [](*args)
tuple = @input[*args]
class Service
class TransactionWrapper
TransactionFailed = Class.new(StandardError)
attr_reader :sequence
delegate :append, :prepend, to: :sequence
def initialize(sequence, connection)
@sequence = sequence
@connection = connection