Last active
April 8, 2016 15:02
-
-
Save solnic/b369420c32b1432f6d239db470dbd2d3 to your computer and use it in GitHub Desktop.
Upcoming rom-repository command support
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-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