Ruby Object Mapper

Gem Version Build Status Dependency Status Code Climate Coverage Status Inline docs

Ruby Object Mapper is an implementation of the Data Mapper pattern in Ruby language. It consists of multiple loosely coupled pieces and uses a powerful relational algebra library called axiom.

Getting started

Currently the setup is a bit verbose. Automatization will come in the future once the API becomes stable.

gem install rom axiom-memory-adapter

1. Set up environment and define schema with mappings

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

  class User
    attr_reader :id, :name

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

  env = ROM::Environment.setup(memory: 'memory://test') do
    schema do
      base_relation :users do
        repository :memory

        attribute :id,   Integer
        attribute :name, String

        key :id
      end
    end

    mapping do
      relation(:users) do
        map :id, :name
        model User
      end
    end
  end

2. 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

Community

Authors

License

See LICENSE file.