Skip to content

Instantly share code, notes, and snippets.

View solnic's full-sized avatar

Peter Solnica solnic

View GitHub Profile
require 'equalizer'
module Checkproc
InvalidValueError = Class.new(StandardError) { include Equalizer.new(:message) }
class Composite
attr_reader :left
attr_reader :right
def initialize(left, right)
Calculating -------------------------------------
method 97.664k i/100ms
proc 85.151k i/100ms
-------------------------------------------------
method 2.611M (± 3.7%) i/s - 13.087M
proc 1.910M (± 3.4%) i/s - 9.537M
Comparison:
method: 2611065.1 i/s
proc: 1909549.3 i/s - 1.37x slower
require 'transproc'
include Transproc::Helper
def source_fn(source)
t(:add_keys, [:source]) >> t(:map_array, t(:map_value, :source, proc { source }))
end
fb_source = source_fn(:facebook)
tw_source = source_fn(:twitter)
require 'rom'
require 'transproc'
module Transformations
extend Transproc::Functions
def eval_keys(hash, param)
hash.each_with_object(Hash[hash]) do |(key, value), copy|
copy[key] =
case value
require 'rom'
require 'byebug'
rom = ROM.setup(:memory) do
relation(:users)
relation(:tasks)
relation(:tags)
commands(:users) do
define(:create) do
@solnic
solnic / manual_rom_env_setup.rb
Created May 7, 2015 13:49
Manual ROM env setup
require "rom/memory"
memory_repo = ROM::Memory::Repository.new
user_dataset = memory_repo.dataset(:users)
class Users < ROM::Relation[:memory]
end
class UserMapper < ROM::Mapper
model name: 'UserEntity' # ROM supports generating model classes
@solnic
solnic / data_proxy_example.rb
Created April 28, 2015 10:49
ROM - DataProxy + Options usage example
require 'rom'
require 'rom/support/options'
require 'rom/support/data_proxy'
class MyDataset
include ROM::Options
include ROM::DataProxy
option :connection, reader: true
@solnic
solnic / rom_custom_coercion_mapper.rb
Last active February 4, 2017 10:16
Custom coercer in ROM using transproc
require 'rom'
Batman = Struct.new(:secret_name)
Transproc.register(:to_batman, proc { |name| Batman.new(name) })
class GothamCityMapper < ROM::Mapper
attribute :superhero, type: :batman
end
@solnic
solnic / ar_create.rb
Created March 25, 2015 13:27
ROM/AR "create" operation profiling
require 'active_record'
require 'hotch'
ActiveRecord::Base.establish_connection('postgresql://localhost/rom')
class User < ActiveRecord::Base
end
User.delete_all
@solnic
solnic / comp_relations.rb
Last active August 29, 2015 14:15
[ROM] composable relations with partial application prototype
require 'rom'
require 'rspec'
module ROM
class Relation
class Composite
attr_reader :left, :right
def initialize(left, right)
@left = left