Skip to content

Instantly share code, notes, and snippets.

@kwando
kwando / ecto_schema.livemd
Created September 14, 2022 09:00
Ecto param schemas [livebook]

Ecto Schema

Mix.install([:ecto])

Section

defmodule ParamSchema do
@baweaver
baweaver / proc_ast_sql.rb
Created February 3, 2022 09:56
Proc to AST applied to potential foundation for SQL hackery
require 'proc_to_ast'
def where(&fn)
ast = fn.to_ast
source = fn.to_source
pp ast:, source:
end
where { age > 18 && occupation.downcase == 'plumber' }
@janko
janko / 0-gemfile.rb
Created July 31, 2016 11:24
Using Shrine with ROM and dry-rb
source "https://rubygems.org"
gem "shrine", github: "janko-m/shrine"
gem "rom-repository"
gem "rom-sql"
gem "sqlite3"
gem "dry-validation"
gem "roda"
gem "sucker_punch", "~> 2.0"

Inferring Schemas From Structs

This is a really quick and dirty example of how you can infer a form schema from a dry-type Struct or Value and how you might use it in a HTTP controller.

@davidpelaez
davidpelaez / custom_processor.rb
Created March 3, 2016 16:10
How to add custom steps to rom-mapper default Transproc processor. Useful to enhance the mapper with custom functionality and data transformations, e.g: lazy evaluation as presented in http://solnic.eu/2015/07/15/importing-data-with-rom-and-transproc.html
module Dataops
module Processors
class EnhancedAttributes < TransprocWithHook
BlockWrapper = Struct.new(:block) do
def call(*args)
self.block.call *args
end
end
command_builder = rom.command # no relation given, so get builder
nested_command = command_builder.create(user: :users) do |user|
user.create(:books)
end
# equivalent:
nested_command = rom.command({user: :users}, [:create, [:books, [:create]]])
# equivalent
@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
@gotar
gotar / combine.rb
Last active December 15, 2015 13:48
chat: {
id: 1,
owner_id: 1,
member_id: 2
}
user1: {
id: 1
name: 'Foo'
}
require 'dry-validation'
class Schema < Dry::Validation::Schema
group :node do
key(:name) { |name| name.str? and name.filled? }
key(:children) do |children|
children.array? do
children.each { |child| child.group?(:node) }