Skip to content

Instantly share code, notes, and snippets.

@flash-gordon
Created February 13, 2017 17:36
Show Gist options
  • Save flash-gordon/7f4c6fa3c1abc10ddf2033f674735b63 to your computer and use it in GitHub Desktop.
Save flash-gordon/7f4c6fa3c1abc10ddf2033f674735b63 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "bundler/setup"
require "rom-repository"
config = ROM::Configuration.new(:sql, 'postgres://localhost/rom_repository')
config.relation(:books) do
schema(:books) do
attribute :id, ROM::SQL::Types::Serial
attribute :title, ROM::SQL::Types::String
attribute :created_at, ROM::SQL::Types::Time
attribute :updated_at, ROM::SQL::Types::Time
end
end
config.relation(:users) do
schema(infer: true) do
associations do
has_many :tasks
end
end
def by_id(id)
where(id: id)
end
def all
select(:id, :name).order(:name, :id)
end
def find(criteria)
where(criteria)
end
end
config.relation(:tasks) do
schema(infer: true) do
associations do
belongs_to :user
end
end
view(:foos, schema) do
where(title: 'foo')
end
def find(criteria)
where(criteria)
end
def for_users(users)
where(user_id: users.map { |u| u[:id] })
end
end
config.relation(:tags)
rom = ROM.container(config)
rom.gateways[:default].use_logger(Logger.new($stdout))
repo = Class.new(ROM::Repository[:users]) {
commands :create, update: :by_pk, delete: :by_pk
relations :tasks, :tags, :books
}.new(rom)
rel = repo.users.combine(many: { tasks: repo.tasks.where(title: 'bar').foos.for_users })
require "pry"
binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment