rom-rb / rom

Data mapping and persistence toolkit for Ruby
https://rom-rb.org
MIT License
2.08k stars 161 forks source link

Schema attributes #6

Closed valikos closed 11 years ago

valikos commented 11 years ago

Hello there!

I have a question about attributes We have a schema

env.schema do
  base_relation :users do
    repository :memory

    attribute :id,   Integer
    attribute :name, String

    key :id
  end
end

Is there a way to take the attributes and pass them to the model? Because we can have a lot of attributes

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

Or whether in ROM support of Virtus for extend model?

solnic commented 11 years ago

There will be a virtus plugin for ROM soon

valikos commented 11 years ago

Oh, that's great, thanks What about the schema if I understand correctly it is needed to describe the tuples for relational algebra We have two definitions: attributes for schema and attributes name list from schema for model I correctly think?

dkubb commented 11 years ago

@valikos yes, for now you would define the in-memory store as well as the mapping to your objects.

We do think that at some point it will be possible to perform inference so that we can minimize the coupling between layers. I don't think we would be doing this implicitly though, it would probably be explicit.

For example, in theory if you were to define a model using virtus, along with a mapper, then there would probably be enough information for the in-memory schema to be defined.

An other idea, when it is ready, would be to infer the schema from an existing database. Aside from the obvious benefit of saving some typing to write the code for the schema, it should be possible to check the mapper against the schema and warn/raise if there are any mismatches. If there was a virtus model, we could probably take this further and assert all three layers are compatible. This way if there are incompatible changes at one of the layers we could know up-front rather than at runtime.

For now we're requiring that things be declared explicitly for now, but it should be possible to cut down on code in the future.

valikos commented 11 years ago

@dkubb thanks a lot for the explanation