Given that an adapter brings its own query API, the implementation of the finders depends on the interface of the current adapter's query.
class ArticleRepository
include Lotus::Repository
def self.most_recent(limit = 5)
query do
desc(:created_at)
end.limit(limit)
end
end
The implementation of .most_recent
won't work if .query
doesn't return a query object that responds to #desc
and #limit
.
So if we do: ArticleRepository.adapter = yaml_adapter
the call to .most_recent
will raise a NoMethodError
.
Of course, I made the assumption here that an hypothetical YamlAdapter::Query
doesn't implement SQL concepts like #desc
.
To overcome this problem, I was thinking to have a different semantic: instead of an assigment to use a DSL + a registry.
Lotus::Model.register_adapter :sql, SqlAdapter.new(...), default: true
Lotus::Model.register_adapter :yaml, YamlAdapter.new(...)
# this defaults to :sql
class ArticleRepository
include Lotus::Repository
end
class CommentRepository
include Lotus::Repository
adapter :yaml
end
Because of this dependency problem, I'm afraid that we should stick with class methods for Lotus::Repository
.
class ArticleRepository
include Lotus::Repository
def initialize(adapter)
@adapter = adapter
end
def most_recent(limit = 5)
query do
desc(:created_at)
end.limit(limit)
end
end
ArticleRepository.new(sql_adapter).most_recent # works fine
ArticleRepository.new(yaml_adapter).most_recent # blows up
What do you think?
But how would you interpret:
This doesn't make sense in that context. Correct me if I am wrong. Or maybe I misunderstood your example.