Skip to content

Instantly share code, notes, and snippets.

View solnic's full-sized avatar

Peter Solnica solnic

View GitHub Profile
require 'dry-validation'
SCHEMA = Dry::Validation.Schema do
configure do
config.input_processor = :sanitizer
def self.messages
Dry::Validation::Messages.default.merge(en: { errors: { unique?: "oops not unique" }})
end
@solnic
solnic / -
Created April 29, 2016 16:32
********************************************************************************
SEEDING 1000 users
SEEDING 3000 tasks
********************************************************************************
********************************************************************************
INSERTED 1000 users via ROM/Sequel
INSERTED 3000 tasks via ROM/Sequel
INSERTED 9000 tags via ROM/Sequel
********************************************************************************
class Users < ROM::Relation[:users]
schema do
attribute :id, Types::Serial
attribute :name, Types::String
associate do
many :posts
end
end
end
@solnic
solnic / rom-repo-crud.rb
Last active April 8, 2016 15:02
Upcoming rom-repository command support
require "rom-repository"
require "rom-sql"
config = ROM::Configuration.new(:sql, 'postgres://localhost/rom_repository')
class Users < ROM::Relation[:sql]
def by_id(id)
where(id: id)
end
end
require "rspec"
require "json"
require "dry-types"
require "dry-validation"
module Types
include Dry::Types.module
RegionCode = Types::Strict::String.constrained(
format: /\A(au|nz|uk|ie)\z/,
@solnic
solnic / v.rb
Created January 26, 2016 23:12
Upcoming dry-validation 0.7.0 macros
class UserSchema < Dry::Validation::Schema::Form
key(:password).maybe(min_size: 6).confirmation
key(:login).required(:bool?).when(:true?) do
key(:password).success?
end
end
schema = UserSchema.new
@solnic
solnic / 10k_multi_thread.rb
Last active January 12, 2016 16:19
Validating 10k hashes 1 thread vs 2 (mri, jruby, rbx results)
require 'dry-validation'
require 'active_model'
require 'benchmark'
require 'thread_safe'
schema = Class.new(Dry::Validation::Schema) do
key(:name, &:filled?)
key(:age) { |v| v.int? & v.gt?(18) }
end.new
@solnic
solnic / type_safe_entity_vs_ar.rb
Last active November 24, 2021 02:23
Fetching type-safe entity with rom repo vs AR
Calculating -------------------------------------
type-safe users 273.000 i/100ms
ar user models 257.000 i/100ms
-------------------------------------------------
type-safe users 2.813k (± 1.7%) i/s - 14.196k
ar user models 2.574k (±10.7%) i/s - 12.850k
Comparison:
type-safe users: 2812.7 i/s
ar user models: 2574.2 i/s - 1.09x slower
@solnic
solnic / fun.rb
Last active December 26, 2015 23:29
Ruby object that has "functions" rather than methods
class Function
attr_reader :name, :fn
class Composed
attr_reader :left, :right
def initialize(left, right)
@left = left
@right = right
end
@solnic
solnic / b.rb
Last active December 24, 2015 17:02
require 'benchmark/ips'
require 'compel'
require 'dry-validation'
require 'dry/validation/schema/form'
params= {
'first_name' => 'Joaquim',
'birth_date' => '1989-0',
'address' => {
'line_one' => 'Lisboa',