Class: Metka::QueryBuilder

Inherits:
Object
  • Object
show all
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 Method Summary collapse

Class Method Details

.instanceObject

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, tags, options)
  strategy = STRATEGIES.fetch(options[:any].present? ? :any : :all)
  nodes = columns.map { |column| strategy.call(model, column, tags) }
  query = join(nodes, using: options[:join_operator])

  options[:exclude].present? ? exclude(query) : query
end