Last active
May 17, 2016 17:28
-
-
Save AMHOL/9c939e0d87695f26e11ffb670cc95db5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'rom', github: 'rom-rb/rom' | |
gem 'rom-sql', github: 'rom-rb/rom-sql' | |
gem 'rom-repository', github: 'rom-rb/rom-repository' | |
gem 'sqlite3' | |
end | |
config = ROM::Configuration.new(:sql, 'sqlite::memory') | |
gateway = config.gateways[:default] | |
migrations = [] | |
migrations << gateway.migration do | |
change do | |
create_table :users do | |
primary_key :id | |
column :name, String, null: false | |
end | |
end | |
end | |
migrations.each do |migration| | |
migration.apply(gateway.connection, :up) | |
end | |
class Users < ROM::Relation[:sql] | |
def by_id(id) | |
where(id: id) | |
end | |
end | |
config.register_relation(Users) | |
rom = ROM.container(config) | |
class UserRepo < ROM::Repository[:users] | |
commands :create, update: :by_id, delete: :by_id | |
def [](id) | |
users.by_id(id).one | |
end | |
end | |
repo = UserRepo.new(rom) | |
c = repo.create(name: 'Jane') | |
r = repo[c.id] | |
u = repo.update(r.id, name: 'John') | |
d = repo.delete(u.id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment