Skip to content

Instantly share code, notes, and snippets.

@solnic
Last active April 8, 2016 15:02
Show Gist options
  • Save solnic/b369420c32b1432f6d239db470dbd2d3 to your computer and use it in GitHub Desktop.
Save solnic/b369420c32b1432f6d239db470dbd2d3 to your computer and use it in GitHub Desktop.
Upcoming rom-repository command support
require "rom-repository"
require "rom-sql"
config = ROM::Configuration.new(:sql, 'postgres://localhost/rom_repository')
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)
user = repo.create(name: 'Jane')
#<ROM::Struct[User] id=4 name="Jane">
repo.update(user.id, name: 'Jane Doe')
#<ROM::Struct[User] id=4 name="Jane Doe">
repo.delete(user.id)
#<ROM::Struct[User] id=4 name="Jane Doe">
repo[user.id]
# nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment