Ruby Object Mapper

1. Set up environment and define schema

  require 'rom'
  require 'axiom-memory-adapter'

  env = ROM::Environment.setup(memory: 'memory://test')

  env.schema do
    base_relation :users do
      repository :memory

      attribute :id,   Integer
      attribute :name, String

      key :id
    end
  end

2. Set up mapping

  class User
    attr_reader :id, :name

    def initialize(attributes)
      @id, @name = attributes.values_at(:id, :name)
    end
  end

  env.mapping do
    users do
      map :id, :name
      model User
    end
  end

3. Work with Plain Old Ruby Objects

  env.session do |session|
    user = session[:users].new(id: 1, name: 'Jane')
    session[:users].save(user)
    session.flush
  end

  jane = env[:users].restrict(name: 'Jane').one