Skip to content

Instantly share code, notes, and snippets.

@nepalez
Created May 21, 2015 22:56
Show Gist options
  • Save nepalez/3f48353a206eaa244165 to your computer and use it in GitHub Desktop.
Save nepalez/3f48353a206eaa244165 to your computer and use it in GitHub Desktop.
ROM commands chaining with mapper
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