Last active
February 4, 2017 15:13
-
-
Save solnic/45b64362cd9acde4283005b45796d195 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
#!/usr/bin/env ruby | |
require "rom-sql" | |
require "rom-repository" | |
require "dry-types" | |
module Relations | |
class Users < ROM::Relation[:sql] | |
schema(:users, infer: true) | |
end | |
class Participants < ROM::Relation[:sql] | |
schema(:participants, infer: true) | |
end | |
end | |
module Repositories | |
class Users < ROM::Repository[:users] | |
end | |
class Participants < ROM::Repository[:participants] | |
end | |
end | |
config = ROM::Configuration.new(:sql, "sqlite::memory") | |
config.gateways[:default].tap do |gateway| | |
migration = gateway.migration do | |
change do | |
create_table :users do | |
primary_key :id | |
column :name, String | |
column :activated_at, Time | |
end | |
create_table :participants do | |
primary_key :id | |
string :name, null: true | |
column :activated_at, Time | |
end | |
end | |
end | |
migration.apply gateway.connection, :up | |
end | |
class CreateChangeset < ROM::Changeset::Create | |
map do |tuple| | |
tuple.merge(activated_at: Time.now) | |
end | |
end | |
config.register_relation Relations::Users | |
config.register_relation Relations::Participants | |
container = ROM.container(config) | |
user_repo = Repositories::Users.new(container) | |
participant_repo = Repositories::Participants.new(container) | |
puts user_repo.changeset(CreateChangeset).data(name: 'Jane').commit.inspect | |
# #<ROM::Struct[User] id=1 name="Jane" activated_at=2017-02-02 11:39:45 +0100> | |
puts participant_repo.changeset(CreateChangeset).data(name: 'Jane').commit.inspect | |
# #<ROM::Struct[Participant] id=1 name="Jane" activated_at=2017-02-02 11:39:45 +0100> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment