rom-rb/rom-relation — pull request #60 by solnic (2013-07-31 10:40:30+00:00)
Title: [WIP] relation mapping support

This will add support for mapped relations. I'm still unsure where the DSL code should be located - here, in rom-mapper or maybe in rom.

The way I imagine it to work is that after you loaded the schema in the env you can call `Mapping.build(env) {}` to define how relations are mapped, you get a hash of mapped rom relations back. I think it has to support defining those mappings separately in multiple files, kinda like defining DM models in multiple files, because that will be a very common use case.

This leads to a missing object because right now `Environment` already has a registry of relations from all repositories, but those are axiom ones, not rom mapped relations. Which makes me think we need a separate `Registry` that will hold rom relations created via `Mapping.build`. You could create the registry yourself and pass it to the builder method as many times you want, this way we could have mappings defined in multiple files.

Please note that naming is a total WIP as well as implementation. Let me know if you think `Mapping` or `Registry` aren't good names.

Currently I made the DSL to look like this:

``` ruby
# this is what we already have
schema = Schema.build do
base_relation :users do
repository :test

attribute :id, Integer
attribute :user_name, string

key :id
end
end

env = Environment.coerce(test: 'memory://test').load_schema(schema)

# this is the new thing
registry = Mapping.build(env) do
users do
model User

map :id
map :user_name, to: :name
end
end
```

^ this will build a rom relation and inject a rom mapper configured based on the mapping definition. I will also add a way to just provide your mapper instance or specify which loading strategies you want to use (we have 3 right now).

In general my motivation behind the DSL is to have it as slick as possible. I also want to add convenient methods to `Environment` so that you can define schema+mapping as quickly as possible, otherwise that will be a bit discouraging because in order to load a model from a db you need to 1) configure env 2) set up schema 3) load schema into env 4) define mappings which is obviously a great separation and will be used like that in, for example, rails integration *but* in simple cases when you just want to get up and running quickly it will be an overkill (for instance think a dead-simple-one-file-sinatra-app).

Ugh, that was a bit too long, sorry about that but please let me know your thoughts :)

refs #59