rom-rb/rom-repository

View on GitHub
bin/console

Summary

Maintainability
Test Coverage
#!/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

  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)

repo = Class.new(ROM::Repository[:users]) {
  commands :create, update: :by_pk, delete: :by_pk
  relations :tasks, :tags, :books
}.new(rom)

require "pry"
binding.pry