Created
May 21, 2015 22:56
-
-
Save nepalez/3f48353a206eaa244165 to your computer and use it in GitHub Desktop.
ROM commands chaining with mapper
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 "rom" | |
ROM.setup :memory | |
class Users < ROM::Relation[:memory] | |
register_as :users | |
end | |
class Tasks < ROM::Relation[:memory] | |
register_as :tasks | |
end | |
class CreateUser < ROM::Commands::Create[:memory] | |
relation :users | |
register_as :create | |
result :one | |
def execute(user) | |
super user | |
end | |
end | |
class CreateTask < ROM::Commands::Create[:memory] | |
relation :tasks | |
register_as :create | |
result :one | |
def execute(task, user) | |
super task.merge(user) | |
end | |
end | |
class UserToTask < ROM::Mapper | |
relation :users | |
register_as :task | |
reject_keys true | |
attribute :user_id, from: :id | |
end | |
rom = ROM.finalize.env | |
command = rom.command(:users).as(:task).create.with(id: 1, name: "Jane") | |
command >>= rom.command(:tasks).create.with(title: "Sleep well") | |
command.call | |
# => { user_id: 1, title: "Sleep well" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment