Skip to content

Instantly share code, notes, and snippets.

@solnic
solnic / rom_mapper_and_anima.rb
Last active August 29, 2015 13:59
ROM mapper with anima model
require 'rom'
require 'anima'
class User
include Anima.new(:id, :name, :tasks)
end
class Task
include Anima.new(:title)
end
@solnic
solnic / axiom_sql_generator.rb
Created April 27, 2014 20:16
Progress on new axiom SQL generator
$LOAD_PATH.unshift './lib'
require 'axiom-sql-generator'
users = Axiom::Relation::Base.new(:users, [[:id, Integer], [:name, String]])
def compile_sql(relation)
sql_ast = Axiom::SQL::Processor.call(relation)
sql_str = SQL::Generator.generate(sql_ast)
Bundler could not find compatible versions for gem "rspec-core":
In Gemfile:
mutant-rspec (>= 0) ruby depends on
rspec-core (~> 2.14.1) ruby
rspec-rails (~> 3.0) ruby depends on
rspec-core (3.0.2)
@solnic
solnic / gist:2a2151b7b67e6d1f8447
Last active August 29, 2015 14:07
Sequel-powered ROM vs AR benchmark
# https://github.com/rom-rb/rom/blob/reboot/benchmarks/basic.rb
Calculating -------------------------------------
schema.users.to_a 20 i/100ms
mappers.users.to_a 17 i/100ms
ARUser.all.to_a 10 i/100ms
-------------------------------------------------
schema.users.to_a 210.7 (±9.5%) i/s - 1060 in 5.074291s
mappers.users.to_a 173.8 (±9.2%) i/s - 867 in 5.026702s
ARUser.all.to_a 102.3 (±5.9%) i/s - 510 in 5.004413s
# without unique column names this is what you need to do to make sure the result set has correct values and there
# are no name clashes
DB[:users].
join_table(:inner, :tasks, user_id: :users__id).
select(:tasks__id___task_id, :users__id___users_id)
# versus this, if we had unique column names by default
DB[:users].natural_join(:tasks).select(:task_id, :user_id)
@solnic
solnic / try_spec.rb
Created December 2, 2014 11:01
kleisli-inspired spike for handling inserts with associations in rom-rb
class Result
attr_reader :value, :error
class Success < Result
def initialize(value)
@value = value
end
def >(f)
f.call(value)
@solnic
solnic / virtus_money_ev.rb
Last active August 29, 2015 14:11
Virtus Money EV Example
require 'virtus'
class Money
include Virtus.value_object
values do
attribute :amount, Integer
attribute :currency, String
end
end
@solnic
solnic / rom-array-adapter.rb
Created December 13, 2014 00:23
Completely useless Array adapter for ROM because why not
class ArrayAdapter < ROM::Adapter
def self.schemes
[:array]
end
def initialize(uri)
super
@connection = {}
end
-- create_table(:users)
-> 0.0039s
LOADED 1000 users via ROM/Sequel
LOADED 1000 users via ActiveRecord
Calculating -------------------------------------
rom.read(:user_json).all.to_a
16 i/100ms
ARUser.all.map(&:as_json)
3 i/100ms
-------------------------------------------------
@solnic
solnic / gist:40edefb0c238f2ef0a1f
Created December 18, 2014 22:30
ROM running with sequel_pg ext VS AR 4.2.0.rc3
Calculating -------------------------------------
[ROM] Loading 1k user objects
29 i/100ms
[AR] Loading 1k user objects
9 i/100ms
-------------------------------------------------
[ROM] Loading 1k user objects
277.2 (±6.5%) i/s - 1392 in 5.040249s
[AR] Loading 1k user objects
111.0 (±9.0%) i/s - 558 in 5.071423s