Class: Metka::QueryBuilder
- Inherits:
-
Object
- Object
- Metka::QueryBuilder
- Defined in:
- lib/metka/query_builder.rb
Constant Summary collapse
- STRATEGIES =
{ all: TagsQuery.new(match: :all).freeze, any: TagsQuery.new(match: :any).freeze }.freeze
- JOINERS =
{ and: ->(nodes) { Arel::Nodes::And.new(nodes) }, or: ->(nodes) { nodes.reduce(:or) } }.freeze
- LEGACY_OPERATORS =
Metka::AND and Metka::OR used to be these Arel classes. Callers that passed them literally still work.
{ Arel::Nodes::And => :and, Arel::Nodes::Or => :or }.freeze
Class Method Summary collapse
-
.instance ⇒ Object
Stateless, so one frozen instance serves every tagged_with call.
Instance Method Summary collapse
Class Method Details
.instance ⇒ Object
Stateless, so one frozen instance serves every tagged_with call.
9 10 11 |
# File 'lib/metka/query_builder.rb', line 9 def self.instance @instance ||= new.freeze end |
Instance Method Details
#call(model, columns, tags, options) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/metka/query_builder.rb', line 30 def call(model, columns, , ) strategy = STRATEGIES.fetch([:any].present? ? :any : :all) nodes = columns.map { |column| strategy.call(model, column, ) } query = join(nodes, using: [:join_operator]) [:exclude].present? ? exclude(query) : query end |