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
require "benchmark/ips"
def foo(seq, kw)
end
def fast
kw = { a: 1, b: 2, c: 3 }
foo(1, kw)
end
@flash-gordon
flash-gordon / rom_reading_aggregates.rb
Created July 4, 2017 16:42 — forked from Kukunin/rom_reading_aggregates.rb
Example of reading aggregates in rom-rb
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required.'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rom'
module Entities
class User < ROM::Struct
def name
[first_name, last_name].join(' ')
end
end
end
class Users < ROM::Relation[:sql]
schema do
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']
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 Maybe
class Some < Maybe
end
class None
end
end
class Result
class Success < Result
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,
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 Foo
def self.===(obj)
true
end
end
begin
1 / 0
rescue Foo
puts 'Rescued!'
class Matcher
def self.[](pattern)
Class.new do
define_singleton_method(:===) { |exc| pattern === exc.message }
end
end
end
begin
1 / 0