Skip to content

Add support for virtual attributes at the relation definition level #518

@waiting-for-dev

Description

@waiting-for-dev

In the same vein that you can define virtual columns in a database, maybe it could be convenient to have something similar for rom attributes at the relation definition level.

In rom-sql, you can already do something like:

relation.order(relation[:attr_1] - relation[:attr_2])

However, the idea would be to have them declared at the relation definition level, together with a new method which could transparently fetch an actual or a virtual attribute.

I think it would be better not to overload .schema and #[] relation methods for this functionality in order to have a clear separation of concerns. In the same way, defined virtual attributes would not be part of the default dataset (at least by default).

Examples

class Relation < ROM::Relation[:sql]
  schema :users  do
    attribute :attr_1, Types::Integer
    attribute :attr_2, Types::Integer
  end

  virtual do |schema|
    attribute(:diff) { |schema| schema[:attr_1] - schema[:attr_2] }
  end
end

# ...

relation.where(relation.fetch(:attr_1) > 2).order(relation.fetch(:diff))

Sure we could find a better name than fetch....

Of course, I could try it myself once I clean a little my stack of pending PRs...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions